We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
You are on page 1/ 11
welcome
PROGRAM
FOR
INHERITANCE INHERITANCE
The process of obtaining data members
and methods from one class to another class is known as inheritance. Let us see Inheritance with a simple example //Create a superclass
class A { int i, j;
void showij() { System.out.println("i and j: " + i + " " + j); } } //Create a subclass by extending class A
class B extends A { int k;
void showk() { System.out.println("k: " + k);
} void sum() { System.out.println("i+j+k: " + (i+j+k)); } } class SimpleInheritance { public static void main(String args []) { A superOb = new A(); B subOb = new B(); // The superclass may be used by itself. superOb.i = 10; superOb.j = 20; System.out.println("Contents of superOb: "); superOb.showij(); System.out.println(); /* The subclass has access to all public members of its superclass. */ subOb.i = 7; subOb.j = 8; subOb.k = 9; System.out.println("Contents of subOb: "); subOb.showij(); subOb.showk(); System.out.println(); subOb.showij(); subOb.showk(); System.out.println(); system.out.println("Sum of i, j and k in subOb:"); subOb.sum(); } } OUTPUT: Contents of superOb: i and j: 10 20 Contents of subOb: i and j: 7 8 k: 9 Sum of i, j and k in subOb: i+j+k: 24 Thank you