Reputation: 161
when 2 classes are created as super and subclass. what would be working differently if one of the objects was declared with a different reference?
class SubClass extends SuperClass
SubClass s1 = new SubClass();
SuperClass s2 = new SubClass();
compare to the next one
class SubClass extends SuperClass
SubClass s1 = new SubClass();
SubClass s2 = new SubClass();
Upvotes: 2
Views: 118
Reputation: 483
invoking static method gonna work differently. check the example.
public class BaseClass {
public void compileSomeThing(){
System.out.println("Base Method ");
}
public static void doSomething(){
System.out.println("Base static method");
}
}
base class
public class SubClass extends BaseClass{
public void compileSomeThing(){
System.out.println("Sub Method" );
}
public static void doSomething(){
System.out.println("Sub static method");
}
}
sub class. and main class:
public static void main(String[]args){
BaseClass bs = new SubClass();
SubClass bs1 = new SubClass();
bs.compileSomeThing();
bs.doSomething();
System.out.println("..........");
bs1.compileSomeThing();
bs1.doSomething();
}
result of compilation:
Sub Method
Base static method
..........
Sub Method
Sub static method
Upvotes: 1
Reputation: 2668
Benefit of polymorphism is in ability to have multiple implementations. As you can see from the code below that function letsHear
accepts any type of Animal
. So this method is agnostic, it doesn't care what type of animal are you passing.
abstract class Animal {
abstract String talk();
}
class Cat extends Animal {
String talk() {
return "Meow!";
}
}
class Dog extends Animal {
String talk() {
return "Woof!";
}
}
void letsHear(final Animal a) {
println(a.talk());
}
int main() {
letsHear(new Cat());
letsHear(new Dog());
}
Concrete implementation could be assigned to it's parent.
Animal cat = new Cat();
Animal dog = new Dog();
If the method letsHear
from the example above would accept only Cat
then you would be required to have another method which accepts Dog
.
Upvotes: 2
Reputation: 153
class SubClass extends SuperClass
1) SubClass s1 = new SubClass();
2) SuperClass s2 = new SubClass();
Explanation of first one:-
s1 is the refernce of subclass and holds a object of sub class, so it will call the method of subclass as well as super class because we extends the superclass method with subclass method so superclass method will automatically available to subclass.we can call both super class as well as sub class method with the help of s1.
Explanation of second one:-
s2 is the refernce of Superclass and holds a object of sub class,so here two case we have to consider i) Method overloading:-method overloading is handle at compile time so it will always call the method of superclass
ii)Method overriding:-method overriding is handle at runtime so it always call the method of subclass. the concept came from the polymorphism.
SubClass s1 = new SubClass();
SubClass s2 = new SubClass();
both s1 and s2 are reference of subclass and holding the object of subclass so in both case it will call the subclass object.
Upvotes: 2
Reputation: 121
Try always to use the highest class you can. It gives you more flexibility by leaving you the possibility to switch the subclass later on. If your are just using it localy, it isn't that much of a great deal and you could probably change the whole definition without any side effects, but even it would be better to use the SuperClass. It would indicate that in this context you just need the functionality defined in the SuperClass and that's the important part.
It's much more important inside the parameters. Imagine the java standard library developer would've defined the Collections methods to take SubClasses like ArrayList. Not only much more code would be needed to take on every SubClass of the Collection interface, it would be incomplete once one creates a new subclass.
Summary: Always ask your self which functionality you really need to accomplish your operation and which superclass is needed for that. Is it List? Collection? Maybe Iteratable? Take the most abstract one.
Upvotes: 1
Reputation: 3992
SubClass s1 = new SubClass(); SuperClass s2 = new SubClass();
On s2
you can only invoke methods declared in SuperClass
.
On s1
you can invoke methods declared in SuperClass
as well as methods declared in SubClass
.
Upvotes: -1