简体   繁体   English

当您可以扩展和实例化 class 时,为什么要制作它?

[英]Why to make a class abstract when you can extend and instantiate it?

I am confused.我很困惑。 I got excited when I got to know that abstract basically means the class is hidden and no one can instantiate it.当我知道抽象基本上意味着 class 是隐藏的并且没有人可以实例化它时,我很兴奋。 Cool.凉爽的。 But when I got to know that someone can come and extend to my class and just instantiate it, use all the methods, what is the point of making a class abstract when someone can just steal all your qualities, act like you and sell himself in the market.但是当我知道有人可以来扩展我的 class 并实例化它时,使用所有方法,当有人可以窃取你所有的品质,像你一样行事并在市场。

If anyone could help me with explaining this, I would be grateful.如果有人可以帮助我解释这一点,我将不胜感激。

If you want to prevent others from inheriting from your class make it final .如果您想防止其他人从您的 class 继承,请将其设为final But than its the last of its hierarchy, so it can no longer be inherited and so to be declared abstract (because its obsolete).但是它不是其层次结构的最后一个,因此它不能再被继承,因此不能被声明为abstract的(因为它已经过时了)。 Abstract classes are templates.抽象类是模板。 They inherit properties and functions without implementing them concretely and only serve to build up a hierarchy.它们继承了属性和功能,但没有具体实现它们,仅用于建立层次结构。 Example:例子:

abstract class Vehicle {...}
class Car extends Vehicle {...}
class Bike extends Vehicle {...}

Vehicle descibes common properties but a concret instantiation is too vague.车辆描述了共同的属性,但具体的实例化太模糊了。 We call all types of vehicles 'Vehicle', even if we never have seen one of them purely in nature.我们将所有类型的车辆称为“车辆”,即使我们从未在自然界中看到过其中一种。 It's a thought construct that makes it easier to classify concrete forms like a car or a bike.这是一种思想结构,可以更轻松地对混凝土 forms 进行分类,例如汽车或自行车。

Abstract class basically means hiding the implementation details and showing only functionality to the user . Abstract class 基本上意味着隐藏实现细节并仅向用户显示功能

Abstract class can have following method.摘要 class 可以有以下方法。

  1. Abstract Method抽象方法

  2. Non Abstract Method After Java 8 they can also have Java 8 之后的非抽象方法他们也可以有

  3. static method static 方法

  4. final method最终方法

    Abstract class A{摘要 class A{

    abstract void run();抽象无效运行(); //Abstract Method //抽象方法

    } }

    class B extends A{ class B 扩展 A{

    void run(){ System.out.println("running safely"); void run(){ System.out.println("安全运行"); } }

    } }

Above you can see run() method doesn't have any implementation in class A.上面你可以看到 run() 方法在 class A 中没有任何实现。

It cannot be instantiated because of this only as there is no implementation of run() in class A它不能被实例化,因为 class A 中没有 run() 的实现

so to make it work it need to be extended and implemented (that's what done in class B).所以为了使它工作,它需要扩展和实现(这就是在 class B 中所做的)。

In outer world people can only see run() they will not able to get how run works or actual implementation of run() method.在外部世界中,人们只能看到 run() 他们无法了解 run 的工作原理或 run() 方法的实际实现。

refer this link for more information: abstract class in java有关更多信息,请参阅此链接: java 中的抽象 class

Abstract classes are not hidden.抽象类没有隐藏。 Abstract classes are "incomplete" classes which can be "completed" in many different ways.抽象类是“不完整”的类,可以通过许多不同的方式“完成”。 If you have classes that contain a lot of similar code, it may be a good idea to move all the similar code into the abstract class and make your classes extend this abstract class.如果您的类包含大量相似代码,最好将所有相似代码移入抽象 class 并让您的类扩展此抽象 class。

Abstract classes are otherwise quite full-featured.抽象类在其他方面功能非常齐全。 They can have constructors even, despite the fact that you can't instantiate them.尽管您无法实例化它们,但它们甚至可以具有构造函数。 These constructors can be called via the super keyword from child class constructors:这些构造函数可以通过子 class 构造函数的super关键字调用:

abstract class Parent {
    private String name;

    public Parent(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

class Child extends Parent {
    private boolean hasMilkTeeth;

    public Child(String name, boolean hasMilkTeeth) {
        super(name); // calls the parent constructor!
        this.hasMilkTeeth = hasMilkTeeth;
    }

    public boolean hasMilkTeeth() {
        return hasMilkTeeth;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM