Reputation: 141
I'm trying to wrap my head around inheritance in java. So far I understood that if i declare an object in the following fashion: Superclass object = new Subclass()
the created child object is restricted to the methods of the parent object. If I want to access the additional methods of the child i would have to cast to the child.
But why are methods still overridden in the child class.
Here's my example
public class Parent {
public Parent() {
}
public void whoAmI(){
System.out.println("I'm a parent");
}
}
public class Child extends Parent {
public Child() {
}
public void whoAmI(){
System.out.println("I'm a child");
}
public void childMethode() {
System.out.println("Foo");
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Parent> list = new ArrayList<>();
Child c = new Child();
Parent p = new Parent();
Parent pc = new Child();
c.whoAmI();
p.whoAmI();
pc.whoAmI();
// Access child methodess
((Child) pc).childMethode();
list.add(c);
list.add(p);
list.add(pc);
System.out.println(list.size());
}
}
pc.whoAmI()
prints "I'm a child". Why doesn't it print "I'm a parent"?
Upvotes: 3
Views: 310
Reputation: 691635
Let's say you're Apple, and you're selling iPods.
There might be subtle differences in the way the iPods are built between one factory and another one, or between the first release of a model of iPod and the last one, because the process has changed.
You, Apple, know what kind of iPod you build:
IPodWithFirstWayOfBuildingIt ipod = new IPodWithFirstWayOfBuildingIt();
or
IPodWithSecondWayOfBuildingIt ipod = new IPodWithSecondWayOfBuildingIt();
But the end customer (i.e., the rest of the code) doesn't give a shit about the way the iPod is built. What matters for him is that the iPod works and behaves like an iPod (the way it has been shown in the commercials, or in the end user manual, or in the javadoc of the Ipod class). He doesn't ask the store "give me an iPod with the first way of building it". But the fact is that he gets an iPod with either the first way of building it, or the second way of building it. And the fact that he calls it "an iPod" doesn't change how the iPod has been built, hence :
IPod iPod = new IPodWithFirstWayOfBuildingIt();
is the way to say that. I build an iPod, with the first way of building it, but I'm going to use it like any other iPod, without caring how it has been built.
Upvotes: 0
Reputation: 41935
class Person{
public void run(){
//running
}
}
class OneLeggedPerson extends Person{
@Override
public void run(){
//cant run have only one leg
}
}
Person p = new OneLeggedPerson();
p.run(); //cannot as it is actually a one legged person
Hope that makes sense now..
Upvotes: 0
Reputation: 49714
Why is a difficult question to answer, it was a design decision taken by the Java team a long time ago.
They decided that all methods are virtual unless they're explicitly marked as static. In some languages it's the other way around (methods are static by default unless declared virtual), it makes no real difference.
The important thing is to understand how static and virtual methods work. (Or as they are sometimes called in Java: instance and class/static methods.)
Upvotes: 1
Reputation: 200138
To avoid confusion, be sure to understand that the subclass object you create always contains all the methods. Downcasting is a no-op; you just treat the very same object as an instance of the subtype.
Corollary: you can never change the behavior of an object by downcasting. It directly follows from this that you always access the same overriding method and never a method from the superclass.
This kind of behavior allows you to substitute different behaviors for the same declared supertype. It's called polymorphism and is the workhorse of Java programming, and OOP in general. Without it there would be no point in having a class hierarchy in the first place. At least, it would be much, much less useful.
Upvotes: 4
Reputation: 50989
You can imagine inheritance as creating hidden member named super
.
Each object instance in Java has it's own constant type. You don't change it while casting. While casting you just say to Java that you are sure this object is of subtype.
Overrided methods remain overriden, this is Java's ideology. If you know C++, you can say that all functions in Java are virtual.
Upvotes: 1