Inheritance and Polymorphism.pdf - Google Drive

4 downloads 142 Views 185KB Size Report
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to op
INHERITANCE AND POLYMORPHISM Super classes and subclasses Inheritance hierarchy

SUPERCLASS AND SUBCLASS 

Inheritance defines a relationship between objects that share characteristics.



It is the mechanism whereby a new class, called a subclass, is created form an existing class, called a superclass, by absorbing its states and behavior and augmenting these with features unique to the new class.



Subclass inherits characteristics of its superclass.



Inheritance provides an effective mechanism for code reuse

INHERITANCE HIERARCHY A subclass can itself be a superclass for another subclass. Inheritance hierarchy.  Consider the relationship between objects: Person, Employee, Student, GradStudent, and UnderGrad.



Person

Student

GradStudent

Employee

UnderGrad

For any of these classes, an arrow points to its superclass.  Employee is a Person  Student is a Person  GradStudent is a Student  UnderGrad is a Student 

INHERITANCE HIERARCHY Suppose the Person class has instance variables name, socialSecurityNumber, age, and instance methods getName, getSocSecNum, getAge, and printName.  Every one of the derived classes shown inherits these variables and methods. 

INHERITANCE HIERARCHY 

The Student class may have additional instance variables studentId and gpa, plus a method computeGrade. All of this additional features are inherited by the subclasses GradStudent and UnderGrad.  GradStudent and UnderGrad could use different algorithms for computing the course grade. Method computeGrade could redefined for these classes.  This is called method overriding. 

IMPLEMENTING SUBCLASSES THE EXTENDS KEYWORD The inheritance between a subclass and superclass is specified in the declaration of the subclass, using word extends. public class SuperClass { //private instance variables //constructors //public methods //private methods } public class Subclass extends SuperClass{ //additional private instance variables //constructors (not inherited) //additional public methods //inherited public methods whose implementation is overridden //additional private methods } 

FOR EXAMPLE, CONSIDER THE FOLLOWING INHERITANCE HIERARCHY:

Student

GradStudent



UnderGrad

The implementation of the classes may look like this

public class Student { // ; myTests = new int[NUM_TESTS]; myGrade =""; }

public Student(String name, int[] tests, String grade){ myName = name; myTests = tests; myGrade = grade; }

public String getName() { return myName;} public String getGrade() { return myGrade; } public void setGrade(String newGrade) {myGrade = newGrade;} public void computeGrade(){ if (myName.equals("")) myGrade = "No grade"; else if(getTestAverage() >= 65) myGrade = "Pass"; else myGrade = "Fail"; } public double getTestAverage(){ double total = 0; for(int score : myTests) total += score; return total/NUM_TESTS; } }

public class UnderGrad extends Student{ public UnderGrad() // default constructor {super();} // constructor public UnderGrad(String name, int[] tests, String grade) { super(name, tests, grade);} public void computeGrade(){ if(getTestAverage() > 70) setGrade("Pass"); else setGrade("Fail"); } }

public class GradStudent extends Student{ private int myGradID; public GradStudent(){ //default constructor super(); myGradID = 0; } // constructor public GradStudent(String name, int[] tests, String grade, int gradID){ super(name, tests, grade); myGradID = gradID; } public int getID() {return myGradID; } public void computeGrade(){ //invokes computeGrade in Student superclass super.computeGrade(); if(getTestAverage() >=90) setGrade("Pass with distinction"); } }

IMPORTANT WHEN USING SUBCLASSES When you write a subclass make sure to provide at least one constructor  Constructors are never inherited form the superclass. 

INHERITANCE INHERITING INSTANCE METHODS AND VARIABLES  

   

The UnderGrad and GradStudent subclasses inherit all of the methods and variables of the Student superclass. The Student instance variables myName, myTests, and myGrade are private, and are therefore not directly accessible to the methods in the UnderGrad and GradStudent subclasses. A subclass can, howerver, directly invoke the public accessor and mutator methods of the superclass. Both UnderGrad and GradStudent use setGrade to access indirectly and modify myGrade. Classes on the same level in hierarchy diagram do not inherit anything form each other. UnderGrad and GradStudent have only identical code they inherit from their superclass.

INHERITANCE METHOD OVERRIDING AND THE super KEYWORD A method in superclass is overridden in a subclass by defining a method with the same return type and signature (name and parameter types).  The computeGrade method in the UnderGrad subclass overrides the computeGrade method in the Student superclass.  Partial overriding is a case when code for overriding a method includes a call to the superclass method.  It is used when subclass method wants to do what the superclass does, plus something extra. In this case keyword super is used in implementation.  super.computeGrade();  invokes method from superclass. 

INHERITANCE

CONSTRUCTORS AND super Constructors are never inherited!  If no constructor is written for a subclass, the superclass default constructor with no parameters is generated.  If superclass does not have a default constructor, but only a constructor with parameters, a compiler error will occur.  If there is a default constructor in the superclass, inherited data members will be initialized as for the superclass, but additional instance variables will get default zero values for primitives and null for reference types. 

INHERITANCE

CONSTRUCTORS AND super A subclass constructor can be implemented with a call to the super method, which invokes the superclass constructor.  The default constructor in the UnderGrad class is identical to that of the Student class.  Implemented with the statement: 

super() 

The second constructor in the UnderGrad class is called whit parameters that match those in the constructor of the Student superclass

public UnderGrad(String name, int[] tests, String grade)

{ super(name, tests, grade);}

INHERITANCE

CONSTRUCTORS AND super For each constructor, the call to super as the effect of initializing the inherited instance variables myName, myTests, and myGrade exactly as the are initialized in the Student class.  In the GradStudent, the inherited instance variables myName, myTest, and myGrade are initialized as for the Student class, but the new instance variable, myGradID must be explicitly initialized. public GradStudent(){ //default constructor super(); myGradID = 0; } // constructor public GradStudent(String name, int[] tests, String grade, int gradID){ super(name, tests, grade); myGradID = gradID; } 