Call a Method of Subclass in Java

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 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() );

calling method in super class using object of sub class how it work?

a.print() is an example of method overriding which is runtime polymorphism. That is, at runtime it decides whether to call the subclass method or the super class method (here your SubClass method is called as it is overridden and matches the parameter signature).

In case of someMethod() call, you have not defined it in your SubClass. That means, your SubClass automatically got a someMethod() when you inherited it from Superclass as it is public. Putting it in simple words, SubClass also has the definition of someMethod(). Thats how JVM got to know which method to call.

Also in SuperClass a, the variable 'a' will be stored in stack whereas the reference will be stored in heap. You have constructed the reference using SubClass's constructor. Meaning the variable 'a' in stack refers to a 'SubClass' object in heap. That is why 'a' is able to call someMethod

How to call a subclass method from super's associated class

You can do this using instanceof operator and casting

public class Doctor extends Person {
//..some code
public void maternityWard() {
for(Person p : building.getPersonList()){
if(p instanceof Parent){
Parent parent = (Parent)p;
parent.setChildBorn();
}
}
}

P.S.
Though this will help you invoking the method of Parent but this is a Bad Practice, though good to understand the language semantics and syntax.

Few good application design practices

  1. higher and lower label modules should depend on only Abstractions. Depending on concrete class and not on the abstractions injects high coupling. Application should be designed to achieve low coupling and high cohesiveness between classes.
  2. Encapsulation (Building should not expose its state directly for access and modification, but should rather expose methods to handle its state)
  3. instanceof and casting is a code smell, avoid both of them.

How to call subclass method through superclass instance in java?

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

((Subclass) obj).method2();


Related Topics



Leave a reply



Submit