• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Access modifiers

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which will declare a method that is available to all member of same package and can be refferenced without instance of class?
public/protected/private/abstract/no access specifier needed
Can any one explain this who has good understanding of access modfiers. Also kly explain this question....

Thanx
Venky
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's consider like this:
Member of a package are top level classes and interfaces only. (non-static inner classes are not members of a package)
Now before a method is accessible, its class must be accessible. A class, which is declared with any one of the access modifiers- public, protected, or default access, is accessible throughout the package in which this class is declared. Therefore a method, which is declared within this class using any one of the access modifiers- public, protected, or default access is accessible through out this package. [The modifier private is ruled out]
But to be referenced without an instance of a class, the method must also have a static modifier(FYI: static is not an access modifier but is an class-member modifier).
I see "static" modifier is missing in your question. Without this modifier, I think that you cannot call a method from outside this class without an instance of this class.
This is what I think. Please let me know if I overlooked something.

thanks,
Rajesh


 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Venky,
Are you sure that the question is correct?
If it correct, then the answer should be abstract.
This is because, any abstract method can only be declared in a class which is declared abstract explicitly.
Now, that we have an abstract class in a package and we want to call the method, we will need to inherit the class. When we inherit the class, we don't need to make an instance of the class. If we want to call a abstract method declared, we will first have to define a body for the method. Then we can call the method in the class.
An abstract class can also contain methods which already have a body definition. IN this case, you can also call those methods by their simple name, as they have been inherited.
Hope it helps.
Niraj
 
Venkat Ramanan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Neeraj's reply. It should be abstract only. Thanks neeraj, for explaining it neatly.
Rgds
Venky
 
Tell me how it all turns out. Here is a tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!