Is there a way in Java to call different child method depending on which subclass is the object?

how to access superclass variables in subclass in java
call subclass method from superclass java
inheritance in java
polymorphism in java
abstract class in java
superclass reference to subclass object in java
java vehicle class example
why can't we assign parent class object to child class reference in java

In my project I have a superclass and two subclasses extending from it. There is a method in the superclass that is overriden differently in each subclass. I want to know if it's possible to introduce a method (in another class) that takes object of either subclass as a parameter and calls a method overriden in one of subclasses (depending on to which subclass does the object belong).

public class Superclass{
    public int method(){return 0;}
}
public class Subclass1 extends Superclass{
    public int method(){return 1;}
}
public class Subclass2 extends Superclass{
    public int method(){return 2;}
}
public class CallingClass{
    public static int dependantCall(Superclass parameter){return parameter.method}

I want to be able to do something like

Subclass1 subclassObject = new Subclass1;
System.out.println(CallingClass.dependantCall(subclassObject));

and get output

1

Yes, it is entirely possible. Here's how that method would look like:

public <T extends Superclass> void foo(T subclassObject) {
...
} 

Or:

public void foo(Superclass obj) {
...
}

Note that in the above method, you can pass subclasses' objects as well (they are covariant data types).

Can we call sub class methods using super class object , Can you call a parent class method from child class object? A method signature is consists of the method name and parameter list in Java i.e. number, type, and order of parameters. Java return type and exceptions are not a part of the method signature. Let’s revise the concept of Data Types in Java. 3. How to Call a Method in Java? In Java to use a method, we need to call it.


That is what Polymorphism is for! Defining the Superclass as a parameter type will allow you to pass either subclass in.

For example in your other class you can define it like this:

// classes Dog and Cat extend Animal and override makeNoise()
class Owner{

  playWith(Animal a){
    a.makeNoise();
  }

}

Now the Owner can accept owner.makeNoise(cat) and owner.makeNoise(dog)

More reading: https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html

Which members are not inherited in a child class?, In Java, all non-static methods are based on the runtime type of the underlying object rather than the type of the reference that points to that object. Therefore, it  12. With this type of binding, the Java Virtual Machine determines at runtime which method to call, depending on the type of the object that a variable references. a. static b. early c. flexible d. dynamic


This is what Java does by default when you create subclases, so no need to do anything special. Each object carries it's type information at run time, and the method invoked would always be the most specific one for the object. Example:

public class Doer {
  public void doSomething() {
    // Body presence
  };
}

public class Painter extends Doer {
  @Override
  public void doSomething() {
    // Paint here
  }
}

public class Manager extends Doer {
  @Override
  public void doSomething() {
    // Micromanage here
  }
}

// Elsewhere in your code: 
public void busyness(Doer doer) {
   doer.doSomething();
}

A style note: if it is possible, one should prefer using interfaces instead of base classes (base classes those should be used only if you want to share implementation between subclasses). Example with interfaces:

public interface Doer {
  void doSomething();
}

public class JackOfAllTrades implements Does {
  @Override
  public void doSomething() {
     // Do whatever necessary
  }
}

// Client code stays exactly the same as above: 
public void busyness(Doer doer) {
   doer.doSomething();
}

Note that in Java a class can have only one base class but can implement multiple interfaces.

@Override annotations are not strictly required, but they help Java compiler to spot some errors for you (e.g. if you misprint method name).

In your example it would look like

public class CallingClass {
  public static int dependantCall(Superclass parameter) {
    return parameter.method();
  }
}

Subclass1 subclassObject = new Subclass1();
System.out.println(CallingClass.dependantCall(subclassObject));

Call a method of subclass in Java, methods. If your usual time period for payment to each Employee object is weekly​, Because each Employee type requires different paycheck-calculating the subclass method overloads the parent class method, and any subclass object lists are polymorphic because the method call operates differently depending on​  I have a class A,B and C. Now B and C extends A. So is there any way that we can call a method of B in A? Well depending on whether or not the extending classes are static or non-static-the same applies for your methods within these classes-there are many different ways to access them. Here is a


calling parent class method from child class object in java, Java provides a neat way to “inherit” parent properties : It is a template or blueprint from which objects are created. Sub Class/Child Class: Subclass is a class which inherits the other class. N.B. : Although you can call the parent method by using a super call, you cannot go up the inheritance hierarchy  9.8 Abstract Classes and Abstract Methods . In previous example we have created Rectangle and Triangle object. The Shape class only be used as super class for inheritance and polymorphism purpose not for object.


Referencing Subclass objects with Subclass vs Superclass , A class in Java can be declared as a subclass of another class using the extends keyword. Similarly, any method accepting an Animal object would accept an instance The actual variable selected when you reference it in the code would depend Because both variables exist in DecimalCalculator , we need a way to​  An abstract method is declared by abstract keyword, such methods cannot have a body. If a class contains an abstract method, then it also needs to be abstract. Concrete Class: A concrete class in Java is a type of subclass, which implements all the abstract method of its super abstract class which it extends to. It also has implementations of


Java Programming, Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). In the absence of any other explicit superclass, every class is implicitly a subclass of Object . A subclass inherits all the members (fields, methods, and nested classes) from its superclass​. When you're writing a method in the subcass that needs to directly call the method inside the superclass (ex. if you're calling method display, inside display in the subclass, you need to reference the superclass so that it's not calling itself over and over." Super is NOT an object refernece