How can do a Polymorphism with subclass in a superclass?

polymorphism example
polymorphism shape example java
inheritance and polymorphism in java with example programs
how do we send information from the subclass to the superclass
java polymorphism exercises
how to access superclass variables in subclass in java
get subclass from superclass java
polymorphism explained

Hello I have the following classes

class A <T extends AnotherClass>{
     public static A getA(){
          return new A();
     }
}
class B<T> extends A{}
class C<T> extends A{}

And I am trying to do a polymorphism of this way:

B b = A.getA();

But I am getting the following error:

Incompatible types requite B found A.

How can I do work this?

Thanks

It is possible to do A b = new B(); but not the other way around like you are trying to do. Basically you can only put a subclass in a super but not a super in a subclass.

Using polymorphism with classes, Multiple subclasses that inherit from the same super class can override is dispatched to the overriding method in the actual instantiated subclass at run time. All it can do is verify that a method exists in the superclass, and verify that the method call's arguments list and return type match the superclass's method declaration.

It can not work. There is no polymorphism involved with static methods. They exist only in the context of the declaring class. So getA will always return an instance of A in your case and it will never be B.

You have not declared what type the A will be in terms of generics, but this is less important.

In this sense

B b = A.getA(); 

is nonsense, because new A() will never be B.

Subclasses, Inheritance, and Polymorphism, We can create subclasses of a given class that will inherit all of the instance variables and methods of the given class (called the super class) using the keyword  I have a question for the polymorphism in java, which is when Superclass s =new Subclass(). the "s" object always invoke the method in the Superclass, but when the override method happened, the "s" will point back to the override methods.

You can't perform Polymorphism but you can perform Reflection.

You need a utility class:

public class ClassUtils {
public static <SUP, SUB extends SUP> void asSubclass( SUP superClass, SUB subClass ) {
for (Field field : superClass.getFields()) {
field.setAccessible(true);
field.set(subClass, field.get(superClass));
}
}
}

You can use it like this:

A a = A.getA();
B b = new B();
ClassUtils.asSubclass( a, b );

OOP Inheritance & Polymorphism - Java Programming Tutorial, Notes: In this example, I used " name " for Book class instead of " title " to illustrate that It is important to note that a subclass is not a "subset" of a superclass. superclass: Parent class being extended. subclass: Child class that inherits behavior from superclass. gets a copy of every field and method from superclass is-a relationship: Each object of the subclass also "is a(n)" object of the superclass and can be treated as one.

Polymorphism and inheritance in Java, To better understand the purpose of polymorphism, take a look at the type of the overridden method if the return type is a subclass of the superclass method. It's run time polymorphism. We can also call it Method Overriding. In a class hierarchy, when a method in a sub class has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. This feature is called method overriding.

Java polymorphism and its types, The compiler coerces the subclass type to the superclass type to restrict Java supports parametric polymorphism via generics, which I'll  Java Inheritance (Subclass and Superclass) In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class; superclass (parent) - the class being inherited from; To inherit from a class, use the extends keyword.

[PDF] Unit 2 – part 4. Polymorphism(OOP), i bl. Using protected instance variables. Advantages subclasses can modify values subclass methods more likely dependent on superclass implementation​. Since SubClass is a SuperClass, you can use an instance of SubClass and treat it as SuperClass. The same behavior is used when working with interfaces: List someList = new ArrayList(); That's the beauty of polymorphism.

Comments
  • A dog and a cat are both animals, but you are trying to: Dog dog = Cat.getCat();
  • I am trying Dog dog = Animal.getAnimal() because Dog is B and animal is A
  • That is the only error you get? so you mean to say that public static getA() { return new A(); } compiles?
  • Nope, you're trying Dog dog = Animal.getAnimal() instead of Dog dog = Animal.getDog()
  • I just edit now, this code is only a draft for an another piece of code more big