• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Access modifiers

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Preety Narashimhan

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