#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
- Inheritance is declared using the "extends" keyword.
- If inheritance is not defined, the class extends a class called Object
{
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
U r doing good .. please upload more information about this ..I read all the topics which was uploaded by you
ReplyDeleteUseful material
ReplyDeletenice efforts
ReplyDeleteVety good content
ReplyDeleteVery useful content
ReplyDeleteKeep it up π
ReplyDeleteVery nice and easy way of study πππππππ
ReplyDeleteGood contentππ
ReplyDeleteGood work . Keep it up,
ReplyDelete