Unit 3 Inheritance and Interface
Unit 3 Inheritance and Interface
Inheritance Basics:
Inheritance in Java is a concept that acquires the properties from one class to other classes; for
example, the relationship between father and son. Inheritance in Java is a process of acquiring
all the behaviours of a parent object.
subclass (child) - the class that inherits from another class
superclass (parent) - the class being inherited from
Why Do We Need Java Inheritance?
Code Reusability: The code written in the Superclass is common to all subclasses. Child
classes can directly use the parent class code.
Method Overriding: Method Overriding is achievable only through Inheritance. It is one
of the ways by which Java achieves Run Time Polymorphism.
Abstraction: The concept of abstract where we do not have to provide all details is
achieved through inheritance. Abstraction only shows the functionality to the user.
How to Use Inheritance in Java?
The extends keyword is used for inheritance in Java. Using the extends keyword indicates you
are derived from an existing class. In other words, “extends” refers to increased functionality.
Syntax :
class derived-class extends base-class
{
//methods and fields
}
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java: single, multilevel and
hierarchical.
In java programming, multiple and hybrid inheritance is supported through interface only. We
will learn about interfaces later.
Note: Multiple inheritance is not supported in Java through class.
When one class inherits multiple classes, it is known as multiple inheritance. For Example:
Hierarchical Inheritance:
When two or more classes inherits a single class, it is known as hierarchical inheritance. In the
example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical
inheritance.
File: TestInheritance3.java
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark() {System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}
}
Output:
meowing...
eating...
Q) Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in
java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B
classes. If A and B classes have the same method and you call it from child class object, there
will be ambiguity to call the method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-time
error if you inherit 2 classes. So whether you have same method or different, there will be
compile time error.
Overriding in Java:
In Java, Overriding is a feature that allows a subclass or child class to provide a specific
implementation of a method that is already provided by one of its super-classes or parent
classes. When a method in a subclass has the same name, the same parameters or signature,
and the same return type(or sub-type) as a method in its super-class, then the method in the
subclass is said to override the method in the super-class.
Method overriding is one of the ways by which Java achieves Run Time Polymorphism. The
version of a method that is executed will be determined by the object that is used to invoke
it.
Rules for Java Method Overriding
1. The method must have the same name as in the parent class
2. The method must have the same parameter as in the parent class.
3. There must be an IS-A relationship (inheritance).
interface printable{
void print();
}
class A6 implements printable{
public void print(){
System.out.println("Hello");
}