File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance

 
Angela lewis
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Satya Lekkala
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Raza Taqi
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Robert Miller
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Les Hayden
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic