Upload
grant-jefferson
View
216
Download
2
Tags:
Embed Size (px)
Citation preview
INHERITANCE
Defining Classes with Inheritance
• Case Study:– Suppose we want implement a class roster that contains
both undergraduate and graduate students.– Each student’s record will contain his or her name, three
test scores, and the final course grade.– The formula for determining the course grade is different
for graduate students than for undergraduate students.
Undergrads: pass if avg test score >= 70
Grads: pass if avg test score >= 80
Modeling Two Types of Students
• There are two ways to design the classes to model undergraduate and graduate students.– We can define two unrelated classes, one for
undergraduates and one for graduates.– We can model the two kinds of students by using
classes that are related in an inheritance hierarchy.
• Two classes are unrelated if they are not connected in an inheritance relationship.
Classes for the Class Roster
• For the Class Roster sample, we design three classes:– Student
– UndergraduateStudent
– GraduateStudent
• The Student class will incorporate behavior and data common to both UndergraduateStudent and GraduateStudent objects.
• The UndergraduateStudent class and the GraduateStudent class will each contain behaviors and data specific to their respective objects.
Inheritance Hierarchy
+ computeCourseGrade() : int + computeCourseGrade() : int
+ computeCourseGrade() : int
+ UndergraduateStudent() : void + GraduateStudent() : void
Definition of GraduateStudent & UndergraduateStudent classes
class GraduateStudent extends Student {
//constructor not shown
public void computeCourseGrade() { int total = 0; total = test1 + test2 + test3; if (total / 3 >= 80) { courseGrade = "Pass"; } else { courseGrade = "No Pass"; } }}
class UndergraduateStudent extends Student {
//Constructor not shown
public void computeCourseGrade() { int total = 0;
total = test1 + test2 + test3;
if (total / 3 >= 70) { courseGrade = "Pass"; } else { courseGrade = "No Pass"; } }
}
Declaring a Subclass
A subclass inherits data and methods from the superclass. In the subclass, you can also:
Add new data
Add new methods
Override the methods of the superclass Modify existing behaviour of parent
Inheritance Rules
1. The private members of the superclass are private to the superclass
2. The subclass can access the members of the superclass according to the accessibility rules
3. The subclass can include additional data and/or method members
Inheritance Rules (continued)
4. The subclass can override, that is, redefine the methods of the superclass
The overriding method in subclass must have similar Name
Parameter list
Return type
5. All members of the superclass are also members of the subclass– Similarly, the methods of the superclass (unless
overridden) are also the methods of the subclass– Remember Rule 1 & 2 when accessing a member
of the superclass in the subclass
• To call a superclass constructor
– super(); //must be the first statement in subclass’s constructor
• To call a superclass method
– super.methodname();
– this is only used if the subclass overrides the superclass method
6. (Using the Keyword super)
The keyword super refers to the direct superclass of a subclass . This keyword can be used in two ways:
Inheritance Rules (continued)
(Accessibility Modifier)
• Sometimes , it is called visibility modifier• Not all properties can be accessed by sub class.• Super class can control a data accessing from subclass
by giving the type of accessing to the members and methods.
• A class can declare the data members or method as a public, private or protected.
• If it is not declared, the data or method will be set to default type.
INHERITANCE
Sub class B
public int bprotected int c
Super class
int apublic int b
protected int cprivate int d
Sub class A
int apublic int b
protected int c
Package B
Package A
Data Accessibility
INHERITANCE
Refer to the previous slide
• Super class has 2 subclasses : Subclass A and Subclass B.
• Subclass A is defined in same package with superclass, subclass B is defined outside the package.
• There are 4 accessibility data types: public, protected, private and default.
• Subclass A can access all properties of superclass except private.
• But, subclass B can only access the properties outside the package which are public and protected.
What’s wrong with the code?How to fix it?
class ClassX{ private int m; public String toString() { return new String("(" + m + ")"); }}public class ClassY extends ClassX{ private int n; public String toString() { return new String("(" + m + " , " + n + ")"); }}
class TestAccesibility{ public static void main(String [] args) { ClassX x = new ClassX; ClassY y = new ClassY; System.out.println("x = " + x); System.out.println("y = " + y); }}
Inheritance and Constructors
• Unlike members of a superclass, constructors of a superclass are not inherited by its subclasses.
• You must define a constructor for a class or use the default constructor added by the compiler.
• The statement super();
calls the superclass’s constructor.• super(); must be the first statement in the
subclass contructor.
public Box(double l, double w, double h){ super(l,w); height = h;}
• A call to the constructor of the superclass must be in the first statement in the child constructor.
Rectangle myRectangle = new Rectangle(5, 3);Box myBox = new Box(6, 5, 4);
Superclass’s Constructor Is Always Invoked
A subclass constructor may invoke its superclass’s constructor. If none is invoked explicitly, the compiler puts super() as the first statement in the constructor. For example, the constructor of class A:
public A(double d) { // some statements
}
is equivalent to
public A(double d) { super(); // some statements
}
public A() {
}
is equivalent to
public A() { super();
}
Example on the Impact of a Superclass without no-arg Constructor
class Fruit { public Fruit(String name) { System.out.println("Fruit constructor is invoked"); }}
public class Apple extends Fruit { public Apple(String name) { System.out.println(“Apple constructor is invoked"); }}
Find out the error in the program: