• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jtips 1, q26

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic