Java: Creating Reference Type from Abstract class and interface

mynameisJEFF

I am new to Java and I have read threads (here) that it is not possible to instantiate an abstract class. So, I tested it out.

The first test I did is shown below. And it seems like I can actually instantiate an abstract class and in fact, I actually have a new type which refers to the abstract class and the type is actually shared by all subclasses the extends it. This also means polymorhpism applies.

import java.util.*;

abstract class AbstractClass{
    public abstract void printname();
}


class Test1 extends AbstractClass{

    private String name;

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

    public void printname(){
        System.out.println("My name is " + name);
    }
}

class Test2 extends AbstractClass{
        private String saysomething;

    public Test2(String saysomething){
        this.saysomething = saysomething;
    }

    public void printname(){
        System.out.println(saysomething);
    }
}

class TestingApp{
    public static void main(String[] args){
        AbstractClass[] abstractclass_list = {new Test1("JEFFFFFF") , new Test2("Hey , say something")};
        for(AbstractClass item : abstractclass_list){
            item.printname();
        }
    }
}

Then I did another test but this time, instead of working on abstract class, I decided to create a type which refers to Interface. It seems like I can instantiate an interface as well. I can actually create a type that refers to the interface. This type is shared by all the classes that implements this interface. And polymorphism applies again.

import java.util.*;

interface  AbstractInterface{
    public void printname();
}


class Test4 implements AbstractInterface{

    private String name;

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

    public void printname(){
        System.out.println("My name is " + name);
    }
}

class Test3 implements AbstractInterface{
    private String saysomething;

    public Test3(String saysomething){
        this.saysomething = saysomething;
    }

    public void printname(){
        System.out.println(saysomething);
    }
}

class TestingAbstractInterfaceApp{
    public static void main(String[] args){
        AbstractInterface[] abstract_list = {new Test4("Helen") , new Test3("Hey , say my name")};
        for(AbstractInterface item : abstract_list){
            item.printname();
        }
    }
}

Question:

I am sensing there is something wrong with what I am doing in my code. But I cannot quite explain how come the code still works when theoretically, it is impossible to instantiate an abstract class and interface. Am I actually instantiating an abstract class and an interface in the examples shown above ? Because this seems like exactly what I have done, as I have a new type for the abstract class and interface. Please correct me, if my logic is wrong or if I am using the wrong words.

Update: SO I guess my misunderstanding is about type. I always thought type can only refer to normal Java classes but not abstract classes and interfaces. How does "type" actually work? Is it creating a reference?

Predrag Maric

Why do you think you are actually instantiating AbstractClass and AbstractInterface?

new Test1("JEFFFFFF") , new Test2("Hey , say something"), new Test4("Helen") , new Test3("Hey , say my name") are all instantiating concrete classes, not abstract ones.

If you refer to AbstractClass[] abstractclass_list = as proof of instantiating abstract classes, that is wrong. Here, you declare an array whose elements are of type AbstractClass, and Test1 and Test2 are (since they extend AbstractClass).

UPDATE

You could have something like this AbstractClass abs = new Test1("hey"); and what it does is it creates a new instance of class Test1, and references that instance from variable abs. abs's concrete type is Test1, but only methods declared in AbstractClass are visible on it. If you want to call methods of Test1, you would have to cast it first.

AbstractClass abs = new Test1("hey");
abs.printname(); // this is ok, and it calls `printname() implemented in Test1
abs.someTest1Method(); // this is NOT ok, someTest1Method() is not visible to abs
((Test1)abs).someTest1Method(); // this is ok, abs is cast to Test1, but would fail if abs was instantiated as 'abs = new Test2("t2")' (would throw ClassCastException)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Abstract class inheriting a generic type requiring a constructor

来自分类Dev

Android abstract parcelable class

来自分类Dev

Abstract class extends concrete class

来自分类Dev

How to dynamically add interface to existing Java class in Groovy

来自分类Dev

如何在Java中的方法签名中使用Class <Interface>

来自分类Dev

casting to an abstract generic base class

来自分类Dev

Creating a reference out of multiple temporaries

来自分类Dev

在Java中将对象扩展抽象类的方法添加到类型为Abstract Class的Arraylist中的方法

来自分类Dev

Java:如何使用Abstract类

来自分类Dev

d:DesignInstance with an interface type

来自分类Dev

Abstract data type constructor can be accidentally bypassed?

来自分类Dev

初始化Type Abstract类的ArrayList

来自分类Dev

VBA:是否有类似Abstract Class的东西?

来自分类Dev

Model Many to Many relations with Abstract Class

来自分类Dev

passing Collection of abstract class as parameter in function

来自分类Dev

Weblogic config error - Message body writer for Java class, and Java type class, and MIME media type application/json was not found

来自分类Dev

Can I Reference Const Fields From a Nested Struct Without Referencing the Containing Class?

来自分类Dev

Creating a class with different argument lengths

来自分类Dev

R:类型为Reference Class的Reference Class中的字段

来自分类Dev

编译时出错“Sample1.java:1: error: class, interface, or enum expected Import java.util.Scanner;”

来自分类Dev

Fails freeing Interface implemented class

来自分类Dev

Struct vs class implementing an interface

来自分类Dev

Equivalence of abstract classes/methods (Java) in Google Go

来自分类Dev

Unable to have methods from one class use user input from another class in Java

来自分类Dev

Java何时引入@interface?

来自分类Dev

How to get generic type information from getAnnotatedParameterTypes() in Java 8?

来自分类Dev

Java graphics not drawing getX() and getY() from player class

来自分类Dev

List out declared methods of a Groovy class from Java

来自分类Dev

How to get name of the object's from a different class in Java

Related 相关文章

  1. 1

    Abstract class inheriting a generic type requiring a constructor

  2. 2

    Android abstract parcelable class

  3. 3

    Abstract class extends concrete class

  4. 4

    How to dynamically add interface to existing Java class in Groovy

  5. 5

    如何在Java中的方法签名中使用Class <Interface>

  6. 6

    casting to an abstract generic base class

  7. 7

    Creating a reference out of multiple temporaries

  8. 8

    在Java中将对象扩展抽象类的方法添加到类型为Abstract Class的Arraylist中的方法

  9. 9

    Java:如何使用Abstract类

  10. 10

    d:DesignInstance with an interface type

  11. 11

    Abstract data type constructor can be accidentally bypassed?

  12. 12

    初始化Type Abstract类的ArrayList

  13. 13

    VBA:是否有类似Abstract Class的东西?

  14. 14

    Model Many to Many relations with Abstract Class

  15. 15

    passing Collection of abstract class as parameter in function

  16. 16

    Weblogic config error - Message body writer for Java class, and Java type class, and MIME media type application/json was not found

  17. 17

    Can I Reference Const Fields From a Nested Struct Without Referencing the Containing Class?

  18. 18

    Creating a class with different argument lengths

  19. 19

    R:类型为Reference Class的Reference Class中的字段

  20. 20

    编译时出错“Sample1.java:1: error: class, interface, or enum expected Import java.util.Scanner;”

  21. 21

    Fails freeing Interface implemented class

  22. 22

    Struct vs class implementing an interface

  23. 23

    Equivalence of abstract classes/methods (Java) in Google Go

  24. 24

    Unable to have methods from one class use user input from another class in Java

  25. 25

    Java何时引入@interface?

  26. 26

    How to get generic type information from getAnnotatedParameterTypes() in Java 8?

  27. 27

    Java graphics not drawing getX() and getY() from player class

  28. 28

    List out declared methods of a Groovy class from Java

  29. 29

    How to get name of the object's from a different class in Java

热门标签

归档