wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Access modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access modifiers " Watch "Access modifiers " New topic
Author

Access modifiers

Preety Narashimhan
Greenhorn

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

Joined: Mar 22, 2002
Posts: 1055
1. You can have a protected inner class, if I understood you correctly...
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
2.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
3. Static methods and final methods are not equivalent.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Apr 26, 2001
Posts: 133
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

Joined: Sep 04, 2002
Posts: 18
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

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

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Jun 15, 2011
Posts: 87

@Preety Narashimhan

Point no: 2
static methods can not be overidden instead they can be redefined in sub class.


Aniket Kulkarni
Oracle Certified Professional, Java SE 6 Programmer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access modifiers