What is inheritance?
- It is a core mechanism in object-oriented programming ,
- A object of one class() is acting as an object of another class.
A class is drived from the another class is called subclass(drived class / extended class / chlid class),The class from which subclass is drived is called as superclass(base class or parent class).
Superclass (Parent Class): The class whose properties and methods are inherited by another class.
Subclass (Child Class): The class that inherits properties and methods from another class.
extends: The Java keyword used to establish an inheritance relationship.
super: A keyword used inside a subclass to invoke the parent class's constructors, methods, or variables.
Java Support three types of inheritances
- Single Inheritance
- Multilevel Inheritance
- Heirarchical Inheritance
1) Single inheritance
- A subclass is derived only-one superclass,It inherits the properties and and behaviour of the superclass.
public class Vehicle{ #superClass } public class Bus extends Vehicle{ #subClass } 2) Multilevel Inheritance
- The derived class will be inheriting a base class and as well as derived class also acts as the base class for another class
public class GrandFather{ } public class Father extends GrandFather{ } public class Child extends Father{ } 3) Heirarchical Inheritance
- More than one subClass is created from one superClass is called as Heirarchical inheritance.
public class Father { } public class Son extends Father{ } public class Daughter extends Father{ } what is polymorphism?
Polymorphism is the fundamental concept of the object-oriented programming that allows a single entity to take multipe form(methods).It enables same method which behaves differently depending on object involved.
Polymorphism improves code flexibility, reusability, and maintainability.
- Polymorphism means "many forms".
- It allows the same method to exhibit different behaviors.
- It is achieved through method overloading and method overriding.
Types of Polymorphism
- Complie-time polymorphism ---> method overloading.
- Run-time polymorphism ---> Method overriding.
1) Complie-time polymorphism
It is the static polymorphsim, occurs when the method is executed id determined during the compilation. Achieved by method overloading.
- method selection is occured at complie time.
- Improve readability and flexibility.
Method overloading
Same method name with different type of arguments and no of arguments differs.
public class Calculator{ public static void main(String[] args){ Calculator myCal = new Calculator(); myCal.add(10,20); myCal.add(10,30,40); myCal.add(10); myCal.add(); } public int add(int no1 , int no2){ System.out,println(no1 + no2); } public int add(int no1 , int no2 , int no3){ System.out,println(no1 + no2 + no3); } public int add(int no1){ System.out,println(no1); } public int add(){ System.out,println(no arguments); } 2) Run-time polymorphism
It is also known as dynamic polymorphism , occurs the method calling is done during the program execution.It achieved in method overriding.
- Method selection occurs at runtime.
- Depends on the actual object type.
Method overriding
It occurs only in the inheritance , where the subclass provide its own implementation method or properties already defined in the superclass.
Note : Every subClass or superClass has an default parent class(base class/superclass) as object
Key charaterics:
- Same method name
- same no of arguments
- same return type
- same access modifider
# class parent public class Parent { public void display(){ } } #class Child public class Child extends Parent{ public static void main(String[] args){ Child newChild = new Child(); newChild.display() } #1st preference for that class then serach in extends class public void display(){ } } 


