Program to Demonstrate Inheritance in Java
Instructions: 1.)Save this program to a file and name it Inheritance.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import javax.swing.JOptionPane; class SuperClass //The Superclass { String str="We Got Copieeed!!!"; void func() { JOptionPane.showMessageDialog(null, str); //Prints the string 'str' in a window. } } class SubClass extends SuperClass //The SubClass inheriting SuperClass { } public class Inheritance { public static void main(String[] args) //Our main { SubClass sub1= new SubClass(); //SubClass Object Initialization. sub1.func(); //Call to function of SuperClass via object of the Subclass } } |
Tested in compilers: Eclipse and Command Line.