17

If I have a base class Base thing = null; of which there is a subclass class Subclass extends Base and I instigate it as thing = new Subclass how would I call a method that is specifically in Subclass, but not in Base? ex. Base has only method() Subclass has method() and specialMethod() the method specialMethod() is the one I want to call.

eyecreate
  • 1,017
  • 3
  • 14
  • 26
  • 2
    It's generally a bad idea. If you know the object is of type Subclass, then refer to it as that and you have no problem. If the method really belongs in Base - put it in Base. – Carl Manaster Apr 23 '10 at 18:56

7 Answers7

18

If you know that thing contains a Subclass, you can do:

((Subclass) thing).specialMethod()
Eric Eijkelenboom
  • 6,943
  • 2
  • 25
  • 29
  • 6
    Note that if thing isn't a `Subclass`, this will bomb with a `ClassCastException`... use `instanceof` to check if `thing` is a `Subclass` first if you're not sure. – Powerlord Apr 23 '10 at 18:55
16

The others have already mentioned how to cast objects to get an answer to your question, but asking that question in the first place points to a possible design problem. Some possible reasons:

Community
  • 1
  • 1
Esko Luontola
  • 73,184
  • 17
  • 117
  • 128
9

You have to cast to the subclass to invoke the method:

Base thing = new SubClass();
((SubClass) thing ).specialMethod();

But if you're in this situation is probably a good indication you class design can be improved. So, before adding a lot of checks and casting like this:

 public void xyz ( Base thing ) {
     if( thing instanceof Subclass ) {
         ((SubClass)thing).specialMethod();
     }
 }

Consider moving the specialMethod() to the base class.

If this is not possible then just work with the subclass directly:

 SubClass thing = ... 
 // no need to cast
 thing.specialMethod();

But of course, this depends on what you're trying to do.

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
3

When dealing with inheritance/polymorphism in Java there are basically two types of casts that you see:

Upcasting:

Superclass x = new Subclass();

This is implicit and does not need a hard cast because Java knows that everything the Superclass can do, the Subclass can do as well.

Downcasting

Superclass x = new Subclass();

Subclass y = (Subclass) x;

In this case you need to do a hard cast because Java isn't quite sure if this will work or not. You have to comfort it by telling it that you know what you're doing. The reason for this is because the subclass could have some weird methods that the superclass doesn't have.

In general, if you want to instantiate a class to call something in its subclass, you should probably just instantiate the subclass to begin with -- or determine if the method should be in the superclass as well.

artgon
  • 714
  • 1
  • 6
  • 9
1

You have to type, or cast thing to the subclass. So:

      Subclass thing = new Subclass();

or:

     ((Subclass) thing).specialMethod();
Yishai
  • 90,445
  • 31
  • 189
  • 263
1

Another approach might be to do the following:

public abstract class Base {

    //method() not implemented

    public abstract void specialMethod();
}

public class Subclass extends Base {

    //method() not implemented

    @Override
    public void specialMethod() {
       //put your code here
       System.out.println("specialMethod from Subclass");
    }
}

So you can do:

thing.specialMethod();

and it will give you: "specialMethod from Subclass".

Tom Stein
  • 345
  • 2
  • 15
0

You could make the method you would like to call being an abstract method and have it implemented in the subclass. Then in your superclass, just call it as usual this.someMethod(). At runtime, it will go to someMethod() and run the code that is implemented from the subclass.

ferics
  • 77
  • 7