Inheritance Example Program in Java

By | December 17, 2010
Inheritance Example Program in Java. You can find inheritance example program (code snippets) in this page.

Inheritance Example Program in Java. You can find inheritance example program (code snippets) in this page.

class Base{

private int num;

public Base(){

num = 100;

System.out.println(“\n\tDefault constructor of Base class.”);

}

public void displayNum(){

System.out.println(“\n\tValue of num: ” + num);

}

}

class Derived extends Base{

private int data;

public Derived(){

data = 200;

System.out.println(“\n\tDefault constructor of Derived class.”);

}

public void displayData(){

System.out.println(“\n\tValue of data: ” + data);

}

}

public class InheritanceDemo{

public static void main(String args[])

{

Derived ob = new Derived();

ob.displayNum();

ob.displayData();

}

}

Result:

Default constructor of Base class.

Default constructor of Derived class.

Value of num: 100

Value of data: 200

Leave a Reply

Your email address will not be published. Required fields are marked *