Single level inheritance in Java


Single Level inheritance - A class inherits properties from a single class. For example, Class B inherits Class A.

Example

 Live Demo

class Shape {
   public void display() {
      System.out.println("Inside display");
   }
}
class Rectangle extends Shape {
   public void area() {
      System.out.println("Inside area");
   }
}
public class Tester {
   public static void main(String[] arguments) {
      Rectangle rect = new Rectangle();
      rect.display();
      rect.area();
   }
}

Output

Inside display
Inside area

Here Rectangle class inherits Shape class and can execute two methods, display() and area() as shown.

Updated on: 30-Jul-2019

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements