0% found this document useful (0 votes)
14 views

Topic 4 - Inheritance in Java

Uploaded by

n0233305r
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Topic 4 - Inheritance in Java

Uploaded by

n0233305r
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

INHERITANCE IN JAVA

Introduction
• Inheritance in Java Mechanism of deriving new class from old class.
The old class is known as-Base Class / Super class / Parent Class
The new class is known as-Derived class/ Sub Class / Child class
Types:
Single Inheritance
Multilevel Inheritance
Multiple inheritance (Not supported in Java)
Hierarchical Inheritance
Single inheritance
When a class extends another one class only then we call it a single inheritance.
Single Inheritance example program in Java
class A{
public void methodA()
System.out.println("Base class method");
}
class B extends A
public void methodB()
System.out.println("Child class method");
public static void main(String args[])
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
Sub Class Constructor
A subclass constructor is used to construct the instance variables of
both the subclass and the superclass.
The subclass constructor uses the keyword super to invoke the
constructor method of superclass.
The super Keyword is used with following Conditions
Super may only be used within a subclass constructor.
The call to super must appear as first statement.
Parameters in the super call must match with declaration in superclass
Example
class Vehicle{
Vehicle()
{
System.out.println("Vehicle is created");
}
class Bike extends Vehicle{
Bike()
super();//will invoke parent class constructor
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike b=new Bike();
}}
Multiple Inheritance
“Multiple Inheritance” refers to the concept of one class extending (Or
inherits) more than one base class.

The problem with “multiple inheritance” is that the derived class will
have to manage the dependency on two base classes.A B C
Multiple Inheritance
Multiple Inheritance is a concept in object-oriented programming that allows a
class to inherit properties from one or multiple parent classes. The problem arises
when methods with similar signatures exist in both subclasses and super-classes.
The compiler is unable to determine which class method should be called first or
given priority when calling the method.
Multilevel Inheritance
In Java, we can achieve multiple inheritance through the concept of interface.
Example:
interface Dog {
void bark();
}
interface Cat {
void meow();
}
class Animal implements Dog, Cat {
public void bark() {
System.out.println("Dog is barking");
}
public void meow() {
System.out.println("Cat is meowing");
}
}
class Main {
public static void main(String args[]) {
Animal a = new Animal(); a.bark(); a.meow();
}
}
Hierarchical Inheritance
In such kind of inheritance one class is inherited by many sub classes.
Example class B,C and D inherits the same class A.
A is parent class (or base class) of B,C & D.A B C D
Hierarchical Inheritance - example
class A{
void display(){
System.out.println("I am method from class A");
} }
class B extends A{
void print(){
System.out.println("I am method from class B");
} }
class C extends A{
void react(){
System.out.println("I am method from class C");
} }
class D extends A{
void exact(){
System.out.println("I am method from class D");
} }
class TestInheritance{
public static void main(String args[]){
B objB = new B(); objB.print(); objB.display(); C objC = new C(); objC.react(); objC.display(); D objD = new D(); objD.exact();
objD.display();
}
}
Overriding Methods
If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding.
In other words, If subclass provides the specific implementation of the method that
has been provided by one of its parent class, it is known as Method Overriding.
Advantage of Java Method Overriding
provide specific implementation of a method that is already provided by its super
class.
Rules for Method Overriding method
• must have same name as in the parent class method.
• must have same parameter as in the parent class.
• must be IS-A relationship (inheritance).
Overriding Methods
If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding.
In other words, If subclass provides the specific implementation of the method that
has been provided by one of its parent class, it is known as Method Overriding.
Advantage of Java Method Overriding
provide specific implementation of a method that is already provided by its super
class.
Rules for Method Overriding method
• must have same name as in the parent class method.
• must have same parameter as in the parent class.
• must be IS-A relationship (inheritance).
Method Overloading vs Method
Overriding
1) Method overloading is used to increase the readability of the
program.Method overriding is used to provide the specific
implementation of the method that is already provided by its super
class.
2) Method overloading is performed within a class. Method overriding
occurs in two classes that have IS-A relationship.
3) In case of method overloading parameter must be different. In case
of method overriding parameter must be same.

You might also like