Benefits of using an abstract classes vs. regular

2020-05-20 04:03发布

I have decided to start doing small coding projects on my own that focus on code quality instead of code quantity and have a question about the use of abstract classes.

Now I know the differences between abstract classes and interfaces with the biggest one (I think) being that interface allow you to only define methods that need to be implemented by classes using the interface and abstract classes allowing you to define both method and members along with default method implementation if you so desire. My question is what the the main benefit of use an abstract class vs a normal class? The only real difference between the two that I can think of is that you can not create an instance of an abstract class. Are there any other differences between the two?

8条回答
兄弟一词,经得起流年.
2楼-- · 2020-05-20 04:17

In OO world, abstract classes used to impose some design & implementation constraints. Nothing more. You never have to use abstract classes in any case. But there might be cases that you better impose those constraints. So what are them? Let's look at by comparing it's oo-counterparts.

Abstract classes vs interfaces

As you know, these are two of the primary concepts of inheritance.

Basically, interface is used just to declare that you're willing to inherit the underlying service and that's it. Contains no implementation & has no functionality. In that sense, interface is abstract. That's why it's a more a design constraint than an implementation constraint. Think of a headphone jack on a speaker. Each headphone needs to implement the jack interface (with start, stop, listen, turnDown, turnUp methods). Each headphone should override this interface to inherit the functionality that the speaker provides and implement accordingly.

Abstract classes, on the other hand, may include methods with an implementation. That's the basic difference and in that sense it may utilize reusing more than an interface. Moreover, they may contain private, protected & non-static fields which you can't via interfaces. You may force subclasses to implement some must-have functionalities with abstract methods (those without implementations). Abstract classes more agile than interfaces.

Of course not to mention, you may only extend one class in java in where you may implement number of interfaces.

Abstract classes vs regular classes

So why not to use regular classes then. What's the benefit of using abstract class? This is pretty simple. If you use abstract classes, you force the core functionality to be implemented by the siblings. As a developer, you don't need to remember that you should implement the essential functions. This is where abstract classes imposing design constraints over regular classes. Plus by making the class abstract you avoid that (incomplete) class to be created accidently.

查看更多
趁早两清
3楼-- · 2020-05-20 04:20

Strictly from a design perspective, it is best to simplify things. I believe the best way to simplify things is to use a simple analogy. Let's use an analogy of birds...

Interface: use this when you want to enforce certain functions which need to be defined. e.g. IBird has a contract for ScreamLikeABird and Fly (interface functions). But you can get more specific and have an IOstrich that has a Run contract. You may also have an IHawk that has an Attack contract...etc.

Abstract: use this when you want to enforce base functions and have base properties. e.g. Avian could be a base class for birds which may have a function called LayEgg as well as propeties called Age, Species, NumberOfChicks...etc. These things don't/shouldn't change the behavior of a bird, since all birds lay eggs...etc. But not all birds sounds the same when it scream or flies the same way (some dont even fly)....etc.... hence they should be implemented via an interface(s).

查看更多
做自己的国王
4楼-- · 2020-05-20 04:21

in my opinion abstract classes have more use in real projects as on books. some times project managers just provide the methods declaration and you have to write code for the methods without modify the core syntax provided by manager. so that is how an abstract class is use full. in simple class method define,declared and coded in same time but not in abstract classes. for ex:-

abstract class Test
{
    abstract void show();//method provided
}
class Child extends Test
{
    void show()//coding 
    {
        System.out.println("saurav");
    }
}
class main
{
    public static void main(String[] args) 
    {
    Test c = new Child();
    c.show();   
    }
}
查看更多
孤傲高冷的网名
5楼-- · 2020-05-20 04:25

The only reason for declaring a class as abstract is so that it can't be instantiated. There are situations where you will have common functionality that is shared between a number of classes, but by itself that common functionality does not represent an object or represents an incomplete object. In that case, you define the common functionality as abstract so that it can't be instantiated.

查看更多
一夜七次
6楼-- · 2020-05-20 04:26

This might help you, Lets consider traveler who may use any type of vehicle i.e car,cycle,bike etc...
but all vehicles moves in the same way with different speed constraints so we can have one

abstract class Avehicle    
{  
        string fuel;  
        public void move()  
{  
 sysout("moving");  
}   
} 

but all vehicles breaking system is different

interface Ivehicle    
{  

        public void breakorstop();  
}  
class Traveler    
{  
  Ivehicle v; 
//Settrers and getters   
 public drive()  
{  
v.move();  
}  
public break()  
{  
v.breakorstop();  
}  
}

So finally Car or Cycle or Bike classes can extend Avehicle and can Implement Vehicle interface

查看更多
Ridiculous、
7楼-- · 2020-05-20 04:28

In addition to not being able to create instances of abstract classes, some languages may support having abstract methods in abstract classes - similar to interfaces, an abstract method will have to be implemented by the class inheriting from the abstract class.

The main benefit of abstract classes in my opinion is if there is some code that has to be shared between classes of the same type. Usually you could use an interface for this, but sometimes the functionality of such classes may overlap and you would end up with code duplication. In this case you can use an abstract class and just put the code there.

查看更多
登录 后发表回答