File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance" Watch "Inheritance" New topic


Teo Framoe

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

class Child extends Mom {
String s = "Child";
void test2() {

class Test {
void test(Mom m) {
void test(Child b) {

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

m.test(); //Mom
b.test(); //Mom Why1?
mb.test(); //Mom Why2?

m.test2(); //Mom
b.test2(); //Child
mb.test2(); //Child - Overridden : Runtrime

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.

[ 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

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.....
I agree. Here's the link:
subject: Inheritance
It's not a secret anymore!