Lecture 5 Inheritance Constructor Chaining Access Modifiers and Object Class
Lecture 5 Inheritance Constructor Chaining Access Modifiers and Object Class
✓ Inheritance
✓ Constructor Chaining
✓ super Keyword
Different classes may have some common properties and behaviors, which can
be generalized in a class that can be shared by other classes. Inheritance enables
you to define a general class and later extend it to more specialized classes.
Inheritance is
✓ Every class in Java is inherited from an existing class either explicitly (extends)
or implicitly (Object class)
✓ is a relationship
Inheritance
✓ A class C1 extended from another class C2, is called sub class and C2
is called super class
✓ Super class or base class or parent class
✓ Sub class or child class or derived class or extended class
class SuperClass { class SubClass extends SuperClass{
int i=10; int k=20;
void showi() { void showk() {
System.out.println("i " + i); System.out.println(" k " + k);
} }
} }
SuperClass SubClass
Class Members
Class Members
k and showk()
i and showi() Inherited Members
i and showi()
Inheritance
✓ No
✓ They are invoked explicitly (Super keyword) or implicitly (constructor
chaining)
Constructor Chaining
class A{
public A(){
System.out.println("In A");
}
}
class B extends A{
public B(){
System.out.println("In B");
}
}
class C extends B{
public C(){
System.out.println("In C");
}
}
public class CC{
public static void main(String[] a){
C ob = new C();
}
}
Super keyword
✓ The keyword super refers to the superclass of the class in which super appears.
✓ This keyword can be used in two ways:
(1) To call a superclass constructor
(2) To call a superclass method
statement that uses the keyword super appear first in the constructor
Super keyword
SuperClass SubClass
Class Members
Class Members
i,k, showi() and showk()
i and showi() Inherited Members
i and showi()
Variables and Method Overriding
• A subclass extending the parent class has access to all the non-private data members and methods its parent
class.
• Most of the time the purpose of inheriting properties from the parent class and adding new methods is to
extend the behaviour of the parent class.
• However, sometimes, it is required to modify the behaviour of parent class. To modify the behaviour of the
parent class overriding is used.
Some important points that must be taken care while overriding a method:
i. An overriding method (largely) replaces the method it overrides.
ii. Each method in a parent class can be overridden at most once in any one of the subclass.
iii. Overriding methods must have exactly the same argument lists, both in type and in order.
iv. An overriding method must have exactly the same return type as the method it overrides.
v. Overriding is associated with inheritance.
Method Overriding
class Figure class Rectangle extends Figure
{ {
double sidea; Rectangle( double a , double b)
double sideb; {
Figure(double a, double b) super ( a, b);
{ }
sidea = a; double area ( )
sideb = b; {
} System.out.println("The Area of Rectangle:");
Figure(double a) return sidea*sideb;
{ }
sidea = a; }
sideb = a; class Squre extends Figure
} {
double area( ) Squre( double a )
{ {
System.out.println("Area inside figure is Undefined.");
return 0;
}
}
Method Overriding
Squre s = new Squre( 23.1);
super (a);
System.out.println("***** Welcome to
} Override Demo ******");
double area( ) f.area();
{ System.out.println(" "+r.area());
System.out.println("Area of Squre: "); System.out.println(" "+s.area());
return sidea*sidea; }
} }
} Output:
class Area_Overrid ***** Welcome to Override Demo ******
{ Area inside figure is Undefined.
public static void main(String[] args) The Area of Rectangle:
{ 1925.46
Figure f = new Figure(20.9, 67.9); Area of Squre:
Rectangle r = new Rectangle( 34.2, 56.3); 533.61
Object Class and its Methods
✓ Default implementation checks whether two reference variables point to the same
object
public Boolean equals(Object o){
return( this == o)
}
✓ Method can be over ridden
Access Modifiers
public
protected -
default - -
private - - -
Access Modifiers
Package 1 Package 2
Class 1
public a
Private b All are declared
Class 4
default c here and are
Protected d accessible here
Only a is accessible here through
Class 2
object of Class 1
Only a,c and d are accessible here Class 5 extends class 1 of package 1
through object of Class 1