抽象类和所有方法都是抽象的类有什么区别?

发布于 2024-09-12 17:28:46 字数 101 浏览 4 评论 0原文

我想知道 Java 中的抽象类和具有所有方法抽象的类有什么区别?我的意思是,抽象类只是一个其方法自动获取抽象的类吗?

I wonder what is the difference in Java between an abstract class and a class that has all its methods abstract? I mean, is an abstract class just a class whose methods automatically get abstract?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝礼 2024-09-19 17:28:46

绝对不是。事实上,一个类可以是抽象的,而没有任何方法都是抽象的,尽管这种情况相对较少(请参阅下面 Mark 的评论以获取示例)。另一方面,如果一个类具有任何抽象方法,那么它必须声明为抽象的。

一般来说,抽象类的目的是提供一个具有一些非抽象行为的骨架,但其他部分仍需由子类填充。例如,这可以与模板方法模式一起使用。

Absolutely not. Indeed, a class can be abstract without any methods being abstract, although that's relatively rare (see Mark's comment below for an example). On the other hand, if a class has any abstract methods, then it must be declared abstract.

Generally speaking, the purpose of an abstract class is to provide a skeleton with some non-abstract behaviour, but other bits still to be filled in by subclasses. This can be used with the template method pattern, for example.

痴情换悲伤 2024-09-19 17:28:46

任何包含一个或多个抽象方法的类也必须声明为抽象的。要声明类抽象,只需在类声明开头的 class 关键字前面使用abstract 关键字即可。抽象类中不能有对象。也就是说,抽象类不能直接用new操作符实例化。这样的对象是没有用的,因为抽象类没有完全定义。此外,您不能声明抽象构造函数或抽象静态方法。抽象类的任何子类必须实现超类中的所有抽象方法,或者将其本身声明为抽象。

Any class that contains one or more abstract methods must also be declared abstract. To declare a class abstract, you simply use the abstract keyword in front of the class keyword at the beginning of the class declaration. There can be no objects of an abstract class. That is, an abstract class cannot be directly instantiated with the new operator. Such objects would be useless, because an abstract class is not fully defined. Also, you cannot declare abstract constructors, or abstract static methods. Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be itself declared abstract.

骄傲 2024-09-19 17:28:46

抽象类和接口之间的唯一区别是抽象类可以被继承,而接口则不能,因此接口没有任何构造函数,与抽象类相反。

The Only difference between abstract class and interface is that abstract class can be inherited and whereas interfaces can't, thus interfaces don't have any constructors conversely with an abstract class.

翻了热茶 2024-09-19 17:28:46

每当你在类中创建一个抽象方法时,你就在类名前显式地提到abstract关键字,就像这样

public abstract class Test {

abstract void show();

}

以下是Java中与抽象类相关的要点

--> 抽象类是无法实例化的类之一。

-->如果你想从子类(其他人)获取任何方法的实现,那么抽象方法可以在这个意义上使用。

--> 抽象类是不完整的,子类必须声明缺失的部分才能成为具体类(可以实例化对象的类),否则这些子类也成为抽象类。

--> 您可以通过“抽象类”来实现作为 OOP 主要支柱的抽象。
抽象隐藏了对象的不相关细节。

--> IS A 关系(继承)的抽象用途。

--> 抽象用于实现多态行为(OOP 的另一个主要支柱)

--> 抽象类不应该是私有的,也不应该包含私有方法。

--> 您扩展单个抽象类而不是多个,因为 Java 是支持单一继承的

-->抽象类必须包含 1 个或 1 个以上抽象方法

--> 如果任何类包含抽象方法,那么它应该显式声明抽象类,即使它包含具体方法。

-->构造函数和静态方法不能声明为抽象方法,因为构造函数不是继承的。

-->如果子类没有实现超类的抽象方法,那么它也成为抽象类。

--> 尝试实例化抽象类的对象是一个编译错误。

--> 抽象超类变量可以保存子具体对象的引用。

Whenever you make a abstract method in the class then you explicitly mention the abstract keyword before the class name, just like this

public abstract class Test {

abstract void show();

}

Here are the points related Abstract class in Java

-->Abstract class is the one of class that cannot be instantiated.

-->If you want to get implementation of any method from child class(other person) then abstract method can use in this sense.

-->Abstract class are incomplete ,subclass must declare missing piece to become concrete class(Class whose object can be instantiated ) , otherwise these subclass also become abstract class.

-->You can achieve abstraction that is main pillar of OOP through by "abstract classes".
Abstraction hide the irrelevant detail of an object.

-->Abstract use for IS A Relationship (Inheritance).

-->Abstraction use for to achieve Polymorphic behavior (Another main pillar of OOP)

-->abstract class should not be private and not contained private method.

-->You extends single abstract class not multiple because Java is Single supported Inheritance

--> Abstract class must contain 1 or more than 1 abstract method

-->If any class contain abstract method then it should explicitly declare abstract class even if it contain concrete method.

--> Constructor and static method cannot be declared as abstract because constructor are not inherited.

--> If child class have not implement the abstract method of super class then it become also abstract class.

-->Attempting to instantiate the object of abstract class is an Compilation error.

-->Abstract super class variable can hold the reference of child concrete object.

坚持沉默 2024-09-19 17:28:46

在其声明中包含abstract关键字的类称为抽象类

  • 抽象类可能包含也可能不包含抽象方法,即没有主体的方法 ( public void get(); )
  • 但是,如果一个类至少有一个抽象方法,则该类必须声明为抽象。
  • 如果一个类被声明为抽象类,则它不能被实例化。
  • 要使用抽象类,您必须从另一个类继承它,并提供其中抽象方法的实现。
  • 如果继承抽象类,则必须提供其中所有抽象方法的实现。

如果您不想为所有抽象方法提供实现,那么可以使用适配器类概念
例子:

abstract class A{
    public void m1();
    public void m2();
    public void m3();
    }

class B extends A{
    public void m1(){}
    public void m2(){}
    public void m3(){}
    }


class C extends B{
    public void m2(){
       System.out.println("Hello m2");
       }
    public static void main(String args){
     C obj=new C();
    C.m2();
    }
    }

A class which contains the abstract keyword in its declaration is known as abstract class.

  • Abstract classes may or may not contain abstract methods ie., methods with out body ( public void get(); )
  • But, if a class have at least one abstract method, then the class must be declared abstract.
  • If a class is declared abstract ,it cannot be instantiated.
  • To use an abstract class you have to inherit it from another class, provide implementations to the abstract methods in it.
  • If you inherit an abstract class you have to provide implementations to all the abstract methods in it.

If you don't want to provide implementation for all abstract methods then there is a concept of adapter class:
Example:

abstract class A{
    public void m1();
    public void m2();
    public void m3();
    }

class B extends A{
    public void m1(){}
    public void m2(){}
    public void m3(){}
    }


class C extends B{
    public void m2(){
       System.out.println("Hello m2");
       }
    public static void main(String args){
     C obj=new C();
    C.m2();
    }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文