This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes inhertitance through multiple packages - not able to analyze the output- Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inhertitance through multiple packages - not able to analyze the output-" Watch "inhertitance through multiple packages - not able to analyze the output-" New topic
Author

inhertitance through multiple packages - not able to analyze the output-

ajithkumar punnakula
Greenhorn

Joined: Dec 27, 2008
Posts: 8
Hi just started learning java so my naming conventions may not be right.. I'm sorry for that and please correct me ...

I have 4 classes (code given below & also attached the .java files )
3 public classes C1, C2, C3 in package pkg1
1 public class C2 in package pkg2

The classes C1, C2, C3 have a method named met1() with default access.


*************
package pkg1;

public class C1 {

public int a = 0;

void met1()
{
a=10;
System.out.println("a in c1 is"+a);
}
}
***************
package pkg2;
import pkg1.C1;

public class C2 extends C1 {

void met1()
{
a = 20;
System.out.println("a in c2 is"+a);
}

}
***************
package pkg1;
import pkg2.C2;

public class C3 extends C2{

void met1()
{
a = 30;
System.out.println("a in c3 is"+a);
}

}
**************
package pkg1;

import pkg2.C2;

public class C4 {


public static void main(String[] args)
{

C1 o1 = new C3();
o1.met1();

C1 o2 = new C2();
o2.met1();

}
}
**************

output i got after running C4 -
a in c3 is30
a in c1 is10


Q1- I believe the met1() in all the three classes are different.
please correct me if i'm wrong in my understanding

Q2- in the case of above statement being true
i thought the output should be either
a in c3 is30
a in c2 is20

as far as i understood java till now - at runtime in polymorphism the actual object method runs not reference's method

(or )
a in c1 is10
a in c1 is10

as i thought may be because all the three met1() methods are different C1 's met1() should run

please do correct me where ever i understood java wrongly...

Thanks in advance
ajith
(hurray i posted my first Q in java ranch)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Welcome to the Ranch

Please use the Code button otherwise your code is illegible.
That is a good example of confusing code. You say you have class C2 and then write C4. Writing C1 C2 C3 and C4 is guaranteed to confuse people. Also some bad design, with public fields.

The real problem is that the method in C2 does not override that in C1. You can try adding the @Override annotation to C2#met1. The explanation is in your own post . . .
Kuldip Shetty
Ranch Hand

Joined: Jan 07, 2010
Posts: 39

In all classes met1() function uses default (package-private) specifier which is visible to the class and its own package. As C1 and C2 classes are under different package, C2.met1() not overiding the function from base class C1.met1(). On invoking o2.met1(); it invokes C1.met1() function.



Kuldip
ajithkumar punnakula
Greenhorn

Joined: Dec 27, 2008
Posts: 8
hi
@Kuldip Shetty
thanks for your time..thanks

@ Campbell Ritchie
hey thanks for your time..
Sorry for confusing C2 and C4....
Will use code button next time...did not know about the code button while i was posting my Q.
Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
You're welcome
Kuldip Shetty
Ranch Hand

Joined: Jan 07, 2010
Posts: 39

You're welcome
 
 
subject: inhertitance through multiple packages - not able to analyze the output-
 
Similar Threads
how many objects will be eligible for garbage collection
Garbage collection doubt
inheritance
inheritance and polymorphism across packages
No output classes