aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inheritance" Watch "inheritance" New topic
Author

inheritance

Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100


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
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Satya Lekkala
Greenhorn

Joined: Jul 02, 2004
Posts: 9
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.


$atya<br />SCJP 1.4
Raza Taqi
Greenhorn

Joined: Jul 06, 2004
Posts: 11
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
Robert Miller
Ranch Hand

Joined: Jun 18, 2004
Posts: 56
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

Joined: Dec 20, 2001
Posts: 3271
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.
Les Hayden
Greenhorn

Joined: Jun 19, 2002
Posts: 28
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


Les Hayden
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheritance