Java Inheritance
Java Inheritance
Java Lecture-29
Topic: Inheritance
Inheritance :
Creating a new class from existing class is referred as Inheritance. OR
When one class acquires the properties of another class then it is referred
as inheritance.
Existing class from which new class is created is referred as super class.
Syntax of Inheritance :
Base Class
Super class
Derived Class
Sub Class
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Types of Inheritance
There are three types of Inheritance in Java
(1) Single Inheritance
(2) Multi-level Inheritance
(3) Hierarchical Inheritance
Note : Java does not support multiple and hybrid inheritance. But this
concept is implemented by using an interface.
1) Single Inheritance :
If there is only one subclass and only one super class then it is referred
as single inheritance
Super class
Sub Class
void setX(int a)
{
x=a;
}
}
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
class B extends A
{
int y;
void setY(int a)
{
y=a;
}
void display()
{
System.out.println("x="+x);
System.out.println("y="+y);
}
public static void main(String [] args)
{
B b = new B();
b.setX(10);
b.setY(20);
b.display();
}
}
Output: x=10
y=20
2) Multi-level Inheritance
When a class extends a class, which extends another class then this is
called multilevel inheritance. For example class C extends class B and
class B extends class A then this type of inheritance is known as
multilevel inheritance.
A
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
3) Hierarchical Inheritance :
It two or more classes are derived from same base class, then it is referred as
hierarchical inheritance.
B C
class B extends A class C extends A
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Java Lecture-30
Topic: ‘super’ keyword
Use of Super :
Whenever a subclass needs to refer to its immediate super class, it can do so
by use of the keyword "super".
There are two uses of super
1) Using super() method to call a super class constructor.
2) Using super keyword to access the hidden member of super class in
subclass.
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
int age;
Person(String s, int a)
{
name=s;
age=a;
}
}
class Employee extends Person
{
double salary;
Employee(String s, int a, double sal)
{
super(s,a); // calls super class constructor
salary=sal;
}
void display()
{
System.out.println("Emp_Name : "+name);
System.out.println("Emp_Age : "+age);
System.out.println("Emp_Salary: "+salary);
}
}
class SuperTest
{
public static void main(String [] args)
{
Employee e = new Employee("Atul",45,50000.0);
e.display();
}
}
Output:
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Java Lecture-31
Topic: Method Overloading and Method Overriding
Method Overloading :
Overloading occurs if several methods have the same name but different
parameters.
The compiler call the correct method by matching the parameter types of
the various methods with the types of values used in the specific method
call.
A Compile Time error occurs if the compiler cannot match the parameter.
Example:
class sample
{
int max (int a, int b)
{
if (a > b)
return (a);
else
return (b);
}
double max (double a, double b)
{
if (a>b)
return (a);
else
return (b);
}
public static void main(String [] args)
{
Sample s = new Sample ( );
System.out.println (“Maximum between 10 & 20 is” + s.max (10, 20));
System.out.println (“Maximum between 3.4 and 1.7 is” + s.Max (3.4, 1.7));
}
}
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Method Overriding :
In Inheritance, when a method in a subclass has the same name and
same parameters list as a method in it super class, then the method in
the subclass is said to override the method in the super class.
Example :
class A
{
void display ( )
{
System.out.println(“From A class display”) ;
}
}
class B extends A
{
void display ( )
{
System.out.println (“From B class display”);
}
public static void main(String [] args)
{
B b = new B ( );
b.display ( ) ;
}
}
Output: From B class display.
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Java Lecture-32
Topic: Dynamic Method Dispatch
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
class DMDTest
{
public static void main(String [] args)
{
A ref ; //super class reference
ref = new A(); //refers to A class Object
ref. display (); //calls A class display()
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Java Lecture-33
Topic: Abstract Method And Abstract Class
Abstract Method :
In Inheritance, we have some methods which must be overridden by the
subclass.
In this case, we want some way to ensure that a subclass must override all
necessary methods.
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Abstract Class :
(i) Any class that contains one or more abstract method must also be
declared as an abstract.
(iii) We can't create an object of an abstract class but we can create the
reference.
Shape(double a, double b)
{
dim1=a;
dim2=b;
}
abstract void area();
}
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
class AbstractTest
{
public static void main(String [] args)
{
Shape s; //s is reference
//s=new Shape(10,10); Error, can't create object of abstract class
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Java Lecture-34
Topic: final keyword
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
System.out.println ("illegal");
}
}
3) To prevent Inheritance :
Sometimes we want to prevent a class from being inherited. To do this
precedes the class declaration with keyword final. Declaring a class as
final, implicitly declares all of its method as final too.
For example :
final class A
{ ----
----
----
}
class B extends A // error can't subclass A
{ ----
----
----
}
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Java Lecture-35
Topic: Access Specifiers in Java
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260