This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access modifiers

 
Preety Narashimhan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
List of doubts on Access Modifiers

1.Are there no protected classes?? Y not if “protected” signifies that it can be accessed/extended by the sub-classes.
2.Please give me a real example of static classes.
3.Is it possible to equate static methods with final methods??
4.Is it possible to sub-class a private class, when it is not even accessible?
5.Is void a datatype?
6.Consider the following eg:

public class SuperClassA{
protected int superClassVarA;
protected void superClassMethodA(){
System.out.println("In superClassMethodA method");
}
}
public class SubClassB extends SuperClassA{
SuperClassA objRefA = new SubClassB(); //1
SubClassB objRefB = new SubClassB(); //2
void subClassMethodB(){
objRefB.superClassMethodA(); //3
objRefA.superClassVarA; // 4
}
}
Here an object of the SuperClassA(objRefA) is referencing the object of the subclassB. Since the member variable “superClassVarA” is protected in the Superclass A, y is it not available to the SubclassB?
7. When a particular native method returns a class reference then how does java handle such a return value, when it does not even know the definition of hte class(the member variables and its methods)
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You can have a protected inner class, if I understood you correctly...
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3. Static methods and final methods are not equivalent.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4. I don't think I understand you here. If it's not accessible it wouldn't be visible, yes?
5. No
 
Lance Finney
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, Void is a datatype, if you're talking about the Void class. It doesn't do much, but it's used in reflection.
 
Eureka Jana
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
6.Try with this:
public class SuperClassA{
protected int superClassVarA;
protected void superClassMethodA(){
System.out.println("In superClassMethodA method");
}
}
public class SubClassB extends SuperClassA{
public void subClassMethodB(){
SuperClassA objRefA = new SubClassB(); //1
SubClassB objRefB = new SubClassB(); //2
objRefB.superClassMethodA(); //3
objRefA.superClassVarA; // 4
}
}
 
Preety Narashimhan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for the late reply ..was outa station..
hey tx a million all ya folks
so let me get things right..
1.Protected and Static classes need to be ineer classes.Cant they stand on their own?
2.Static methods can be overidden as in
public static void print() { System.out.println("Not Final, Static"); }
could be overidden as follows?
public static void print(String str) { System.out.println("Not Final, Static Overidden? :" + str); }
3.Basically there was a question which of teh following classes can be subclassed, and one of the options was a private class so i was wondering if they can be sub-classes at all?
4.Lance, the Void that u mention is it a Wrapper class?? Since it threw compilation errors when i tried to instantiate a void type variable or pass a void argument.
5.Eureka, the code u provided, has tehm as local objects, but it still on compilation of the SubClassB throws me this compilation error :
SubClassB.java:16: Invalid expression statement.
objRefA.superClassVarA; // 4
^
1 error
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. No
2. No. See the sample code below:

3. Yes. An inner private class can be subclassed. See the sample code below:

4. The Void class is considered a wrapper class. It is not the same as void, of course.
 
Aniket S. Kulkarni
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Preety Narashimhan

Point no: 2
static methods can not be overidden instead they can be redefined in sub class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic