Java 如何创建抽象类和接口的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4321386/
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-08-14 15:51:03  来源:igfitidea点击:

How to create an object of an abstract class and interface

javainterfaceabstract-class

提问by gautam

How do I create an object of an abstract class and interface? I know we can't instantiate an object of an abstract class directly.

如何创建抽象类和接口的对象?我知道我们不能直接实例化抽象类的对象。

回答by Ignacio Vazquez-Abrams

You write a class that derives from the abstract class or implements the interface, and then instantiate that.

您编写一个派生自抽象类或实现接口的类,然后将其实例化。

回答by wkl

You can not instantiate an abstract class or an interface - you can instantiate one of their subclasses/implementers.

您不能实例化抽象类或接口 - 您可以实例化它们的子类/实现器之一。

Examples of such a thing are typical in the use of Java Collections.

这种事情的例子在 Java 集合的使用中很典型。

List<String> stringList = new ArrayList<String>();

You are using the interface type List<T>as the type, but the instance itself is an ArrayList<T>.

您使用接口类型List<T>作为类型,但实例本身是一个ArrayList<T>.

回答by digiarnie

public abstract class AbstractClass { ... }

public interface InterfaceClass { ... }

// This is the concrete class that extends the abstract class above and
// implements the interface above.  You will have to make sure that you implement
// any abstract methods from the AbstractClass and implement all method definitions
// from the InterfaceClass
public class Foo extends AbstractClass implements InterfaceClass { ... }

回答by maerics

public abstract class Foo { public abstract void foo(); }
public interface Bar { public void bar(); }
public class Winner extends Foo implements Bar {
  @Override public void foo() { }
  @Override public void bar() { }
}
new Winner(); // OK

回答by Dmitri

You can provide an implementation as an anonymous class:

你可以提供一个匿名类的实现:

new SomeInterface() {
    public void foo(){
      // an implementation of an interface method
    }
};

Likewise, an anonymous class can extend a parent class instead of implementing an interface (but it can't do both).

同样,匿名类可以扩展父类而不是实现接口(但它不能同时实现)。

回答by Karl Knechtel

"instantiate" means "create an object of".

“实例化”的意思是“创建一个对象”。

So you can't create one directly.

所以你不能直接创建一个。

The purpose of interfaces and abstract classes is to describe the behaviour of some concrete class that implements the interface or extends the abstract class.

接口和抽象类的目的是描述一些实现接口或扩展抽象类的具体类的行为。

A class that implements an interface can be used by other code that only knows about the interface, which helps you to separate responsibilities, and be clear about what you want from the object. (The calling code will only know that the object can do anything specified in the interface; it will not know about any other methods it has.)

一个实现了接口的类可以被其他只知道接口的代码使用,这有助于你分离职责,并清楚你想要的对象。(调用代码只知道对象可以做接口中指定的任何事情;它不知道它拥有的任何其他方法。)

If you are using someone else's code that expects a Fooable(where that is the name of some interface), you are not really being asked for an object of some Fooable class (because there isn't really such a class). You are only being asked for an instance of some class that implements Fooable, i.e. which declares that it can do all the things in that interface. In short, something that "can be Foo'd".

如果您使用的其他人的代码需要 a Fooable(其中是某个接口的名称),那么您实际上并没有被要求提供某个 Fooable 类的对象(因为实际上并没有这样的类)。你只被要求提供一个实现 Fooable 的类的实例,即它声明它可以做那个接口中的所有事情。简而言之,“可以被Foo'd”的东西。

回答by gigadot

What you know is correct. You cannot create an object of abstract class or interface since they are incomplete class (interface is not even considered as a class.)

你所知道的是正确的。您不能创建抽象类或接口的对象,因为它们是不完整的类(接口甚至不被视为类。)

What you can do is to implement a subclass of abstract class which, of course, must not be abstract. For interface, you must create a class which implement the interface and implement bodies of interface methods.

您可以做的是实现抽象类的子类,当然,它不能是抽象的。对于接口,您必须创建一个实现接口并实现接口方法主体的类。

Here are orginal tutorial on oracle site, http://download.oracle.com/javase/tutorial/java/IandI/abstract.htmland http://download.oracle.com/javase/tutorial/java/concepts/interface.html

这里是 oracle 站点上的原始教程,http://download.oracle.com/javase/tutorial/java/IandI/abstract.htmlhttp://download.oracle.com/javase/tutorial/java/concepts/interface。 html

回答by Mahendra Liya

There are two ways you can achieve this.

有两种方法可以实现这一点。

1) Either you extend / implement the Abstract class / interface in a new class, create the object of this new class and then use this object as per your need.

1)要么在新类中扩展/实现抽象类/接口,创建这个新类的对象,然后根据需要使用这个对象。

2) The Compiler allows you to create anonymous objects of the interfaces in your code.

2) Compiler 允许您在代码中创建接口的匿名对象。

For eg. ( new Runnable() { ... } );

例如。( new Runnable() { ... } );

Hope this helps.

希望这可以帮助。

Regards, Mahendra Liya.

问候,马亨德拉·利亚。

回答by Marcin

To create object of an abstract class just use newjust like creating objects of other non abstract classes with just one small difference, as follows:

要创建抽象类的对象,只需使用new就像创建其他非抽象类的对象一样,只有一个小区别,如下所示:

package com.my.test;

public abstract class MyAbstractClass {
    private String name;

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

    public String getName(){
        return this.name;
    }


}

package com.my.test;

public class MyTestClass {

    public static void main(String [] args)
    {
        MyAbstractClass ABC = new MyAbstractClass("name") {
        };

        System.out.println(ABC.getName());
    }

}

In the same way You can create an object of interface type, just as follows:

同理可以创建接口类型的对象,如下:

package com.my.test;

public interface MyInterface {

    void doSome();
    public abstract void go();

}

package com.my.test;

public class MyTestClass {

    public static void main(String [] args)
    {

        MyInterface myInterface = new MyInterface() {

            @Override
            public void go() {
                System.out.println("Go ...");

            }

            @Override
            public void doSome() {
                System.out.println("Do ...");

            }
        };

        myInterface.doSome();
        myInterface.go();
    }

}

回答by tsingh

NO, we can't create object out of an interface or Abstract class because

不,我们不能从接口或抽象类中创建对象,因为

Main intention of creating an object is to utilize the wrapped methods and data. As interface don't have any concrete implementation hence we cannot.

创建对象的主要目的是利用包装的方法和数据。由于接口没有任何具体的实现,因此我们不能。

For abstract class we may have concrete method or abstract method or both. There is no way for the API developer to restrict the use of the method thats don't have implementation.

对于抽象类,我们可能有具体方法或抽象方法或两者兼而有之。API 开发人员无法限制没有实现的方法的使用。

Hope help.

希望有所帮助。