Single level inheritance in Java

JavaJava Programming Java 8

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.

raja
Published on 02-Feb-2018 12:55:23
Advertisements