• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jtips 1, q26

 
Cristian Negresco
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Cristian Negresco
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic