How to create an object of an abstract class and interface

前端 未结 12 1174
旧巷少年郎
旧巷少年郎 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:03
    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
    
    0 讨论(0)
  • 2020-12-09 05:10

    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.

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

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

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

    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();
        }
    
    }
    
    0 讨论(0)
  • 2020-12-09 05:15

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

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

    No, you are not creating the instance of your abstract class here. Rather you are creating an instance of an anonymous subclass of your abstract class. And then you are invoking the method on your abstract class reference pointing to subclass object.

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

    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.html and http://download.oracle.com/javase/tutorial/java/concepts/interface.html

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