All about java Inheritance types why options example

Inheritance:

The concept of reutilazation is called as inheritance.
Inheritance is used to eliminate the reduandancy of program.


Note : Keyword "extends" is used to inherit one class into another class.

The class which gives it's properties to another class is called as super class or base class.
The class which takes properties from other class is called as sub-class or derived class.

Syntax:
class B extends A
{
-----
-----
}

Types of inheritance

1] Single inheritance :

The one to one communication between super and sub class, this concept is called as single inheritance.

/*Example of simple inheritance */
class A
{
 void fun1()
 {
  System.out.println("Hello this is Super class method");
 }
}
class B extends A
{
 void fun2()
 {
  System.out.println("Hello this is Sub class method");
 }
 
 public static void main(String args[])
 {
  A a1=new A();
  B b1=new B();
  
  a1.fun1();
  b1.fun2();
  b1.fun1();
 }
}


2] Multilevel inheritance :

To inherit more than one classes in chain format with a finite termination, this concept is called as multilevel inheritance.

Tip [Rule] message : One java class can extends only one java class.
According to this rule, Java doesn't follow multiple, Multi-path and Hybrid inheritance.

Note : Class object is the super must built in class of java hierarchy. Every built in and user defined classes should inherit the object class. This class provides the context creation, constructor calling, data overloading and execution properties to the object creation statement.


JAVA Does not Follow The Cyclic Inheritance.

Constructor are executed in the manner they are defined in inheritance


Write a program to define the constructor execution inheritance.

class A extends object
{
 A()
 {
  System.out.println("Class A Constructor");
 }
}
class B extends A
{
 B()
 {
  System.out.println("Class B Constructor");
 }
}
class C extends B
{
 C()
 {
  System.out.println("Class C Constructor");
 }
}
class D extends C
{
 D()
 {
  System.out.println("Class D Constructor");
 }
 
 public static void main(String args[])
 {
 D d1=new D();
 C c1=new C();
 B b1=new B();
 A a1=new A();
 }
}



Super


Note : Kayword 'super' is used to call the super class parameterized constructor from sub class constructor.

"Super" should be the first statement of constructor body.

Write a program to call the super class default and parameterized constructor from sub class.

class A
{
 A()
 {
  System.out.println("Class A constructor");
 }
 A(int a)
 {
  System.out.println("A is "+a);
 }
}
class B extends A
{
 B()
 {
  System.out.println("Class B constructor");
 }
 B(int b)
 {
  System.out.println("B is "+b);
 }
}
class C extends B
{
 C()
 {
  System.out.println("Class C constructor");
 }
 C(int c)
 {
  System.out.println("C is "+c);
 }
 
 public static void main(String args[])
 {
 C a1=new C();
 }
}


Java program execution criteria :


JVM loads all the static data of class into memory. At last it loads the main method. But executes the main() method first to all main() method creates objects.
Object's call the constructor creates context for remaining non-static data of class. It loads this data into newly created context and assign the control to the object.

Method overriding :


To define the same name method with same signature and same arguments in super class as well as in sub class, this concept is called as method overloading.

Note : Keyword "super" is used to call the super class overrided method from sub class method.

Write a program to define methods overriding.

class P
{
 void fun1()
 {
  System.out.println("Hello super class method");
 }
}
class Q extends P
{
 void fun1()
 {
  Super.fun1();
  System.out.println("Hello Sub class method");
 }
 public static void main(String args[])
 {
  Q q1=new Q();
  q1.fun1();
 }
}
Axact

Narendra

Hello, everyone! I am Narendra Bagul founder of Javanotes9. First of all thank you for visiting my blog. Hope you all like my java notes as well. I'm young part time blogger and a Computer Engineering student. I started blogging as a hobby. Now here I'm sharing my little acquired knowledge about my favorite programming language JAVA.

Post A Comment: