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

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

问题描述

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

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

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

看下面的例子,它使我的观点更清楚.

继承:

父类

public class Parent {//对于所有子类,此方法将保持不变.无需覆盖公共无效 abc() {System.out.println("这里是父级");}//这个方法需要从子类中重写公共 int getROI() {返回0;}}

子类

public class Child extends Parent{@覆盖公共 int getROI(){返回 5;}公共静态无效主(字符串 [] args){孩子孩子=新孩子();child.abc();System.out.println(child.getROI());}}

抽象类:

父类

抽象类父{//对于所有子类,此方法将保持不变.无需覆盖公共无效 abc() {System.out.println("这里是父级");}//这个方法需要从子类中实现公共抽象 int getROI();}

子类

public class Child extends Parent{公共 int getROI(){返回 5;}公共静态无效主(字符串 [] args){孩子孩子=新孩子();child.abc();System.out.println(child.getROI());}}

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

O/P:家长在这里5

所以我认为,

继承:我们需要重写子类中的方法

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

所以继承和抽象类是一样的不管abstract关键字

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

有什么显着差异吗?

解决方案

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

Abstract是限制被实例化.

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

抽象类 Vehicle{字符串名称;}抽象类 VehiclePart{字符串名称;到期日期;}类 Car 扩展了 Vehicle{列出<车辆部件>部分;}类 RacingCar 扩展了 Vehicle{}齿轮类扩展了 VehiclePart{int numOfGears;}

<块引用>

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

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


<块引用>

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

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

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]

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.

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:    
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

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?

解决方案

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

Abstract is to restrict from being instantiated.

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

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 class: Put abstract keyword in method name and need to implement the method in child class

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天全站免登陆