Inheritance program in Java
- Oct 31, 2017
- 1 min read
source code:
import java.util.Scanner;
class father{ String name; int age; void get(){ Scanner sc=new Scanner(System.in); System.out.print("Enter Father's Name: "); name=sc.nextLine(); System.out.print("Enter Father's age: "); age=sc.nextInt(); } void show(){ System.out.println("Father: "+name); System.out.println("Age: "+age); } } class son extends father{ String name; int age; void get(){ super.get(); Scanner sc=new Scanner(System.in); System.out.print("Enter Son's Name: "); name=sc.nextLine(); System.out.print("Enter Son's age: "); age=sc.nextInt(); } void show(){ super.show(); System.out.println("Son: "+name); System.out.println("Age: "+age); } } class inheritance{ public static void main(String[] args) { System.out.println("\n===========Inheritance=========\n"); son s=new son(); s.get(); father f=new father(); System.out.println("\nCalling Parent class Method. . "); f.get(); System.out.println("\nCalling Parent class show() Method. . \n"); f.show(); System.out.println("\nCalling Subclass show() Method. . \n"); s.show();
} }
Output:








Comments