Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Inheritance

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}

}
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone explain in detail
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check this
https://coderanch.com/t/265549/java-programmer-SCJP/certification/Data-Hiding
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is because the method test is presnt only in your class mum and it gets inherited.....
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic