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 { // data memebers public final static int NUM_TESTS =3; private String myName; private int[] myTests; private String myGrade; //constructors public Student(){ myName =""; 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; } 

Inheritance and Polymorphism.pdf

There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Inheritance and ...

185KB Sizes 4 Downloads 179 Views

Recommend Documents

JUDGEMENT ON RIGHTS OF INHERITANCE AND SUCCESSION ...
Main menu. Displaying JUDGEMENT ON RIGHTS OF INHERITANCE AND SUCCESSION OF CHRISTIAN PRIESTS AND NUNS BY KERALA HIGH COURT.pdf.

PRINCIPLES OF INHERITANCE AND VARIATION.pdf
Mendel was an Austrian Monk who is considered as the “father of genetics “ ... The seeds produced as a result of this cross are collected and grew. ... Eg . tall , dwarf , red flower , violet flower , round seed , wrinkled seed, axial flower ,. t

Mulhall, Book Review, Inheritance and Originality, Wittgenstein ...
Mulhall, Book Review, Inheritance and Originality, Wittgenstein, Heidegger, Kierkegaard.pdf. Mulhall, Book Review, Inheritance and Originality, Wittgenstein, ...

Lamarckian Inheritance
sometimes known as 'soft' heredity). The Lamarckian theory is thus rejected ... habit-directed form of evolu- tion than with Darwinian 'trial and error'. There were.

proper inheritance - GitHub
All essential behavior of our software must be documented, and yet there are important advantages, with respect to development, verification and testing,.

Java-Inheritance, Interface & Exception
There is a special way to call the superclass's constructor. – There is ... BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37);. Box plainbox = new Box();.

Java-Inheritance, Interface & Exception
System.out.println(k);. } void sum() {. System.out.println(i+j+k);. } } class SimpleInheritance { public static void main(String args[]) {. A superOb = new A();. B subOb ...

Mixin-based Inheritance - Semantic Scholar
Department of Computer Science ... enforces a degree of behavioral consistency between a ... reference, within the object instance being defined. The.

Ga/No Conversion and Feature Inheritance Masao Ochi ...
Dec 9, 2017 - English ECM as 'optional' raising (Lasnik 1999). (65) a. Mary made John out to be a liar. b. Mary made out John to be a liar. (66) Mary believes him to be a fool. (67) FI from v to V (raising to the matrix VP) a. [C [Mary T [vP v [VP be

Man's Supreme Inheritance by F.M.Alexander.pdf
5 JOHNSON'S COURT, E.C.4. LONDON. Page 3 of 239. Man's Supreme Inheritance by F.M.Alexander.pdf. Man's Supreme Inheritance by F.M.Alexander.pdf.

The-Inheritance-The-Innkeepers.pdf
eBook PDF The Inheritance (The Innkeepers). (-eBooks-) The Inheritance (The .... Download John Davies ebook file totally free and this. book pdf identified at ...

INHERITANCE MECHANISM OF SOME YIELD COMPONENTS IN.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. INHERITANCE ...