Results 1 to 4 of 4
Thread: why the out put is 10
- 01-15-2010, 05:06 AM #1
why the out put is 10
Hi,
Why the output is 10
class Q16
{
public static void main(String args[])
{
A a = new B();
a.callme();
System.out.println(a.r);
}
}
class A
{
void callme()
{
System.out.println("A");
}
int r=10;
}
class B extends A
{
public void callme()
{
System.out.println("B");
}
int r=20;
}
Result be
B
10
-
Tsk, tsk... You've posted more than 30 times here and should know about using code tags.
So this exercise makes you think a bit about the nature of polymorphism in Java -- does Java allow for polymorphism for both methods and fields or just methods alone?
- 01-15-2010, 07:38 AM #3
shows the value in parent class then in subclass
thank for your reply
it will invoke the methods
then
class Earth{
public static void main(String[] args) {
// Earths myEarth = new Earths();
//Planet myPlanet = (Planet)myEarth;
Planet myPlanet=new Earths();
myPlanet.hide();
myPlanet.override();
}
}
class Planet {
public static void hide() {
System.out.println("The hide method in Planet.");
}
public void override() {
System.out.println("The override method in Planet.");
}
}
class Earths extends Planet {
public static void hide() {
System.out.println("The hide method in Earth.");
}
public void override() {
System.out.println("The override method in Earth.");
}
}
same but different name pgm the out put is
The hide method in Planet.
The override method in Earth.
why it shows the value in parent class then in subclass
- 01-15-2010, 09:52 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Bookmarks