Friday, 1 November 2013

Hierarchical Inheritance

In hierarchical  inheritance, one class is extended by many subclasses. It is one-to-many relationship.
 In the following example there are two child classes that inherit the same base class "Hinh" and in the main class, objects of both the child classes are created that call the respective functions. Both the child functions call the functions defined in their body and the function of the base class.

Program for Hierarchical Inheritance

import java.io.*;
class Hinh
{
void fun_hinh()
{
System.out.println("Function of hinh class");
}
}

class Childone extends Hinh
{
void fun_childone()
{
System.out.println("Function of childone class");
}
}

class Childtwo extends Hinh
{
void fun_childtwo()
{
System.out.println("Function of childtwo class");
}
}

public class Main_class
{
public static void main(String arg[])
{
Childone ch1=new Childone();
ch1.fun_hinh();
ch1.fun_childone();
Childtwo ch2=new Childtwo();
ch2.fun_hinh();
ch2.fun_childtwo();
}
}


Output:


full details in json
Proudly Powered by Blogger.
back to top