📜  我们可以在Java中实例化抽象类吗?

📅  最后修改于: 2022-05-13 01:55:17.278000             🧑  作者: Mango

我们可以在Java中实例化抽象类吗?

抽象类,我们听说抽象类是可以有抽象方法并且不能被实例化的类。我们不能在Java中实例化抽象类,因为它是抽象的,不完整,因此不能使用。

示例 1

Java
// Java program to demonstrate abstract class
// cannot have instance
  
public abstract class ClassOne {
  
    public void printSomething()
    {
        System.out.println("Hello in abstract class");
    }
}
class CreateClassOne {
  
    public static void main(String[] args)
    {
        // instance of abstract 
          // class "ClassOne"
        ClassOne obj = new ClassOne();
    }
}


Java
// Java code to demonstrate
// Anonymous class
  
public abstract class ClassOne {
  
    public void printSomething()
    {
        System.out.println("Hello in abstract class");
    }
}
  
class InheritClassOne {
  
    public static void main(String[] args)
    {
  
        // obj points to anonymous subclass
        ClassOne obj = new ClassOne() {};
        
          // calls the implementation 
          // provided by ClassOne
        obj.printSomething();
    }
}


Java
// java program to demonstrate anonymous class
  
interface NewClass {
    public void show();
}
  
class AnonymousClassExample {
  
    public static void main(String[] args)
    {
        // nc points to anonymous class inside
        // the {}
        NewClass nc = new NewClass() {
            public void show()
            {
                System.out.println(
                    "This is an anonymous class implementing interface");
            }
        };
  
        // calls the implementation
        // method of anonymous class
        nc.show();
    }
}


Java
// Anonymous class implementing abstract class
  
public abstract class ClassOne {
  
    public ClassOne()
    {
        System.out.println(
            "Anonymous(Unnamed) Subclass object Created");
    }
  
    public void printSomething()
    {
        System.out.println(
            "Hello,in abstract class:printSomething method");
    }
    public abstract void implementMethod();
}
  
class AnonymousClassExample {
  
    public static void main(String[] args)
    {
        // anonymous class
        ClassOne obj = new ClassOne() {
            @Override public void implementMethod()
            {
                System.out.println(
                    "Implemented abstract method in anonymous class");
            }
        };
  
        // calls abstract class implementation
        obj.printSomething();
  
        // calls anonymous class implementation
        obj.implementMethod();
    }
}


输出

prog.java:17: error: ClassOne is abstract; cannot be instantiated
        ClassOne obj = new ClassOne();
                       ^
1 error

示例 2

Java

// Java code to demonstrate
// Anonymous class
  
public abstract class ClassOne {
  
    public void printSomething()
    {
        System.out.println("Hello in abstract class");
    }
}
  
class InheritClassOne {
  
    public static void main(String[] args)
    {
  
        // obj points to anonymous subclass
        ClassOne obj = new ClassOne() {};
        
          // calls the implementation 
          // provided by ClassOne
        obj.printSomething();
    }
}
输出
Hello in abstract class

这里是抽象类实例化的!但那是不可能的!

是的,答案还是一样,抽象类不能被实例化,这里ClassOne的第二个例子中的对象不是创建的,而是抽象类的一个匿名子类的实例。然后您在指向子类对象 obj 的抽象类引用上调用方法printSomething() 。当您在第二个类中创建对象时添加了{ }时,编译器会将其视为匿名类,其中{ }表示匿名类的主体。

示例 1

Java

// java program to demonstrate anonymous class
  
interface NewClass {
    public void show();
}
  
class AnonymousClassExample {
  
    public static void main(String[] args)
    {
        // nc points to anonymous class inside
        // the {}
        NewClass nc = new NewClass() {
            public void show()
            {
                System.out.println(
                    "This is an anonymous class implementing interface");
            }
        };
  
        // calls the implementation
        // method of anonymous class
        nc.show();
    }
}
输出
This is an anonymous class implementing interface

在这个例子中,接口没有被实例化,但是一个匿名类正在实现接口 New Class。现在,您可能已经了解第二个示例如何工作,而不是抽象类实例化,而是创建匿名子类或由抽象类实现抽象类。

示例 2

Java

// Anonymous class implementing abstract class
  
public abstract class ClassOne {
  
    public ClassOne()
    {
        System.out.println(
            "Anonymous(Unnamed) Subclass object Created");
    }
  
    public void printSomething()
    {
        System.out.println(
            "Hello,in abstract class:printSomething method");
    }
    public abstract void implementMethod();
}
  
class AnonymousClassExample {
  
    public static void main(String[] args)
    {
        // anonymous class
        ClassOne obj = new ClassOne() {
            @Override public void implementMethod()
            {
                System.out.println(
                    "Implemented abstract method in anonymous class");
            }
        };
  
        // calls abstract class implementation
        obj.printSomething();
  
        // calls anonymous class implementation
        obj.implementMethod();
    }
}
输出
Anonymous(Unnamed) Subclass object Created
Hello,in abstract class:printSomething method
Implemented abstract method in anonymous class