How to create an object of an abstract class and interface

前端 未结 12 1175
旧巷少年郎
旧巷少年郎 2020-12-09 04:36

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.

相关标签:
12条回答
  • 2020-12-09 05:21

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

    You cant instantiate an abstract class or an interface you can only instantiate one of their derived classes.

    In your example

     MyAbstractClass ABC = new MyAbstractClass("name") {
        };
    

    You are instantiating any class that implements Suprising

    0 讨论(0)
  • 2020-12-09 05:23

    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.

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

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

    Hope this helps.

    Regards, Mahendra Liya.

    0 讨论(0)
  • 2020-12-09 05:25
    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 { ... }
    
    0 讨论(0)
  • 2020-12-09 05:28

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

    0 讨论(0)
  • 2020-12-09 05:28

    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.

    Hope help.

    0 讨论(0)
  • 2020-12-09 05:29

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

    0 讨论(0)
提交回复
热议问题