#Coding || Java || Inheritance

 

Java Inheritance

 The mechanism of deriving a new class from an old one is called inheritance

⚫ Allows subclasses to inherit all the variables  and methods of their parent classes

⚫ Old Class-> Base Class->Super class-> Parent Class

⚫ New Class-> Subclass->Derived Class-> child clas.


Types of Inheritance



              






Defining a Subclass in Java

  • Inheritance is declared using the "extends" keyword.
  • If inheritance is not defined, the class extends a class called Object
         Class subclassname extends superclassname
   {
      variables declaration
      methods declaration
}


Inheritance Hierarchy

Each  Java class has one (and only one) superclass.
Inheritance creates a class hierarchy
Classes higheer in the hierarchy are more general and more abstract
Classes lower in the hierarchy are more specific .

package com.demo;

public class Parent1 {
static
{
System.out.println("This is ststic block of Parent");
}
static
{
System.out.println("This is ststic block1 of Parent");
}
{
System.out.println("This is non static block of Parent");
}
public Parent1()
{
System.out.println("This is Parent");
}
public static void Demo()
{
System.out.println("Demo Methood");
}
}


public class Child1 {
static
{
System.out.println("This is static of Child");
}
{
System.out.println("This is non static of child");
}
      public Child1()
      {
      System.out.println("This is Child");
      }
}

public class Grandchild {
static
System.out.println("This is Grandchild");
}
}

package com.demo;

public class Client {
public static void main (String[] args)
{
Child1 child1=new Child1();
//p1.Demo();
Parent1 p1=new Parent1();
Grandchild g1=new Grandchild();
}

}

Output:-

This is static of Child
This is non static of child
This is Child
This is ststic block of Parent
This is ststic block1 of Parent
This is non static block of Parent
This is Parent
This is Grandchild




Comments

  1. U r doing good .. please upload more information about this ..I read all the topics which was uploaded by you

    ReplyDelete
  2. Useful material

    ReplyDelete
  3. Very useful content

    ReplyDelete
  4. Keep it up πŸ‘

    ReplyDelete
  5. Very nice and easy way of study 😊😊😊😊😊😊😊

    ReplyDelete
  6. Good contentπŸ‘πŸ‘

    ReplyDelete
  7. Good work . Keep it up,

    ReplyDelete

Post a Comment

Popular posts from this blog

#Constructor

# How to write java program #Structure || Java Program