tazboy
tazboy

Reputation: 1754

Proper way of calling a method in a subclass if it's referenced as a super class

class SuperClass () {
  doSuperStuff();
}

class SubClass extends SuperClass () {
  doStuff();
}

SuperClass aClass = new SubClass();

In order to call the method doStuff() do I need to cast it like (SubClass)aClass.doStuff(); ?

  1. Is that normally the way to do it?
  2. Is there a better way?
  3. Should I "always" initialize SubClass that way in case I want to put a bunch of subclasses of SuperClass into an array or something like that?

UPDATE: After reading everyone's response:

Ok. I'm definitely missing something in my understanding. I thought that having a subclass inherit everything from the superclass and then making one more method inside the subclass is ok. Should I be making an interface then?

Also, I had SuperClass class = new SubClass(); but renamed it to SuperClass aClass = new SubClass();

Upvotes: 0

Views: 1628

Answers (4)

user3437460
user3437460

Reputation: 17454

  • First of all, if you want to invoke a method from the subclass, in the first place you don't have to use a data type of superclass to create the variable. You could simply:

    MySubClass obj1 = new MySubClass();
    

obj1 will be able to access both methods from its superclass (due to inheritance, except private methods in the superclass) and from itself.

  • Secondly, you can't name a variable as class.

  • Thirdly, if you want to do a casting, it goes like this:

    MySuperClass obj2 = new MySubClass();
    ((MySubClass)obj2).doStuff();
    

Upvotes: 3

alainlompo
alainlompo

Reputation: 4434

I think this line will probably not compile

SuperClass class = new SubClass();

However assuming you have SuperClass clazz = new SubClass(); The declaration is done at compile time and so is the invocation of the methods. Therefore you will only see the methods that are declared on the type with which clazz was declared. So yes if you need to invoke a sub class methods you need to perform the appropriate cast on clazz.

Regarding your second question: this is the correct way to do things if you want to take advantage of polymorphic behaviour... which lead us to your third question. Yes this is the way to take advantage of polymorphism by having a bunch of subclasses's behaviours accessed in a uniform way after storing in a collection or an array declared with the base type

Upvotes: 0

Jim Garrison
Jim Garrison

Reputation: 86754

If doStuff() pertains only to the subclass and has no meaning in the superclass then you should be using subclass instances at that point.

If, however doStuff() has semantic validity in all subclasses of the superclass, then the superclass should have an abstract method doStuff() that is implemented in all subclasses.

Alternatively, if doStuff() applies to only some subclasses you could define an interface with that method and have those subclasses implement the interface.

Which approach you use depends on the overall application structure and you haven't really provided enough information for us to guide you further.

Upvotes: 1

tddmonkey
tddmonkey

Reputation: 21184

  1. Your code won't actually work, but yes casting is the only way to do this (but don't). What you need to do is ((SubClass)class).doStuff();
  2. No. I would recommend not even doing this
  3. No. If you need a SubClass, declare it as a SubClass. If you have a collection of objects that all inherit from SuperClass the only methods you should be calling are those defined on SuperClass. Doing anything else will lead to horrible, spaghetti like unmaintainable code

Upvotes: 1

Related Questions