• 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

inheritance

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What is the result?
A. Compilation will fail.
B. Compilation succeeds and no exceptions are thrown.
C. Compilation succeeds but an exception is thrown at line 5 in ClassTest.java.
D. Compilation succeeds but an exception is thrown at line 6 in ClassTest.java.
Answer: B


But var has private accessibility so how's it available in Test?
Thanks
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable var has private accessibility, true, but the method getVar does not. That method is accessible to the subclass.

From the subclass, you aren't accessing the private member directly. Rather, you're invoking a method of the superclass which is then free to access the private member because it is executing within the parent class, where the private member is declared.

This is actually done quite often with "getter and setter" methods.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey is right,

a class with private variables and public getter and setter methods to access them are usually called beans. they also have a property to have a default no-arg constructor.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Respected Friend,
the variable is pricate but the method returning the value of this variable is not private so it can be accessed by the sub class and since the method returns the private variable therefore the variable can also be accessed in the sub class.

Taqi Raza,
taqi10@yahoo.com
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be precise, the method getVar() has default accessibility, which means it can be accessed by any class in the same package. The fact that Test is a subclass is incidental. If Test were in a different package it wouldn't be able to call getVar() unless getVar() were protected or public.

Robert
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Robert Miller:
Just to be precise, the method getVar() has default accessibility, which means it can be accessed by any class in the same package. The fact that Test is a subclass is incidental. If Test were in a different package it wouldn't be able to call getVar() unless getVar() were protected or public.

Robert



Ummm...default members are also inherited, just like protected and public members. It doesn't matter what packages these classes are in - the fact that one is a subclass of the other means that the getVar method will always be accessible to the subclass.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Default access is across the package, not the inheritance. The following comes from the language specification

If none of the access modifiers public, protected, or private are specified, a class member or constructor is accessible throughout the package that contains the declaration of the class in which the class member is declared, but the class member or constructor is not accessible in any other package



Les
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Les Hayden:
Default access is across the package, not the inheritance. The following comes from the language specification



Les



My bad - I sometimes slip back into C++ mode. In Java, default access is more restrictive than protected access. Sorry about that. Les is correct, default members are not accessible outside the package.
 
reply
    Bookmark Topic Watch Topic
  • New Topic