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 Programmer Certification (SCJP/OCPJP) and the fly likes jtips 1, q26 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "jtips 1, q26" Watch "jtips 1, q26" New topic
Author

jtips 1, q26

Cristian Negresco
Ranch Hand

Joined: Sep 15, 2001
Posts: 182
Hello,
See the following code:

public class Superclass {
public static void main(String[] args) {
System.out.println(new Subclass().methodA());
}
Superclass() {
System.out.println("SuperClass Constructor Executed");
}
private int methodB() {
System.out.println("methodB in Superclass");
return 9;
}
int methodA() {
System.out.println("methodA in Superclass");
return methodB();
}
}

class Subclass extends Superclass
{
Subclass()
{
System.out.println("SubClass Constructor Executed");
}
/*protected int methodB()
{
System.out.println("methodB in Subclass");
return 1;
}*/
}

and check the result:
SuperClass Constructor Executed
SubClass Constructor Executed
methodA in Superclass
methodB in Superclass
9

How the private method B is visible from the Subclass?
I just do not see the logic, what do I miss?
Thanks,
..Cristian
Cristian Negresco
Ranch Hand

Joined: Sep 15, 2001
Posts: 182
Sorry,
I have simplified the code, now it is clearer:
public class Superclass {
public static void main(String[] args) {
Subclass s = new Subclass();
s.methodA();
}
private void methodB() {
System.out.println("methodB in Superclass");
}
void methodA() {
System.out.println("methodA in Superclass");
methodB();
}
}
class Subclass extends Superclass
{
}
methodA in Superclass
methodB in Superclass
My question is how method B is visible for an instance of class Subclass.
Thanks in advance,
Cristian
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
private means private to the class and not to the instance. Since the instance of Subclass is created within Superclass it can access methodB
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Cristian Negresco
Ranch Hand

Joined: Sep 15, 2001
Posts: 182
Hi,

I am not sure that I get your answer,
I tried another variant too:

class Superclass {

private void methodB() {
System.out.println("methodB in Superclass");
}
void methodA() {
System.out.println("methodA in Superclass");
methodB();
}
}
class Subclass extends Superclass
{
}
public class Test {
public static void main(String args[])
{
Subclass s = new Subclass();
s.methodA();
}
}
I am still unclear, now the instance of Subclass is not created in Superclass, I think I still miss something.
Thanks,
Cristian
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
you call methodA from within Subclass, that's ok since methodA has default accessibility. Then methodA calls methodB, that's ok too since methodA and methodB are in the same class.
What you cannot do is call directly methodB from within Subclass
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jtips 1, q26
 
Similar Threads
method call
Jtips - mockexam #1
Overriding
access modifiers
problem regarding constructor