Professional Documents
Culture Documents
3. PNR Inheritance
3. PNR Inheritance
REUSABILITY
Model Number
Color
Weight
extends
Voice command
Roadside Assistance
Airbags
• Suppose you need to define classes to model
circles, rectangles, and triangles.
• These classes have many common features
• What is the best way to design these classes so
– as to avoid redundancy and
– make the system easy to comprehend and
– easy to maintain?
• The answer is to use inheritance.
Superclasses and Subclasses
• Inheritance enables you to define a general
class (e.g., a superclass) and later extend it to
more specialized classes (e.g., subclasses).
• Whenever you inherit/extend a class, a copy
of superclass’s members is created in the
subclass object and thus, using the subclass
object you can access the members of both
classes.
Example
In the following example we have a class named SuperClass with a method with name
demo(). We are extending this class with another class (SubClass).
Now, you create an object of the subclass and call the method demo().
class SuperClass{
public void demo() {
System.out.println("demo method");
}
}
public class SubClass extends SuperClass {
public static void main(String args[]) {
SubClass obj = new SubClass();
obj.demo();
}
}
Output :
demo method
class Calculation {
int z;
Super
Keyword
Use of Super Keyword
Call
Constructor of Call Method
super class of super class
• The syntax to call a superclass’s constructor is:
• super(), or super(parameters);
Cuboid(int l){
this(l,l,l);
}
• Writing only super() will give compilation error because
super class does not contain no argument constructor
The super keyword
• It is used to differentiate the members of
superclass from the members of subclass, if
they have same names.
• It is used to invoke the superclass constructor
from subclass.
Method Overriding
Inherit Override
Method Method
Method Overriding and use of super keyword
ME a2=new ME();
a2.setData();
a2.display();
}
}
Variable shadowing
How many
variables??
• How many instance variable in above
example?
• Ans – 3 (length and width from super class and
length of subclass)
• Length in cuboid is known as shadow variable
How to call variable with same name??
Solution
super
keyword
30*20=600 10*20=200
Find output
class Rectangle { class Cuboid extends Rectangle{
int length; int length ; // shadow variable
int width; //remains zero since it is not initialized
int area() { from constructor
return length*width;
}
int height;
void setDimensions(int l, int w) {
int volume() {
length = l;
return super.area() *height;
width = w;
}
void setDimensions(int l) { }
setDimensions( l, l);
} Cuboid(int l, int w, int h){
Rectangle (int l, int w){ super(l,w);
setDimensions(l,w); height = h;
} }
Rectangle(int l){
Cuboid(int l){
this(l,l);
this(l,l,l);
}
}
}
int area(){ // overriding method
return super.length *height;
}
class TestCuboid{
public static void main(String args[]) {
Rectangle r1,r2;
Cuboid c1,c2;
}
}
class TestCuboid{
public static void main(String args[]) {
Rectangle r1,r2;
Cuboid c1,c2;
}
}
• TestCuboid.java
Why?
• System.out.println("area of r2 is : " +
r2.area()); // area () of cuboid is called?
in java method binding is done at the runtime
So the method invocation is determined at the
runtime by the instance in which a method is
being invoked.
Since r2 refers to an instance of Cuboid the
method used will be of cuboid
Find Output
class Super {
int x;
Super(int x) {
class Temp {
this.x=x; public static void main(String[] args)
} {
void display() { Sub s1=new Sub(1,2);
System.out.println("Super x s1.display();
="+x);
}
}
}
}
class Sub extends Super {
int y;
Sub(int x, int y) {
super(x);
this.y=y;
}
void display() {
System.out.println("Sub x
="+x);
System.out.println("Sub y
="+y);
}
Using final with VARIABLES,METHODS
and CLASSES
Final
means
CONSTANT Can not be modified once
declared
Final with static variable
Prevent Override
Final with class
Prevent Inheritance
Types of inheritance
• Java supports the following four types of
inheritance:
• Single Inheritance
• Multi-level Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
Single Inheritance
// instances
Student student1, student2;
student1 =new UnderGraduate();//widening,valid
student2 =new Graduate();//widening,valid
Graduate student3;
student3 = student2;//compilation error
student3 = (Graduate)student2;//explicit cast //done,valid
• public class Test {
• public static void main(String[] args)
• {
Test t= new Test();
System.out.println(t instanceof Test); }
}
• true
Find output : Example
class Parent{}
class Child1 extends Parent{}
class Child2 extends Parent{}
class Test { public static void main(String[] args) {
Parent p =new Parent();
Child1 c1 = new Child1();
Child2 c2 = new Child2();
System.out.println(c1 instanceof Parent); //true
System.out.println(c2 instanceof Parent); //true
System.out.println(p instanceof Child1); //false
System.out.println(p instanceof Child2); //false
p = c1;
System.out.println(p instanceof Child1); //true
System.out.println(p instanceof Child2); //false
p = c2;
System.out.println(p instanceof Child1); //false
System.out.println(p instanceof Child2); //true
}}
Other operators
• New : create new instance
• [] : access the elements of an arrary
• . : access any member of an instance or
invoking methods on an instance
Thank you