What is the main advantage of creating an abstract class

Why do we declare a class abstract? I know that it cannot be created, but why give it a special keyword. Even a “normal” class will work just as well and can be easily subclassed. so what is the main advantage of creating an abstract class?

+3
source share
9 answers

In an abstract class you can implement some method and you can make some referrals, all your clients will also have to implement it. you can provide some common functions, also you can have some inherited fields and some of the skeleton methods here

+6
source

, : , .

, , , public void methodname(){} - , , . , , , ? , ? - .

, . , , , , .

+3

- .

, .

( ) , .

, .

+2

"" .

"" , , () . ( ) . ( ).

savety! - , , , ().

, , , () , .

, , , .

+2

. GUI, . AbstractSuperClass. , AbstractSuperClass, . , , , AbstractSuperClass, . , , GUI . , .

, API , . AbstractSuperClass .

+2

, , .   .

.

, :

  • .

  • ( work())

, , , , , , - work().

0

, , , . , .

, . , , . Google, :) .

0

, ( ).

public abstract class Shape {
    public double calculateArea();
}

public class Square : Shape {
    private double side;

    double calculateArea() {
        return side*side;
    }
}

public class Circle: Shape {
    private double radius;

    double calculateArea() {
        return 3.1415 * radius * radius;
    }
}

public class MainClass() {
    public static void Main() {
        Shape myShape = new Square();
        system.out.print(myShape.calculateArea());
        myShape = new Circle();
    }
}

Shape, , . Shape, ( , ).

0

, , , , , , .

, , , . : Java . , , . , , , .

To summarize, you usually want to define the behavior (what the objects will do) with interfaces, and not in abstract classes. In my opinion, the main advantage for abstract classes is that the focus from the use case refers to the implementation hierarchy and reuse of code from subclasses.

0
source

Source: https://habr.com/ru/post/1785020/


All Articles