How to call a Subclass as a Superclass Type

barapapupi

i'm currently learning inheritance and I'm having some trouble understanding the concept. For the following code could someone tell me why I can't declare a subclass of type Superclass?

public class Test{
    public static void main(String[] args){
    Superclass myA = new Superclass();
    Superclass myB = new Subclass();
    }
public class Superclass{
    private string a;

    public Superclass()
    {
        a="";
    }
    public void setA(String userA)
    {
        a = userA;     
    }
}
public class Subclass extends Superclass{
    private String b;

    public Subclass()
    {
        b = "";
    }
    public void setB(String userB)
    {
        b = userB;
    }
    public void display()
    {
        System.out.print(b)
    }
}

myA doesnt give me any problems, but myB does. I want to be make an array of lets say Superclass, but I want to be able to use the Subclass.Something like this

Superclass[] X = new Superclass[2];
x[0] = new Subclass();
X[0].setB("hello");
X[0].display();
Harish Narayan

You are assigning subclass object to a superclass type variable(i.e,upcasting is done), its fine. But when u do that, only superclass methods can be accessed. So in order to access the subclass methods please do downcast to its subclass type. eg.

    Superclass[] X = new Superclass[2];
    X[0] = new Subclass();
    ((Subclass) X[0]).setB("hello");
    ((Subclass) X[0]).display();

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to call subclass methods when subclass objects are stored in superclass array?

분류에서Dev

How to force call superclass implementation Object.toString() rather than subclass overriding implementation on an object in Java

분류에서Dev

How to force call superclass implementation Object.toString() rather than subclass overriding implementation on an object in Java

분류에서Dev

how can i determine the ACTUAL return type of a specific subclass of a generic superclass?

분류에서Dev

How to set the superclass instance within the initialization of subclass

분류에서Dev

How to create an array of instances of a subclass from a superclass

분류에서Dev

Get only the properties of the superclass from subclass

분류에서Dev

Alter superclass' variable from subclass on Python

분류에서Dev

Java 입문 : SuperClass 및 SubClass

분류에서Dev

SuperClass의 모든 SubClass와 SubClass의 모든 SuperClass 인쇄

분류에서Dev

Accessing Subclass Properties After Passing as The Superclass (as3)

분류에서Dev

How to access the superclass

분류에서Dev

Can't call a State in subclass?

분류에서Dev

C ++-{superclass}의 변수를 {subclass}로 캐스팅

분류에서Dev

SubClass 대신 SuperClass를 참조로 사용하십니까?

분류에서Dev

SuperClass가 구현 한 SubInterface로 Subclass 캐스팅

분류에서Dev

Java wrapper class subclass of concrete type

분류에서Dev

Reflecting superclass's value type arguments in Scala, without TypeTag

분류에서Dev

How to intercept a call from $http and provide a response accordingly to the type of request?

분류에서Dev

How to call async method inside a method which has return type?

분류에서Dev

Wordpress: How to call a function through custom post type functions in a plugin?

분류에서Dev

SuperClass 생성자 매개 변수를 SubClass에 전달 하시겠습니까?

분류에서Dev

SuperClass 생성자 매개 변수를 SubClass에 전달 하시겠습니까?

분류에서Dev

WPF - How to databind subclass property correctly

분류에서Dev

How can I add subclass to NSTableCellView?

분류에서Dev

Generic call signatures and type inference

분류에서Dev

How can I define event handlers in a Fabric.js subclass?

분류에서Dev

How do I reference a subclass's parent view controller?

분류에서Dev

How best to subclass NSManagedObject to provide a set of core methods

Related 관련 기사

  1. 1

    How to call subclass methods when subclass objects are stored in superclass array?

  2. 2

    How to force call superclass implementation Object.toString() rather than subclass overriding implementation on an object in Java

  3. 3

    How to force call superclass implementation Object.toString() rather than subclass overriding implementation on an object in Java

  4. 4

    how can i determine the ACTUAL return type of a specific subclass of a generic superclass?

  5. 5

    How to set the superclass instance within the initialization of subclass

  6. 6

    How to create an array of instances of a subclass from a superclass

  7. 7

    Get only the properties of the superclass from subclass

  8. 8

    Alter superclass' variable from subclass on Python

  9. 9

    Java 입문 : SuperClass 및 SubClass

  10. 10

    SuperClass의 모든 SubClass와 SubClass의 모든 SuperClass 인쇄

  11. 11

    Accessing Subclass Properties After Passing as The Superclass (as3)

  12. 12

    How to access the superclass

  13. 13

    Can't call a State in subclass?

  14. 14

    C ++-{superclass}의 변수를 {subclass}로 캐스팅

  15. 15

    SubClass 대신 SuperClass를 참조로 사용하십니까?

  16. 16

    SuperClass가 구현 한 SubInterface로 Subclass 캐스팅

  17. 17

    Java wrapper class subclass of concrete type

  18. 18

    Reflecting superclass's value type arguments in Scala, without TypeTag

  19. 19

    How to intercept a call from $http and provide a response accordingly to the type of request?

  20. 20

    How to call async method inside a method which has return type?

  21. 21

    Wordpress: How to call a function through custom post type functions in a plugin?

  22. 22

    SuperClass 생성자 매개 변수를 SubClass에 전달 하시겠습니까?

  23. 23

    SuperClass 생성자 매개 변수를 SubClass에 전달 하시겠습니까?

  24. 24

    WPF - How to databind subclass property correctly

  25. 25

    How can I add subclass to NSTableCellView?

  26. 26

    Generic call signatures and type inference

  27. 27

    How can I define event handlers in a Fabric.js subclass?

  28. 28

    How do I reference a subclass's parent view controller?

  29. 29

    How best to subclass NSManagedObject to provide a set of core methods

뜨겁다태그

보관