aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

Teo Framoe
Greenhorn

Joined: Jan 05, 2007
Posts: 13
Why "Mum" in the 2. an 3. output line?
class Mom {
String s ="Mom";
void test() {
System.out.println(s);
}
void test2() {
System.out.println(s);
}
}

class Child extends Mom {
String s = "Child";
void test2() {
System.out.println(s);
}
}

class Test {
void test(Mom m) {
System.out.println("Mom");
}
void test(Child b) {
System.out.println("Child");
}

public static void main(String[] s) {
Test t = new Test();
Mom m = new Mom();
Child b = new Child();
Mom mb = new Child();

System.out.println("-Inherid-");
m.test(); //Mom
b.test(); //Mom Why1?
mb.test(); //Mom Why2?

System.out.println("-Overidden-");
m.test2(); //Mom
b.test2(); //Child
mb.test2(); //Child - Overridden : Runtrime

System.out.println("-Overloaded-");
t.test(m); //Mom
t.test(b); //Child
t.test(mb); //Mom - Overloaded : Compile time
}

}
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Its because the test method is only in your Base class i.e Mom not in your Child one. So as cause of inheritance "mom" gets printed at both method calls as child have inherited those two methods.

Thanks
[ October 03, 2007: Message edited by: pranav bhatt ]
Pankaja Shinde
Ranch Hand

Joined: Sep 15, 2006
Posts: 87
can anyone explain in detail
vijaya bharath
Ranch Hand

Joined: Jun 10, 2005
Posts: 66
check this
http://www.coderanch.com/t/265549/java-programmer-SCJP/certification/Data-Hiding


Regards,<br />Vijaya Bharath.<br />SCJP1.4 <br />SCWCD5.0
Srikanth Iyer
Ranch Hand

Joined: Apr 30, 2007
Posts: 52
That is because the method test is presnt only in your class mum and it gets inherited.....
 
wood burning stoves
 
subject: Inheritance