Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg eaymanelshenawy@yahoo.com eaymanelshenawy@gmail.com Lecture 4 Inheritance and Polymorphism 1
Objectives IN THIS Lecture, YOU WILL: Learn about inheritance Learn about subclasses and superclasses Explore how to override the methods of a superclass Examine how constructors of superclasses and subclasses work Learn about polymorphism Examine abstract classes Become familiar with interfaces Learn about composition 4 Encapsulation: OOP you can combine data and operations on that data in a single unit Operations can (directly) access the data, but the internal state of an object cannot be manipulated directly. 5 2
Inheritance You can create new classes from an existing classes. subclass (Circle) can extend base class (Shape). subclass (Car) can extend base class (vehicle). The new created class is called a subclass /derived class; The original class is called super or base class Reuse the code and save the programmer time. Inheritance ( is-a relationship) Composition (aggregation) ( has-a relationship) Many common features Best class design to avoid redundancy? 6 Inheritance The inheritance hierarchy describes a relationship between entities. A poodle is a dog, which is a mammal, which is an animal. A poodle has the same properties as a dog and can do anything a dog can do PartTimeEmployee (pay rate, and number of hours worked) is a person (first name, and last name) Able to extend the definition of the class Person by adding additional members (data and/or methods). 7 3
public class Person { private String firstname; //store the first name private String lastname; //store the last name //Default constructor; //Initialize firstname and lastname to empty string. public Person() { firstname = ""; lastname = ""; } //Set firstname and lastname public Person(String first, String last){ setname(first, last); } //Method to output the first name and last name public String tostring(){ return (firstname + " " + lastname); } //Method to set firstname and lastname public void setname(string first, String last) { firstname = first; lastname = last; } //Method to return the firstname public String getfirstname() { return firstname; } Inheritance } //Method to return the lastname public String getlastname(){ return lastname; } 8 Inheritance Shape is the superclass. The classes Circle and Rectangle are derived classes from Shape, and the class Square is derived class from Rectangle. Every Circle and every Rectangle is a Shape. Every Square is a Rectangle. 9 4
Super class and sub class rules 1. The private members of the superclass are private to the superclass; hence, the members of the subclass(es) cannot access them directly. 2. The subclass can directly access the public members of the superclass. 3. The subclass can include additional data and/or method members. 4. The subclass can override (redefine) the public methods of the superclass. The same method, number, and types of may be exist in the sub-class. Only affect objects of the subclass, not to the objects of the superclass. 5. All data members/methods of the superclass are also data members of the subclass (unless overridden). 10 single inheritance and multiple inheritance Single inheritance: the subclass is derived from a single superclass; Multiple inheritance, the subclass is derived from more than one superclass. Java supports only single inheritance; that is, in Java a class can extend the definition of only one class. 11 5
single inheritance and multiple inheritance Single inheritance: the subclass is derived from a single superclass; Multiple inheritance, the subclass is derived from more than one superclass. Java supports only single inheritance; that is, in Java a class can extend the definition of only one class. 12 Overriding A class SubClass is derived from a class SuperClass. The SubClass data and methods are the SuperClass data and methods + its own data and methods. Overriding : adding a method with the same signature as given by the superclass. To override a public method of the superclass in the subclass, the corresponding method in the subclass must have the same name, type, and parameter list. Overloading: If the corresponding method in the superclass and the subclass has the same name but different parameter lists, then this is method overloading in the subclass, which is also allowed. Whether you override or overload a method of the superclass in the subclass, you must know call to the superclass that has the same name as that used by a method of the subclass. 13 6
Rectangle Example UML class diagram of the class Rectangle. Rectangle Example 7
Class Box Example The class Box overrides the methods tostring and area, and overloads the method setdimension. Class Box Example 8
Keyword: super How to access superclass? keyword super refers to the superclass super can be used: To call a superclass constructor To call a superclass method To call an override method use Super.methodName(parms) To call non-override method Use the name of the method and parameters. 18 Keyword: super 19 9
Keyword: super 20 Are superclass s Constructors Inherited? No. They are not inherited. How to invoke superclass s constructors? Explicitly using the super keyword. Implicitly call the no-arg constructor if it exists. (Otherwise: compilation error) 21 10
Superclass s Constructor Is Always Invoked Subclass s constructor may invoke any of superclass s constructors. If none invoked explicitly, super() inserted at beginning. public A() { } is equivalent to public A() { super(); } public A(double d) { // some statements } is equivalent to public A(double d) { super(); // some statements } 22 Keyword: super (explicitly) specify a call to the default constructor of the class Rectangle, Use the reserved word super with no parameters, Call to the (default) constructor of the superclass must be the first statement. When this constructor of Box executes, it triggers the execution of the constructor with two parameters of type double of the class Rectangle. 23 11
How Super class and sub-class works 24 25 12
26 CAUTION You must use the keyword super to call the superclass constructor. Syntax error: Invoking superclass s constructor by name in subclass. Call to a superclass constructor must appear first in constructor. 27 13
public class Person { private String firstname; //store the first name private String lastname; //store the last name //Default constructor; //Initialize firstname and lastname to empty string. public Person() { firstname = ""; lastname = ""; } //Set firstname and lastname public Person(String first, String last){ setname(first, last); } //Method to output the first name and last name public String tostring(){ return (firstname + " " + lastname); } //Method to set firstname and lastname public void setname(string first, String last) { firstname = first; lastname = last; } //Method to return the firstname public String getfirstname() { return firstname; } Inheritance } //Method to return the lastname public String getlastname(){ return lastname; } 28 Inheritance 29 14
PartTimeEmployee Constructors 30 31 15
Protected Members of class if a member of a superclass needs to be accessed directly (only) by a subclass, that member is declared using the modifier protected. 32 33 16