Could anyone pl.clarify my doubts..??
class base4
{
int i=99;
public void amethod()
{
System.out.println("Base method()");
}
base4()
{
amethod();
}
}
public class derived extends base4
{
int i=-1;
public static void main(
String args[])
{
derived mine = new derived();
mine.amethod();
System.out.println(mine.i);
}
public void amethod()
{
System.out.println("Derived method");
}
}
The above code when compiled and run outputs
Derived method
Derived method
-1
The method call amethod in the Base class construtor seems to bind with the derived class amethod.
But The following code when compiled and run outputs
parent method2
parent method1
eventhough the object is referring to the child2 object and method1 is overriddden in teh child2 class why
parent method1 is executed instead of child2 method1..Pl clarify..
class Parent
{
private void method1()
{
System.out.println("parent method1");
}
public void method2()
{
System.out.println("parents method2");
method1();
}
}
public class child2 extends Parent
{
public void method1()
{
System.out.println("childs methid");
}
public static void main(String args[])
{
Parent p=new child2();
p.method2();
}
}