Introduction Inheritance Software reusability Create new class from
Introduction • Inheritance – Software reusability – Create new class from existing class • Absorb existing class’s data and behaviors • Enhance with new capabilities – Subclass extends superclass • Subclass – More specialized group of objects – Behaviors inherited from superclass • Can customize – Additional behaviors AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Introduction (Cont. ) • Class hierarchy – Direct superclass • Inherited explicitly (one level up hierarchy) – Indirect superclass • Inherited two or more levels up hierarchy – Single inheritance • Inherits from one superclass – Multiple inheritance • Inherits from multiple superclasses – Java does not support multiple inheritance AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Superclasses and subclasses • Superclasses and subclasses – Object of one class “is an” object of another class • Example: Rectangle is quadrilateral. – Class Rectangle inherits from class Quadrilateral – Quadrilateral: superclass – Rectangle: subclass – Superclass typically represents larger set of objects than subclasses • Example: – superclass: Vehicle • Cars, trucks, boats, bicycles, … – subclass: Car • Smaller, more-specific subset of vehicles AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Inheritance examples AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Superclasses and subclasses (Cont. ) • Inheritance hierarchy – Inheritance relationships: tree-like hierarchy structure – Each class becomes • superclass – Supply members to other classes OR • subclass – Inherit members from other classes AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Inheritance hierarchy for Shapes. AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
protected Members • protected access – Intermediate level of protection between public and private – protected members accessible by • superclass members • subclass members • Class members in the same package – Subclass access to superclass member • Keyword super and a dot (. ) AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Relationship between Superclasses and Subclasses • Superclass and subclass relationship – Example: Commission. Employee/Base. Plus. Commissi on. Employee inheritance hierarchy • Commission. Employee – First name, last name, SSN, commission rate, gross sale amount • Base. Plus. Commission. Employee – First name, last name, SSN, commission rate, gross sale amount – Base salary AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Creating and Using a Commission. Employee Class • Class Commission. Employee – Extends class Object • Keyword extends • Every class in Java extends an existing class – Except Object • Every class inherits Object’s methods • New class implicitly extends Object – If it does not extend another class AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Declare private instance variables Class Commission. Employee extends class Object Implicit call to Object constructor Initialize instance variables Invoke methods set. Gross. Sales and set. Commission. Rate to validate data AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Calculate earnings AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Override method to. String of class Object AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Common Programming Error If a public method could be overridden as a protected or private method, the subclass objects would not be able to respond to the same method calls as superclass objects. Once a method is declared public in a superclass, the method remains public for all that class’s direct and indirect subclasses. AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Instantiate Commission. Employee object Use Commission. Employee’s get methods to retrieve the object’s instance variable values Use Commission. Employee’s set methods to change the object’s instance variable values AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Implicitly call object’s to. String method AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Creating a Base. Plus. Commission. Employee Class without Using Inheritance • Class Base. Plus. Commission. Employee – Implicitly extends Object – Much of the code is similar to Commission. Employee • private instance variables • public methods • constructor – Additions • private instance variable base. Salary • Methods set. Base. Salary and get. Base. Salary AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Add instance variable base. Salary Use method set. Base. Salary to validate data AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Method set. Base. Salary validates data and sets instance variable base. Salary Method get. Base. Salary returns the value of instance variable base. Salary Update method earnings to calculate the earnings of a base-salaried commission employee Update method to. String to display base salary AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Instantiate Base. Plus. Commission. Employee object Use Base. Plu. Commission. Employee’s get methods to retrieve the object’s instance variable values AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Use Base. Plus. Commission. Empl oyee’s set. Base. Salary methods to set base salary Explicitly call object’s to. String method AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Software Engineering Observation With inheritance, the common instance variables and methods of all the classes in the hierarchy are declared in a superclass. When changes are required for these common features, software developers need only to make the changes in the superclass—subclasses then inherit the changes. Without inheritance, changes would need to be made to all the source code files that contain a copy of the code in question. AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Creating a Commission. Employee. Base. Plus. Commiion. Employee Inheritance Hierarchy • Class Base. Plus. Commission. Employee 2 – Extends class Commission. Employee – Is a Commission. Employee – Has instance variable base. Salary – Inherits public and protected members – Constructor not inherited AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Class Base. Plu. Commission. Employee 2 is a subclass of Commission. Employee Invoke the superclass constructor using the superclass constructor call syntax AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Compiler generates errors because superclass’s instance variable commission. Rate and gross. Sales are private Compiler generates errors because superclass’s instance variable first. Name, last. Name, social. Security. Number, gross. Sales and commission. Rate are private AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Commission. Employee. Base. Plus. Commission. Employee Inheritance Hierarchy Using protected Instance Variables • Use protected instance variables – Enable class Base. Plus. Commission. Employee to directly access superclass instance variables – Superclass’s protected members are inherited by all subclases of that superclass AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Declare protected instance variables AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Must call superclass’s constructor AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Directly access superclass’s protected instance variables AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Commission. Employee. Base. Plus. Commission. Employee Inheritance Hierarchy Uing private Instance Variables • Reexamine hierarchy – Use the best software engineering practice • Declare instance variables as private • Provide public get and set methods • Use get method to obtain values of instance variables AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Declare private instance variables AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Use get methods to obtain the values of instance variables AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Inherits from Commission. Employee 3 AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Invoke an overridden superclass method from a subclass Use get methods to obtain the values of instance variables Invoke an overridden superclass method from a subclass AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Create Base. Plus. Commission. Employee 4 object. Use inherited get methods to access inherited private instance variables Use Base. Plus. Commission. Employee 4 get method to access private instance variable. AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Use Base. Plus. Commission. Employee 4 set method to modify private instance variable base. Salary. AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Commission. Employee. Base. Plus. Commission. Employee Inheritance Hierarchy Using protected Instance Variables (Cont. ) • Using protected instance variables – Advantages • subclasses can modify values directly • Slight increase in performance – Avoid set/get method call overhead – Disadvantages • No validity checking – subclass can assign illegal value • Implementation dependent – subclass methods more likely dependent on superclass implementation – superclass implementation changes may result in subclass modifications • Fragile (brittle) software AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Constructors in Subclasses • Instantiating subclass object – Chain of constructor calls • subclass constructor invokes superclass constructor – Implicitly or explicitly • Base of inheritance hierarchy – Last constructor called in chain is Object’s constructor – Original subclass constructor’s body finishes executing last AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Software Engineering Observation When a program creates a subclass object, the subclass constructor immediately calls the superclass constructor (explicitly, via super, or implicitly). The superclass constructor’s body executes to initialize the superclass’s instance variables that are part of the subclass object, then the subclass constructor’s body executes to initialize the subclass-only instance variables. (cont…) AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Inheritance hierarchy for university Community. Members AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Common Programming Error A compilation error occurs if a subclass constructor calls one of its superclass constructors with arguments that do not match exactly the number and types of parameters specified in one of the superclass constructor declarations. AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Object Class • All classes in Java inherit directly or indirectly from class Object • Class Object methods – – – – clone equals finalize get. Class hash. Code notify, notify. All, wait to. String AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
OBJECTIVES In this lection you will learn: • The concept of polymorphism. • To use overridden methods to effect polymorphism. • To distinguish between abstract and concrete classes. • To declare abstract methods to create abstract classes. • How polymorphism makes systems extensible and maintainable. • To determine an object's type at execution time. • To declare and implement interfaces. AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Introduction • Polymorphism – Enables “programming in the general” – The same invocation can produce “many forms” of results • Interfaces – Implemented by classes to assign common functionality to possibly unrelated classes AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Polymorphism Examples • Polymorphism – When a program invokes a method through a superclass variable, the correct subclass version of the method is called, based on the type of the reference stored in the superclass variable – The same method name and signature can cause different actions to occur, depending on the type of object on which the method is invoked – Facilitates adding new classes to a system with minimal modifications to the system’s code AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Demonstrating Polymorphic Behavior • A superclass reference can be aimed at a subclass object – This is possible because a subclass object is a superclass object as well – When invoking a method from that reference, the type of the actual referenced object, not the type of the reference, determines which method is called • A subclass reference can be aimed at a superclass object only if the object is downcasted AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Typical reference assignments AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Assign a reference to a base. Plus. Comm ission. Employ ee object to a Commission. Em ployee 3 variable Polymorphically call base. Plus. Commission. Employee’s to. String method AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Abstract Classes and Methods • 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 AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Software Engineering Observation 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. AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Employee hierarchy UML class diagram. AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Creating Abstract Superclass Employee • 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 AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Declare abstract class Employee Attributes common to all employees AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
abstract method earnings has no implementation AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline Class Salaried. Employee extends class Employee Call superclass constructor Call set. Weekly. Salary method Validate and set weekly salary value AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Override earnings method so Salaried. Employee can be concrete Override to. String method Call superclass’s version of to. String AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Creating and Using Interfaces • Interfaces – Keyword interface – Contains only constants and abstract methods • All fields are implicitly public, static and final • All methods are implicitly public abstract methods – Classes can implement interfaces • The class must declare each method in the interface using the same signature or the class must be declared abstract – Typically used when disparate classes need to share common methods and constants – Normally declared in their own files with the same names as the interfaces and with the. java file-name extension AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Developing a Payable Hierarchy • Payable interface – Contains method get. Payment. Amount – Is implemented by the Invoice and Employee classes • UML representation of interfaces – Interfaces are distinguished from classes by placing the word “interface” in guillemets ( « and » ) above the interface name – The relationship between a class and an interface is known as realization • A class “realizes” the method of an interface AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Good Programming Practice When declaring a method in an interface, choose a method name that describes the method’s purpose in a general manner, because the method may be implemented by a broad range of unrelated classes. AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Payable interface hierarchy UML class diagram. AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Declare interface Payable Declare get. Payment. Amount method which is implicitly public and abstract AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE Class Invoice implements interface Payable FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE FACULTY OF ENGINEERING & SCIENCE
Outline AGDER COLLEGE Declare get. Payment. Amount to fulfill contract with interface Payable FACULTY OF ENGINEERING & SCIENCE
- Slides: 78