Questions tagged [access-specifier]

The access specifier in an object-oriented language determines how a class restricts access to its members.

The access specifier in an object-oriented language determines how a class restricts access to its members.

The usual access specifiers are private, protected and public.

  • A class member whose access specifier is private can only be used by instances of that class.
  • A class member whose access specifier is protected can only be used by some other classes.
    Usually this means that access to these members is granted only to subclasses, but this is not a hard-and-fast rule. In Java, for example, classes that are in the same package also have access to each other's protected members.
  • A class member whose access specifier is public can be used by all other classes in the program.

You can use this tag for questions about how access to the members of your class is resolved.

242 questions
0
votes
1 answer

How to access a function from an outside class in VB.NET

I'm trying to access a function which belongs to a Class frmamain from another class. please can anyone tell me how i have to achieve this? i'm new to VB.NET. thanks in advance. The code throws the following error "ConvertImagerBnW() is not…
NikRock
  • 15
  • 1
  • 7
0
votes
1 answer

Can't access protected members of a class from a subclass defined in a different package

I am learning Java using the book Java: The Complete Reference. I am currently at chapter 9 and thus just inroduced to packages. On page 187, it says "If you want to allow an element to be seen outside your current package, but only to classes that…
user5818995
0
votes
1 answer

C++'s version of Java's package-protection?

I'm working on the graphics code for a game library in Java. I made package called com.engine.graphics. In this package, I have a lower-level class called VertexArrayObject. This class is used by "client-level" classes that clients will use;…
hacksoi
  • 1,301
  • 13
  • 23
0
votes
1 answer

why if my file name and public class name differs then i get compile error?

public class constprac { public static void main(String args[] ) { consttest class1=new consttest("ria"); class1.showName(); } } **public** class consttest{ String gname; public consttest(String name){`` …
0
votes
1 answer

Java-Access Speficier

Can someone explain this behaviour?Is it a bug or I am missing something obvious? 1)Create 2 packages, say pack1 and pack2 in same directory. 2)In pack1 create a class X package pack1; import pack2.*; public class X { void eat() { …
Akash Mahajan
  • 512
  • 4
  • 16
0
votes
2 answers

In C#, How Can I Use Public Class Members(Methods Or Variables) From Different Assembly

I am working on C# encapsulation from Tutorialspoint.com. And I read this What is the difference between Public, Private, Protected, and Nothing?1 question from Stackoverflow. I read answer and i understood access specifiers in teoric. Now I want…
0
votes
1 answer

where the behavior of private and static method is different from only private method

As for my understanding: When a method is static it is early bind can call with the name of class even before no object is created can call only static member inside it. I never found any other behaviour of static either at compile time or…
TheCurious
  • 593
  • 1
  • 4
  • 29
0
votes
3 answers

Why public methods are there in object class? They can be protected and will keep same behaviour with more private manner

As for java architecture is concern "to keep things as much private as possible".Thats why finalize method is protected in object class. protected void finalize() throws Throwable { } Why is the finalize() method in java.lang.Object…
TheCurious
  • 593
  • 1
  • 4
  • 29
0
votes
0 answers

Access Modifier(Protected Keyword) in Java

Can anyone explain, why can't I access protected variable in a subclass of another package? Testp2, OtherPackage, and Protection2 classes are stored in package p2, while Protection class is stored in p1 package. System.out.println("n_pro =…
Jainam Jhaveri
  • 1,479
  • 1
  • 18
  • 31
0
votes
1 answer

Access Specifier Error(Cannot find Symbol)

C:\Users\jaina_000\Desktop\learn_java\p1>javac Testp1.java Testp1.java:6: error: cannot find symbol Protection ob = new Protection(); ^ symbol: class Protection location: class Testp1 Testp1.java:6: error:…
Jainam Jhaveri
  • 1,479
  • 1
  • 18
  • 31
0
votes
2 answers

Public static member appears to be null inside public static method

I am writing a method that lives outside of main and interacts with a public static member. I initialized the member inside main() and proceed to try and use it inside my method, and the pointer is null: public class Program { … public…
0
votes
1 answer

Is it possible to configure default access spec (package private) to public?

Is there a compiler flag that can make that happen, or is that default "cemented" in the language?
0
votes
1 answer

Why is a protected variable visible at class level?

A protected variable is access to any class within a package and only to a subclass that extends the base class outside the package. Why did Java implemented this in this was i.e default within package instead of private within package? This is…
user892871
  • 1,025
  • 3
  • 13
  • 28
0
votes
1 answer

Abstract and final are access modifiers or access specifiers

abstract final class Outer { } So i was compiling the above code and got obvious error ,but the error was Illegal combination of access modifiers ,but the java doc http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html says there…
user3690061
  • 118
  • 1
  • 3
  • 15
0
votes
2 answers

How to access a protected variable from another package

Here I want to access that protected variable rollno to my another package package2. So guys I have put both programs: So here when I run Check.java it throws an error not defined @ Protected1 the only objective here is to access the protected…
user3522245
  • 5
  • 2
  • 5