java inheritance sample program

Java Inheritance Program:


Super class

package com.java.Inheritanceclass;                               //package

public class SuperClass {
           
            int num;

            public SuperClass( int num) {                       //Constructor 
                        super();
                        System.out.println("Super Class");
                        this.num=num;
                        System.out.println(num);
             
            }

            public int getNum() {
                        return num;
            }           
}

Sub class

package com.java.Inheritanceclass;

public class SubClass extends SuperClass {                //extending super class
          
            int Subnum;
           
            public SubClass(int num1, int num2) {
                        super(num1);
                        System.out.println("Sub Class");
                        Subnum = num2;
            }

            public int getSubnum()
            {
                        return Subnum;
            }

}


Main method class

package com.java.Inheritanceclass;

public class Main {

 public static void main(String[] args) {
      SubClass sub = new SubClass(12, 10);                     //creating object
      System.out.println(sub.getSubnum());
     
            }

}



EmoticonEmoticon