Okay, when you extend a class, like the name says it, it will extend to the allready present code.
The class being extended is ParentClass
class ParentClass // extends java.lang.Object
{
public void parentMethod()
{
System.out.println("Printing from the parent.");
}
}
If you create a class ChildOne that extends ParentClass, ChildOne will take over all that code:
ZekeDragon created this ChildOne class:
class ChildOne [B]extends ParentClass[/B]
{
public void childOneMethod()
{
System.out.println("Printing from child one.");
}
}
It extends ParentClass, so you could actually see it as:
class ChildOne [strike][B][COLOR="red"]extends ParentClass[/COLOR][/B][/strike]
{
public void childOneMethod()
{
System.out.println("Printing from child one.");
}
[B][COLOR="blue"]public void parentMethod()
{
System.out.println("Printing from the parent.");
}[/COLOR][/B]
}
That makes it pretty normal that if you do one.parentMethod(), it will display "printing from the parent"
ChildOne one = new ChildOne();
one.parentMethod();
OUTPUT:
Printing from the parent
And then we got ChildTwo, defined as:
class ChildTwo extends ParentClass
{
public void childTwoMethod()
{
System.out.println("Printing from child two.");
}
public void parentMethod()
{
childTwoMethod();
}
}
Which, as ZekeDragon mentioned has 'Overridden' the parentMethod(). It's common to also have an annotation there to make it more clear, like followed:
class ChildTwo extends ParentClass
{
public void childTwoMethod()
{
System.out.println("Printing from child two.");
}
[B]@Override[/B]
public void parentMethod()
{
childTwoMethod();
}
}
That annotation doesn't really change anything, it just makes it more clear for the user so he/she knows that method is overridden.
Now, just like ChildOne, this ChildTwo extends, takes over, all the ParentClass' code, So you could see it as:
class ChildTwo [B][COLOR="red"][strike]extends ParentClass[/strike][/COLOR][/B]
{
public void childTwoMethod()
{
System.out.println("Printing from child two.");
}
public void parentMethod()
{
childTwoMethod();
}
[B][COLOR="blue"]public void parentMethod()
{
System.out.println("Printing from the parent.");
}[/COLOR][/B]
}
Now, you see there is a conflict there, ChildTwo now has 2 methods with the same name and parameters: parentMethod().
Because parentMethod is declared in both classes, Java will use the "lowest" (okay, can't find a better word than 'lowest') one, and ignore the parentMethod from ParentClass.
So actually you can see "overriding" as "overwriting".
In the end ChildTwo looks like:
class ChildTwo [B][COLOR="red"][strike]extends ParentClass[/strike][/COLOR][/B]
{
public void childTwoMethod()
{
System.out.println("Printing from child two.");
}
public void parentMethod()
{
childTwoMethod();
}
}
With only his own parentMethod.
I hope it nows makes more sence that
ChildTwo two = new ChildTwo();
two .parentMethod();
outputs:
OUTPUT:
Printing from child two.
-------------------------------------------------------------------------
Now, you see that when I created my objects, I used the same class both Left and Right Where ZekeDragon used ParentClass left.
me:
ChildOne one = new ChildOne();
ZekeDragon:
ParentClass one = new ChildOne();
And both code worked.
The difference lies in the availability of the methods.
While me and ZekeDragon's 'one' object, both are actually ChildOne objects. ZekeDragon can only use it as if it was an object of ParentClass.
Which means, that I would have access to
public void childOneMethod()
{
System.out.println("Printing from child one.");
}
And ZekeDragon can't access that one, because it's declared as a ParentClass object.
ChildOne one = new ChildOne();
ParentClass one2 = new ChildOn();
one.childOneMethod(); //No problem here
one2.childOneMethod(); //Big problem here