Inheritance and Polymorphism in Java
Inheritance in Java
- Inheritance in Java is a mechanism where one object acquires the properties and behaviors of another object (parent object).
- It represents the IS-A relationship, also known as the parent-child relationship.
- Inheritance is the process where one class acquires the properties (methods and fields) of another.
- It makes information manageable in a hierarchical order.
- The subclass (derived class or child class) inherits properties from the superclass (base class or parent class).
- Inheritance allows creating new classes based on existing ones, reusing methods and fields, and adding new ones.
Advantages of Inheritance
- Method Overriding: Enables runtime polymorphism.
- Code Reusability: Allows programmers to reuse superclass code without rewriting it when creating subclasses.
Syntax of Java Inheritance
class Subclass-name extends Superclass-name {
// methods and fields
}
- The
extendskeyword indicates a new class derives from an existing class. - "Extends" means to increase the functionality.
- The inherited class is the parent or super class, and the new class is the child or subclass.
Types of Inheritance in Java
Based on class, there are three types: single, multilevel, and hierarchical.
Multiple and hybrid inheritance are supported through interfaces only.
Single Inheritance:
- Class B inherits from Class A.
Multilevel Inheritance:
- Class C inherits from Class B, which inherits from Class A.
Hierarchical Inheritance:
- Both Class B and Class C inherit from Class A.
Multiple Inheritance:
- Class D inherits from both Class A and Class B (supported through interfaces).
Hybrid Inheritance:
- A combination of multiple types of inheritance.
Activity 1: Single Inheritance Example
class Animal{
void eat() {System.out.println("eating...");}
}
class Dog extends Animal{
void bark() {System.out.println("barking...");}
}
class SingleInheritance{
public static void main(String args[]){
Animal a=new Animal();
a.eat(); // valid
Dog d=new Dog();
d.bark(); // valid
d.eat(); // valid
}
}
Output:
eating...
barking...
eating...
Activity 2: Multilevel Inheritance Example
class Animal{
void eat() {System.out.println("eating...");}
}
class Dog extends Animal{
void bark() {System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep() {System.out.println("weeping...");}
}
class MultiLevel{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
Output:
weeping...
barking...
eating...
Activity 3: Hierarchical Inheritance Example
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 Hierarchical{
public static void main(String args[]){
Cat c=new Cat();
c.eat(); // valid
c.meow(); // valid
//c.bark(); //Compile time error
}
}
Output:
eating...
meowing...
Member Access and Inheritance
- A subclass includes all members of its superclass but cannot access private members.
Behavior of Access Modifiers in Java Inheritance
- Private: Not inherited.
- Protected: Inherited, but usage is limited within the package.
- Public: Inherited by all subclasses.
- Default: Inherited within the same package.
- Every class in Java automatically extends the root class
Objectif it doesn't explicitly extend another class.
Multiple Inheritance
Multiple inheritance is not supported in Java through classes; it can be achieved through interfaces.
- Consider classes A, B, and C where C inherits from A and B. If A and B have the same method, calling it from C would cause ambiguity.
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B{ // suppose if it were
public static void main(String args[]){
C obj=new C();
obj.msg(); // Now which msg() method would be invoked?
}
}
Method Overriding
- If a subclass has the same method as declared in the parent class, it's method overriding.
- It's used in runtime polymorphism.
- If a subclass provides a specific implementation of a method provided by a parent class, it's method overriding.
- If a child class is not satisfied with the parent class's method implementation, it can redefine the method in its own way.
- The overriding method is in the child class, and the overridden method is in the parent class.
Rules for Java Method Overriding
- Must be IS-A relationship (inheritance).
- Methods must have the same name and parameters (same method signature).