Single Inheritance Example

This program demonstrates single inheritance.

Problem Statement

Write a Java program to show single inheritance between Person and Student.

Source Code

1 class Person {
2 String name;
3 void show(){ System.out.println("Name: "+name); }
4 }
5 class Student extends Person {
6 int age;
7 void display(){ System.out.println("Age: "+age); }
8 }
9 public class SingleInheritance {
10 public static void main(String[] args) {
11 System.out.println("Eduinq Single Inheritance");
12 Student s=new Student();
13 s.name="Arun"; s.age=20;
14 s.show(); s.display();
15 }
16 }

Program Output

Eduinq Single Inheritance
Name: Arun
Age: 20

Explanation

Subclass inherits attributes and methods from superclass. - Student class extends Person, gaining its properties. - Inheritance promotes code reuse and hierarchical relationships. Conclusion: Shows how to implement single inheritance.

Quick Links to Explore