C++: access-specifiers

I'm currently taking a course in C++ programming and we are covering classes and objects. We had a discussion last night and could not come up with a good example of why you would ever need to use specifically, the private access-specifier, along with protected. Would this ever be useful and why? Please give an example.

Presumably, as a very trivial example, you might not
want a very particular error announcement to be
accessible outside the actual class that it concerns.
Start doing that kind of thing and who knows where
you'll end up; you might get the kind of Error
Messages of Dubious Provenance™ emitted by The
Flagship Product™ of That Big Famous Software
Company™. I mean, if the subclass is going to take on
responsibility for detecting and reporting problems
with its parent, think of all the other b.s. it's
going to have to keep track of as well. You might as
well just write straight C code.
But along with the free advice, you get this bit of
nagging: It's really unfair to yourself and your
mates in the discussion group not to explore
open-ended questions like this on your own. Even
coming up with an off-target answer after thinking
about it for awhile is better than having an answer
handed to you. In the present context, Please give
an example sounds suspiciously like asking for
help with homework. Doing that online is VERY poor
form.
Mr.Stein,
Your quick response is appreciated but the online scolding is not. This discussion was trivial and had nothing to do with an actual assignment. The instructor as well as my mates could not think of a reason why this would be done. Exploring this open ended question on my own would not only take time I don't have but also produce VERY poor design considering you should know what your code is doing before you sit down to type. I consider this forum a resource available for things such as this. I'm sure google could have pulled up actual code but I wanted interaction with coders such as yourself. Once again, your response is appreciated.

Similar Messages

  • Access specifiers for interface methods

    When we implement the interface ,we have to specify the implementing method access specifier to be "PUBLIC" ,but not "PROTECTED" or "PRIVATE".
    Compiler is giving an error -- attempting to assign weaker access privileges ,when we specify protected.
    what is the internal reason for this?Why we shouldnt make the access weaker in the implementing class.
    Can any one help me on this.Your help is highly appreciated.

    When we implement the interface ,we have to specify
    the implementing method access specifier to be
    "PUBLIC" ,but not "PROTECTED" or "PRIVATE".
    Compiler is giving an error -- attempting to assign
    weaker access privileges ,when we specify protected.
    what is the internal reason for this?There is absolutely no point in having a private interface method. The interface represents a visible abstraction and private methods are never visible so it is a contradiction in terms.
    An interface is intended to represent an abstraction that a user (software) uses. Protected via child/parent represents a usage that is restricted to a child from a parent. The child can already see the parent so there is no point in having an abstraction for the child. And it would probably be unwise to limit a child by such an abstraction.
    Protected via the package and interfaces is more contentious as to why it is not allowed. There are those that argue that this should be allowed so that a package can use interfaces but restrict them to the package. To me this seems like a minor point given that most interfaces will probably represent an abstraction to other packages and not within a single package. This applies specifically to default access as well.

  • Problem with access specifier and static???

    I've a question. I saw a code written for Sun's Java Tutorial. The program had three class methods and all of them start like this:
    static public void methodName1{
    static public void methodName2{
    and
    static public void main(String[] args){
    My question is why the keyword static was used first? Why not access specifier? I mean what's the reason behind it? Is there any difference between them?
    Thanks in advance.
    --DM

    The order doesn't matter. The programmer thought it was a good idea for some reason. I might be tempted to do it if I had static and instance methods with the same name or something like that.
    public static void myMethod() {
    public void myMethod() {
    or
    static public void myMethod() {
    public void myMethod() {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Java inner class access specifiers

    public class MyClass1 {
    public static void main(String argv[]){ }
    /*Modifier at XX */ class MyInner {}
    What modifiers would be legal at XX in the above code?
    1) public
    2) private
    3) static
    4) friend

    Iam a newbie to javaI don't care. I still assume you have a brain and expect you to use it.
    and iam struck with this
    question, iam getting a compile time error for any
    access specifiers mentioned as options, but the
    answers given by my superior are 1,2,3, are
    correct..Then you made a typo or whatever. Make sure you eliminate the problems that are not "illegal access modifier" errors.
    What good is knowing the reply to this if you can't even write an example?

  • Access Specifiers for a class

    Why we can't declare private or protected for a class as access specifier.
    Can you give me an example on it.

    This is the third time someone asked something like this today:
    http://forum.java.sun.com/thread.jspa?threadID=664848 (where someone references your post on JavaRanch).
    http://forum.java.sun.com/thread.jspa?threadID=664790

  • Need new access specifier

    I think there must be another access specifier that is like protected but not friendly, I mean only the supclasses can access that, not the classes in that package.
    Suppose a class that has private or friendly fields and I had to define another field of the same type for its subclass.
    Or I have a class with a protected field that I know I will use it if I want to define a subclass of it. But I don't want to let the other classes in thatpackage modify this field unintentionally.
    The problem is not only for fields. There are situations that I want to permit subclasses to call a method but I don't want other classes in the same package call it.

    there isn't.
    perhaps you need to think more about which classes sit in which packages ?
    maybe a better package structure would give you this control.

  • Regarding Protected access specifier

    Hi ,
    I thought that indeed you could access protected data member from another package IF you are using Inheritance. It makes the data memeber in question "friendly" to the derived class. At least that is my understanding.
    You can not access protected data element directly in different packages. package test1;
    public class Test123 {
    protected int x=42;
    package test2;
    import test1.Test123;
    public class Test234 extends test1.Test123{
         public static void main(String args[]){
              Test123 ob=new Test123();
              System.out.println("x="+ob.x);
    but My question is that ..if u run this program its not compile..Compile time exception is throws saying that the field Test123.x is not visible..but according to protected access specifier this will work...and one thing that if i make field ' x ' of Test123 as public it is working... How it is possible ...can anybody pls help it out.. Thanx in advance...

    This access is provided even to subclasses that reside in a different package from
    the class that owns the protected feature.As you have found this (and similar) can be confusing.
    Have a look at the Java Language Specification (http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.7) "6.6.7 Example: protected Fields, Methods, and Constructors". Can you see the difference between the delta() and delta3d() methods?
    "A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object." As another example of this, the following compiles OKpackage test2;
    import test1.Test123;
    public class Test234 extends test1.Test123 {
        public static void main(String args[]) {
            Test234 ob = new Test234();
            System.out.println("x=" + ob.x);
    }When run it prints "42" - showing that the same d@mn field is being accessed! Like you, I'd appreciate hearing a simple rationale for this (if there is one).

  • "windows cannot access specified device, path or file", adobe reader x

    hi,
    i had an older version of reader (9.something i think) that started making troubles, so i removed it (via control panel) and tried to use reader x instead. but after installing it i tried to run it and got this message titled "c:\programfiles\adobe\reader10.0\eula.exe":
    "windows cannot access specified device, path or file. you may not have the appropriate permitions to access the item".
    after clicking "ok" the program shuts.
    i downloaded this version from adobe's site and registered. i also tried to go back to version 9 but the installation itself wouldn't go.
    i'm using windows xp and downloaded reader using chrome. i deleted all previous adobe reader files from my computer prior to downloading, so it should behave as if this the first time this program was installed on it.
    how can i solve it? (i've already tried going through the whole process again and again)
    thank you for any help.

    I have found out how to solve this issue..
    Took A few days to figure it out and a lot of installs and un-installing
    Install CS6 master collection again and restart your machine- Make sure all anti-virus is disabled
    What you have to do:
    1. Press Start
    2. go under "Abode Master Collection CS6 (it should say its empty)
    3. now left click the folder "Adobe Master Collection" and open it..
    4. it will now come up with all programmes that SHOULD have worked - their thumbnails will not be visible it will look as if its corrupted.
    5. Now this is a long procedure- go on the first shortcut, lefts click and go to properties.
    6. Then select the option that says "Open File Location" it will then take you to its main folder.
    7. Left click the highlighted Programme within the main folder, then go on properties
    8. Go on the security Tab, press "continue" then you go on "Add" then type your account name, then "check name" then tick the box "full control"
    9. Then navigate to the "compatibility" Tab and select "Run this programme as an administrator (if you don't do this it will come up with another error)
    You should now have the thumbnail (picture) for the programme now
    You have to now repeat this for the rest of the programmes/shortcut's within the "Adobe Master Collection" Folder

  • Access specifier

    Hi,
    Can anybody tell me as to why we use the public access specifier in the main method as a standard.I have tried out the other access specifiers in the main method and it works fine.
    [please note i do understand the relevance of each access specifier in other contexts.]
    tubby

    It's supposed to be public because the JVM specification says so, but, yeah, you can use more restrictive access modifiers (that is to say, the compiler will permit more restrictive modifiers). Sun doesn't consider the issue an open bug, but you can read the discussion on the issue here: http://developer.java.sun.com/developer/bugParade/bugs/4252539.html

  • Default access specifier

    HI
    What is the default access specifier in Java?
    Is it default or package-private?
    What is the heirarchy of access specifiers?
    Thanks

    http://www.google.com/search?q=java+access+specifiers

  • What are Access Modifiers and Access Specifiers??

    what are Access Modifiers and Access Specifiers??
    advance thanks

    http://www.google.com/search?q=what+are+Access+Modifiers+and+Access+Specifiers%3F
    Advance welcome.
    ~

  • About access specifiers (inheritance)

    abstract Class A
    abstract public add();
    Class B extends A
    protected add() //this wont work
    //some code here
    The access specifier for add method in A is public ,and in B is protected.This is not allowed by compiler.
    Why cant we make the access weaker in the sub class?What is the drawback in allowing to make the access weaker.

    Because any class that uses class A has the right to expect to use the add() method, since it is public in A. Since B is an A, it must allow some one to use an instance of B as if it were an A.

  • Comparing access specifiers  Protected

    Why protected access specifiers are used ? .
    Where comes the difference of using
    void fun1(int a,intb)
    and
    protected void fun(int a, int b)
    }

    From http://www.rijas.t35.com ,
    Note that I did experiment in a variable as protected
    not in a method
    I did both but saw no diffrence from
    no-access Specifiers and
    protected access specifiers
    I extend it two times and
    used package one time
    but saw no difference .
    I am sure ...U can help me !!!
    package pkg;
       class DatePro
           protected  int yy;
       class DatePro2 extends DatePro
           protected int mm;
    public class Date extends DatePro2
                   int dd;
              public void getDate()
                        System.out.println(dd+" "+mm+" "+yy);
           public  void setDate(int d,int m, int y)
                        dd=d;
                        mm=m;
                        yy=y;
    import pkg.*;
    class PkgCheck1
             void   pkgCall()
                        int a=100,b=200;
                        Date d1=new Date();
                        d1.setDate(10,5,07);
                        d1.getDate();
    class PkgCheck2 extends PkgCheck1
                 public static void main(String args[])
                    PkgCheck2 p1 =new PkgCheck2();
                   p1.pkgCall();
                    System.out.println("Hi  I am Rijas P A "+ " You  create a folder pkg  and place Date.java in it");
                    System.out.println("Place PkgCheck2.java - above that folder compile and run PkgCheck2.java");
    thank you...
    Edited by: omnie on Oct 26, 2007 4:27 PM

  • New to java(need help on access specifier)

    hi! i am new to java.plzzzzz help me i have to make a project on access specifier's i know all theroy.but
    i am unable to understand how i can define all specifiers practicly.i mean in a program.
    thanks.plzzzzzzzz help me

    the most common project i can think of is a payroll system..
    you can have real implementation of all the access specifiers
    good luck

  • My java program runs fine even if i don't specify access specifier of class

    Hi,
    My java program runs fine even if i don't specify access specifier of class as public .
    Then why do they say that atleast one class should be specified as public.
    please help.

    public access specifier is the default access
    specifier
    so if you dont give the access specifier before the
    class name it is not wrong.I think that you are wrong. The default specifier is package or "package-private".
    See here:
    http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
    Message was edited by:
    petes1234

Maybe you are looking for

  • IMac (Late 2006) fan speed is 4600 rpm and nothing will change it

    I just brought my iMac 24" 2.16GHz Late 2006 back from the Apple Store today and the ODD fan is running at 4600 rpm!!! The noise it generates is way too loud. It is comparable to the wind tunnel G4 I replaced. Any thoughts on how to lower the fan spe

  • How to restrict copies printed in PDF?

    Hi, Is there any way to restrict the number of copies of a PDF printed? Is there any way to restrict the printing of a PDF so that it can't be printed after a certain date? Jane

  • Minimizing downtime of prod. db on account of encryption

    Hi, We are in need of encrypting a Oracle 8i database with 170 million records based on 3DES alhorithm.But we cannot afford the downtime of the production database.How to minimize the down time. regards Dilawar.

  • What I have learned about the optical drive and my question is:

    I am used to burning data to a CD or DVD on the PC and never experienced a problem. With an RW disk, I can simply open the disk and add, delete, format, etc. without any problem. It is fairly easy to make the disk as a Data disk which effectively wor

  • Right click doesn't work

    When I try to right click and drag a file or a folder to copy it or make a shortcut in Windows (Windows 7), it doesn't work. I only get a purple line following the mouse-arrow "drawn" on the screen(!) , which disappears when I let go of the button. R