Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Advertisements

COP 3003 Object-Oriented Programming - Inheritance Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
Chapter 8 – Sections covered
UML Class Diagram: class Rectangle
Chapter 10: Inheritance and Polymorphism
Java SE 8 for Programmers, Third Edition Advanced Java Programming.
Object Oriented Programming using Java - Polymorphism
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Inheritance using Java
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Dr. S. HAMMAMI Ahmad Al-Rjoub Chapter 4 Polymorphism CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Method signature Name and parameter list public static void test() public static int test() => Syntax error, Duplicate method test You cannot declare more.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Outline 1 Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
O O P Polymorphism Object Oriented Programming Prepared & Presented by: dr.Ismail Farahat Chapter 4.
Slide 1 Polymorphism: Part I. Slide 2 Summary: Derived Class * ‘is-a’ relationship n Different from ‘has-a’ or ‘uses-a’, or … by class-in-class * Public.
Class Relationships A class defines a type of data Composition allows an object of another class to define an attribute of a class –Employee “has a”
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Inheritance.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Object Oriented Programming
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Outline Class and Object-Oriented Programing –Encapsulation and inheritance Example –Commission Employee –Encapsulation and inheritance Strings and Formatting.
1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface.
Inheritance ndex.html ndex.htmland “Java.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
Object-Oriented Programming: Polymorphism
One class is an extension of another.
Chapter 11: Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
UML Class Diagram: class Rectangle
Object-Oriented Programming: Polymorphism
One class is an extension of another.
Object-Oriented Programming: Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9 Object-Oriented Programming: Inheritance
Polymorphism CT1513.
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Abstract Classes Page
Polymorphism: Part I.
Recitation Course 0610 Speaker: Liu Yu-Jiun.
Object-Oriented Programming: Inheritance
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Inheritance
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Abstract Classes

Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract methods inherited from an abstract superclass

Abstract Classes An abstract class is not intended to be used to create objects. Abstract classes:  Classes that are too general to create real objects  Used only as abstract superclasses for concrete subclasses and to declare reference variables  Many inheritance hierarchies have abstract superclasses occupying the top few levels  Keyword abstract Use to declare a class abstract Also use to declare a method abstract –Abstract classes normally contain one or more abstract methods –All concrete subclasses must override all inherited abstract methods

Abstract Classes (Cont’d) By declaring one or more methods to be abstract and by omitting the method body, only objects of derived classes which override the method(s) can be instantiated. Example: public abstract void drawHere(); A class that has at least one abstract method must be declared abstract. However, a class could be always declared abstract without having any abstract method!

Abstract Classes (Cont’d) An abstract class declares common attributes and behaviors of the various classes in a class hierarchy. An abstract class typically contains one or more abstract methods that subclasses must override if the subclasses are to be concrete. The instance variables and concrete methods of an abstract class are subject to the normal rules of inheritance.

Attempting to instantiate an object of an abstract class is a compilation error. Example: Abstract Classes (Cont’d) abstract class Shape { … } Shape sh = new Shape();

Failure to implement a superclass’s abstract methods in a subclass is a compilation error unless the subclass is also declared abstract. Example: Abstract Classes (Cont’d) abstract class Shape { public abstract void drawHere(); } class Rectangle extends Shape { // The method drawHere() is NOT implemented! }

Abstract Classes (Cont’d) Example: public abstract class AbstractClassExample { protected int x; public void abstract print(); public void setX(int a) { x = a; } public AbstractClassExample() { x = 0; }

UML Inheritance Diagrams for Abstract Classes

Creating Abstract Superclass: Employee class abstract superclass Employee  earnings is declared abstract No implementation can be given for earnings in the Employee abstract class  An array of Employee variables will store references to subclass objects earnings method calls from these variables will call the appropriate version of the earnings method

The Employee Class Hierarchy

Abstract Class Employee: Outline // Employee abstract superclass. public abstract class Employee { private String firstName; private String lastName; private String socialSecurityNumber; // three-argument constructor public Employee( String first, String last, String ssn ) { firstName = first; lastName = last; socialSecurityNumber = ssn; } // end three-argument Employee constructor Declare abstract class Employee Attributes common to all employees

Abstract Class Employee: Outline (Cont’d) // set first name public void setFirstName( String first ) { firstName = first; } // end method setFirstName // return first name public String getFirstName() { return firstName; } // end method getFirstName // set last name public void setLastName( String last ) { lastName = last; } // end method setLastName // return last name public String getLastName() { return lastName; } // end method getLastName

Abstract Class Employee: Outline (Cont’d) // set social security number public void setSocialSecurityNumber( String ssn ) { socialSecurityNumber = ssn; // should validate } // end method setSocialSecurityNumber // return social security number public String getSocialSecurityNumber() { return socialSecurityNumber; } // end method getSocialSecurityNumber // return String representation of Employee object public String toString() { return String.format( "%s %s\nsocial security number: %s", getFirstName(), getLastName(), getSocialSecurityNumber() ); } // end method toString // abstract method overridden by subclasses public abstract double earnings(); // no implementation here } // end abstract class Employee abstract method earnings() has no implementation

Implementing an Abstract Method Define the speak() method as abstract in the superclass Animal. public abstract class Animal { protected String kind; // Cow, pig, cat, etc. public Animal() { } public String toString() { return "I am a " + kind + " and I go " + speak(); } public abstract String speak(); // Abstract method } Implement speak() differently in each subclass. public class Cat extends Animal { public Cat() { kind = "cat"; } public String speak() { return "meow"; } public class Cow extends Animal { public Cow() { kind = "cow"; } public String speak() { return "moo"; }

Extending Concrete Classes public class SalariedEmployee extends Employee { private double weeklySalary; … // calculate earnings; override abstract method earnings in Employee public double earnings() { return getWeeklySalary(); } // end method earnings … }

Extending Concrete Classes (Cont’d) public class CommissionEmployee extends Employee { private double grossSales; // gross weekly sales private double commissionRate; // commission percentage… // calculate earnings; override abstract method earnings in Employee public double earnings() { return getCommissionRate() * getGrossSales(); } // end method earnings … }

Extending Concrete Classes (Cont’d) public class HourlyEmployee extends Employee { private double wage; // wage per hour private double hours; // hours worked for week // calculate earnings; override abstract method earnings in Employee public double earnings() { if ( getHours() <= 40 ) // no overtime return getWage() * getHours(); else return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5; } // end method earnings