java 继承和抽象类之间的确切区别是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40626800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:21:25  来源:igfitidea点击:

What is exact difference between Inheritance and Abstract class?

javaoopinheritanceabstract-classabstraction

提问by Pratik Patel

I know the fundamentals of OOP concepts[Inheritance, Abstraction, Encapsulation, Polymorphism]

我知道OOP概念的基础[继承、抽象、封装、多态]

We use Inheritancein case of Parent-Child relationship[Child can have all functionalities which Parent have and can add more functionality to itself too]

我们在父子关系的情况下使用继承[子可以拥有父拥有的所有功能,也可以为自己添加更多功能]

And we use Abstract class(In java) for a partial set of default implementations of methods in a class, which also can be implemented by simple Inheritance.

并且我们使用抽象类(在java中)作为类中方法的部分默认实现,也可以通过简单的继承来实现。

Look below example which makes my point clear.

看下面的例子,这使我的观点清楚。

Inheritance:

遗产:

Parent class

父类

public class Parent {

    // This method will remain same for all child classes.No need to override
    public void abc() {
        System.out.println("Parent here");
    }

    // This methods need to be overridden from child class
    public int getROI() {
        return 0;
    }
}

Child class

儿童班

public class Child extends Parent{

    @Override
    public int getROI(){
        return 5;
    }

    public static void main(String[] args) {
        Child child =new Child();
        child.abc();
        System.out.println(child.getROI());
    }
}

Abstract Class:

抽象类:

Parent class

父类

abstract class Parent {

    // This method will remain same for all child classes.No need to override
    public void abc() {
        System.out.println("Parent here");
    }

    // This methods need to be implemented from child class
    public abstract int getROI();
}

Child class

儿童班

public class Child extends Parent{

    public int getROI(){
        return 5;
    }

    public static void main(String[] args) {
        Child child =new Child();
        child.abc();
        System.out.println(child.getROI());
    }
}

For above programs o/p will be same.

对于上述程序,o/p 将相同。

O/P:    
Parent here
5

So I think,

所以我认为,

Inheritance:We need to override the method in child class

继承:我们需要覆盖子类中的方法

Abstract class:Put abstract keyword in method name and need to implement the method in child class

抽象类:方法名中放置abstract关键字,需要在子类中实现该方法

So Inheritance and abstract class is same regardless of abstract keyword

所以无论抽象关键字如何,继承和抽象类都是相同

So we can implement abstract class using inheritance, here just method signature change classes(That's my belief).

所以我们可以使用继承来实现抽象类,这里只是方法签名更改类(这是我的信念)。

Is there any significant difference?

有什么显着差异吗?

回答by prem kumar

Inheritanceis for inheriting properties and having some of its own as well.

继承是为了继承属性并拥有一些自己的属性。

Abstractis to restrict from being instantiated.

Abstract是限制被实例化。

Example:
Lets take Vehicle and VehiclePart. But Vehicle as such is very abstract and not complete. So we want Vehicle class abstract because we don't want to instantiate it directly. Car is more meaningful entity than Vehicle and car is a Vehicle. So car extends vehicle and it is not abstract.

示例:
让我们以 Vehicle 和 VehiclePart为例。但是像这样的 Vehicle 非常抽象且不完整。所以我们想要 Vehicle 类抽象,因为我们不想直接实例化它。Car是比Vehicle更有意义的实体,car是Vehicle。所以汽车是汽车的延伸,它不是抽象的。

abstract class Vehicle{
    String name;
}

abstract class VehiclePart{
    String name;
    Date expiry;
}

class Car extends Vehicle{
     List<VehicleParts> parts;
}

class RacingCar extends Vehicle{

}

class Gear extends VehiclePart{
   int numOfGears;
}

Inheritance: We need to override the method in child class

继承:我们需要覆盖子类中的方法

Nope. in the above example you can see Car is inheriting properties like name from Vehicle. Overriding is optional. Like RacingCar can override methods of Car and make it a little bit custom. But basically it is getting(inheriting) some properties from base class. Like all the basic properties of a car will in Car and not in RacingCar. RacingCar will have properties specific to it.

不。在上面的示例中,您可以看到 Car 继承了 Vehicle 的 name 等属性。覆盖是可选的。像 RacingCar 一样可以覆盖 Car 的方法并使它有点自定义。但基本上它是从基类获取(继承)一些属性。就像汽车的所有基本属性都将在 Car 中而不是在 RacingCar 中。RacingCar 将具有特定于它的属性。



Abstract class: Put abstract keyword in method name and need to implement the method in child class

抽象类:方法名中放置abstract关键字,需要在子类中实现该方法

Nope. It is just to restrict its instantiation. Eg. We don't want to instantiate Vehicle object because there is no meaning to it. A vehicle has to be something like car, bus etc etc. It can't just be a vehicle. So we put abstract and restrict instantiation.

不。只是为了限制它的实例化。例如。我们不想实例化 Vehicle 对象,因为它没有意义。交通工具必须是汽车、公共汽车等。它不能只是交通工具。所以我们把抽象和限制实例化。

回答by AleSod

With inheritance you don't need to override a method. Without overriding getROIin Childyou could still call new Child().getROI()and get 0as response.

使用继承,您不需要覆盖方法。如果没有覆盖getROIChild您仍然可以调用new Child().getROI()并获得0响应。

If on the other hand a method is abstract, it will need to be implemented by the child as there is no default implementation.

另一方面,如果方法是抽象的,则需要由子项实现,因为没有默认实现。

回答by Marius

An abstract class means you can't instantiate it directly.

抽象类意味着您不能直接实例化它。

new Parent()

is not allowed.

不被允许。

An abstract method will need to be implemented in an extended class.

抽象方法需要在扩展类中实现。