Program to demonstrate Single Inheritance

Inheritance is the property in which objects of one class acquires the properties of other class. This is the one of feature of Java known as Reusability. With the use of inheritance, the information is made manageable in a hierarchical order. A class that is inherited is called a superclass. The class that does the inheriting is called subclass. It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements. There are following types of Inheritance:
  1. Single Inheritance (only one superclass and subclass)
  2. Multiple Inheritance (several super classes for a subclass)
  3. Hierarchical Inheritance (one super class with many subclasses)
  4. Multilevel Inheritance (subclass is super class of another class)
  5. Hybrid Inheritance (combination of above four types)
Java supports the first three types of inheritance. Java does not support Multiple inheritance and hybrid inheritance. Java does not directly implement the multiple inheritance. You can achieve this by making use of the new concept in Java called Interface. The syntax of single inheritance is given below:
class <superclass-name> {
    // Body of superclass
}

class <subclass-name> extends <superclass-name> {
 // Body of subclass
}

This a program for the single inheritance, in that AA is a superclass which has one variable i and one method showI( ). Next the class AA is get inherited in class BB which also have one variable j and one method showJ( ). Now class BB have variable i, method showI( ) which is inherited from parent of BB that is from class AA and its own data member and method. Now in SingleInhDemo class we have created an object AA and BB classes, initialized the data members and called the methods of classes.

Note that after compiling the SingleInhDemo.java file the three .class files will be created namely "AA.class", "BB.class" and "SingleInhDemo.class".


PROGRAM
class AA {
 
 int i;
 
 void showI() {
  
  System.out.println("i = "+i);
 }
}

class BB extends AA {

 int j;
 
 void showJ() {
  
  System.out.println("j = "+j);
 }
}

public class SingleInhDemo {

 public static void main(String[] args) {
  
  BB b = new BB();    // Creating object of subclass
  
  b.i = 56;      // Assign value to i of AA class
  b.j = 23;      // Assign value to j of BB class
  
  b.showI();     // Call method showI( ) of AA class
  b.showJ();     // Call method showJ( ) of BB class
 }
}
OUTPUT
C:\>javac SingleInhDemo.java
C:\>java SingleInhDemo
i = 56
j = 23

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative