INHERITANCE AND METHOD OVERRIDING

Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another.

TYPES OF INHERITANCE :

  1.  SINGLE INHERITANCE : When a class extends another one class only then we  call it a single inheritance.single
  2. MULTILEVEL INHERITANCE : Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class.multilevel
  3. HEIRARCHICAL INHERITANCE  : In such kind of inheritance one class is inherited by many sub classes.hierarchical
  4. HYBRID INHERITANCE  : Hybrid inheritance is a combination of Single and Multiple inheritance.hybrid

METHOD OVERRIDING

If subclass  has the same method as declared in the parent class, it is known as method overriding in java. It is used for runtime polymorphism.

Example :


class A{
void run(){System.out.println("A");}
}

class B extends A{
void run(){System.out.println("B");}

public static void main(String args[]){
B obj = new B();
obj.run();
}

Output :


B

Leave a comment