Computer >> Máy Tính >  >> Lập trình >> Java

Kế thừa mức đơn trong Java

Kế thừa mức đơn - Một lớp kế thừa các thuộc tính từ một lớp duy nhất. Ví dụ:Lớp B kế thừa Lớp A.

Ví dụ

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();
   }
}

Đầu ra

Inside display
Inside area

Ở đây lớp Rectangle kế thừa lớp Shape và có thể thực thi hai phương thức là display () và area () như hình.