简体   繁体   中英

How to cast subclass object to superclass object

i have 2 classes, called superclass and subclass, i tried to cast the subclass object to superclass, but its seems does not work when i want to use the subclass object from the superclass. Please help to explain. Thanks. These are the code:-

public class superclass
{
    public void displaySuper()
    }
        System.out.println("Display Superclass");
    }
}

public class subclass extends superclass
{
    public void displaySub()
    {  
        System.out.println("Display Subclass");
    }
}


public class Testing
{    
   public static void main(String args[])
   {
      subclass sub = new subclass();
      superclass sup = (superclass) sub; 

when i tried to use the displaySub() from the subclass get error

      sup.displaySub(); //the displaySub method cant found
   }
}

A superclass cannot know subclasses methods.

Think about it this way:

  • You have a superclass Pet

  • You have two subclasses of Pet , namely: Cat , Dog

  • Both subclasses would share equal traits, such as speak
  • The Pet superclass is aware of these, as all Pet s can speak (even if it doesn't know the exact mechanics of this operation)
  • A Dog however can do things that a cat cannot, ie eatHomework
  • If we were to cast a Dog to a Pet , the observers of the application would not be aware that the Pet is in fact a Dog (even if we know this as the implementers)
  • Considering this, it would not make sense to call eatHomework of a Pet

You could solve your problem by telling the program that you know sup is of type subclass

public class Testing
{    
   public static void main(String args[])
   {
      subclass sub = new subclass();
      superclass sup = (superclass) sub; 
      subclass theSub = (subclass) sup;
      theSub.displaySub();
   }
}

You could solve the problem altogether by doing something like this:

public class superclass
{
    public void display()
    }
        System.out.println("Display Superclass");
    }
}

public class subclass extends superclass
{
    public void display()
    {  
        System.out.println("Display Subclass");
    }
}

public class Testing
{    
   public static void main(String args[])
   {
      subclass sub = new subclass();
      superclass sup = (superclass) sub; 
      sup.display();
   }
}

check out this tutorial on more info: overrides

Take a look at java inheritance: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

A subclass can use superclass methods, but a superclass cannot use subclass methods. If you want to call a superclass to use a subclass method, you must first cast the superclass instance to the subclass. (subclass) sup

Even though both super and sub object point to the same object in memory, your super object knows nothing about the existence of sub.subMethod(). Hence, you got the above error. The following figure illustrates what happened:

在此处输入图片说明

This isn't going to work, because you're performing a narrowing reference conversion . From the JLS:

From any reference type S to any reference type T, provided that S is a proper supertype of T (§4.10)

In your scenario, subclass is a proper subtype of superclass . This means it is permissible to declare an instance of a subclass as a superclass :

superclass sc = new subclass();

as you've previously discovered.

However, class members are only ever transferred via inheritance ; that is, the superclass may pass along fields and methods to its subclasses, but the subclass cannot do the same for its superclass.

From the JLS:

The members of a class type are all of the following:

  • Members inherited from its direct superclass (§8.1.4), except in class Object, which has no direct superclass

  • Members inherited from any direct superinterfaces (§8.1.5)

  • Members declared in the body of the class (§8.1.6)

To tie all of these rather large words together:

  • You're travelling up the inheritance chain from child to parent
  • A parent may pass along methods and fields to its child, but a child cannot pass methods and fields to its parent
  • If you refer to an instance of a subclass by its superclass (as in superclass sc = new subclass(); ), you cannot access any of the subclass' member fields or methods.

As a general programming practice, the only time that you would use a broader class declaration is when you only need that object to do certain operations based on the traits of the parent class or interface, and not anything specific to sub-instances of the class or interface. In this case, you should avoid using the parent and use the child class instead, as you rely on very specific behavior that is only present in the child class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM