Multilevel Inheritance:
In the below example their are total three classes each have their own methods and are giving different output . We all know that in some or the other way properties if our parents came in their child taking the same concept the program is taking the properties of their parent class and and also have their own unique properties . We can inherit the parent class in child class my using "extends" keyword .
Example:
class GrandParents
{
public static void Age(int age)
{
System.out.println("Age of GrandParents is:"+" "+age);
}
}
class Parents extends GrandParents
{
public static void Nationality(String citizenShip)
{
System.out.println("Parents are from:"+" "+citizenShip);
}
}
class Child extends Parents
{
public static void Gender(String gender)
{
System.out.println("Child is a"+" "+gender);
}
}
public class Main
{
public static void main(String[] args)
{
Child. Age(52);
Child. Nationality("India");
Child. Gender("Male");
}
}
Comments
Post a Comment