How to Call Subclasses' Methods on a Superclass Object

Calling a subclass method from superclass

When you declare a variable as having the type of the superclass, you can only access (public) methods and member variables of the superclass through that variable.

Pet cat = new Cat("Feline",12,"Orange"); 
cat.getName(); // this is OK
cat.getColor(); // this is not OK, getColor() is not in Pet

To access the methods in the concrete class (Cat in this case), you need to either declare the variable as the derived class

Cat cat = new Cat("Feline",12,"Orange"); 
cat.getName(); // OK, getName() is part of Cat (and the superclass)
cat.getColor(); // OK, getColor() is part of Cat

Or cast it to a type you know/suspect is the concrete type

Pet cat = new Cat("Feline",12,"Orange"); 
((Cat)cat).getName(); // OK (same as above)
((Cat)cat).getColor(); // now we are looking at cat through the glass of Cat

You can even combine the two methods:

Pet pet = new Cat("Feline",12,"Orange"); 
Cat cat = (Cat)pet;
cat.getName(); // OK
cat.getColor(); // OK

Java Inheritance: Calling a subclass method in a superclass

I'm very new to java and would like to know whether calling a subclass
method in a superclass is possible.

A superclass doesn't know anything about their subclasses, therefore, you cannot call a subclass instance method in a super class.

where is the proper place to set public static void main.

I wouldn't recommend putting the main method in the Admin class nor the User class for many factors. Rather create a separate class to encapsulate the main method.

Example:

public class Main{
public static void main(String []args) {
User user1 = new Admin("Bill", 18, 2);

System.out.println("Hello "+user1.getName());
user1.getLevel();
}
}

How do you call a subclass method from a superclass in Java?

The answer for my specific JavaFX subclassing question is over here Subclassing a JavaFX Application. For general subclassing the answers here are quite adequate.

How to call subclass method through superclass instance in java?

to do so, you have to cast it obj to SubClass:

((Subclass) obj).method2();

How to call subclass method (Java)

Your declared array arr is an array of type (class) SuperClass objects. Everything in the array must be a thing that is a SuperClass.

Your subclass named SubClass IS a thing of type SuperClass — it is an extension of that class, by adding y and getY, but it still conforms to SuperClass. Anything that you can do with SuperClass you can also do with SubClass, so you are allowed to add SubClass objects to the array of type SuperClass.

Where your error is, at arr[0].getY(), the thing that is in the spot at arr[0] is, in fact, a SubClass, because you just put it there, but there is no guarantee that that is the case — the only guarantee is that things in the array are of the type SuperClass, and things of type SuperClass don't have a getY() method.

When you know that a thing is of type SubClass you can cast it, like: (SubClass) arr[0], and must parenthesize it to apply the cast before you attach the method call, so it would be ((SubClass) arr[0]).getY()

Casting has it's own potential errors, if you try to cast something to SubClass when that thing isn't actually a SubClass it will cause an error (but that is another discussion).

So the answer to the question "How to call subclass method" is to cast the object to the subclass:

// Direct replacement for your line of code:
System.out.println( ((SubClass) arr[0]).getY() );

// or assign it to another variable first:
SubClass sc = (SubClass) arr[0];
System.out.println( sc.getY() );


Related Topics



Leave a reply



Submit