继承和抽象类之间的确切区别是什么? [英] What is exact difference between Inheritance and Abstract class?

查看:648
本文介绍了继承和抽象类之间的确切区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解OOP概念的基础知识[继承,抽象,封装,多态]

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

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

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

我们将 Abstract class (在Java中)用于类中方法的部分默认实现集,也可以通过简单继承来实现.

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.

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

Look below example which makes my point clear.

继承:

父类

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

子类

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 {

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

子类

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

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

For above programs o/p will be same.

O/P:    
Parent here
5

所以我认为

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

Inheritance: We need to override the method in child class

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

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

因此,继承和抽象类是相同的,而与抽象关键字无关

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).

有什么显着差异吗?

推荐答案

继承用于继承属性并具有一些自己的属性.

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

摘要是为了限制实例化.

Abstract is to restrict from being instantiated.

示例:
让我们接受Vehicle和VehiclePart.但是,车辆本身是非常抽象且不完整的.所以我们想要Vehicle类抽象,因为我们不想直接实例化它.汽车是比汽车更有意义的实体,汽车是汽车.所以汽车扩展了车辆,它不是抽象的.

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.

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

不.在上面的示例中,您可以看到Car正在从Vehicle继承名称之类的属性.覆盖是可选的.像RacingCar一样,可以重写Car的方法并使其有点自定义.但基本上,它是从基类获取(继承)一些属性.就像汽车的所有基本属性一样,汽车将具有这种特性,而不是RacingCar. RacingCar将具有特定于它的属性.

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.


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

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

不.只是为了限制其实例化.例如.我们不想实例化Vehicle对象,因为它没有任何意义.车辆必须是诸如汽车,公共汽车等之类的东西.它不能只是车辆.因此我们放置了抽象并限制了实例化.

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.

这篇关于继承和抽象类之间的确切区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆