This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance

 
Teo Framoe
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • 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
}

}
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Pankaja Shinde
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone explain in detail
 
vijaya bharath
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this
http://www.coderanch.com/t/265549/java-programmer-SCJP/certification/Data-Hiding
 
Srikanth Iyer
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because the method test is presnt only in your class mum and it gets inherited.....
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic