I read somewhere that, overriding is the means by which you get polymorphism. Polymorphism is the ability for an object to vary behavior based on its type.
Now can i just say that when different subclasses override the member of a parent class then it gives me polymorphism?
Also
class A
{
public void hello()
{
printf("in A");
}
}
class B extends A
{
public void hello()
{
printf("in B");
}
}
class C extends A
{
public void hello()
{
printf("in C);
}
}
Now if i do
B b=new B();
C c=new C();
A a1=b;
A a2=c;
a1.hello();
a2.hello();
now will a1 use all the members of b those are inherited from A and hence print in B; and similarly for a2?
Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces
Polymorphism is important not only to the derived classes, but to the base classes as well. Anyone using the base class could, in fact, be using an object of the derived class that has been cast to the base class type.
In other words you have polymorphism every time using a base class you can have “different” behaviors.Overriding is just a way to have polymorphism but think about interface you don’t override any methods but you can still have polymorphism (implementing the interface in different ways)