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.

Similar Messages

  • 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).

  • 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.

  • Public access specifier in interface

    I have a slight problem with my code below. I created a file and named it ClosedCurve.java* and wrote all code into the file.
    My code below wont be compiled. unless I add an access specifier public . and I do not understand why i need to do that. I got an error that say "Cannot reduce the visibility of the inherited method from ClosedCurve"
    package classs.and.objects;
    public interface ClosedCurve {
        double computeArea();
    class Triangle implements ClosedCurve {
        double computeArea() // I need to change to public double computeArea()
            return 0.0;
    }

    The default specifier within the interface (in fact the only specifier within the interface) is public. The default specifier (when none are noted) for classes is package-private which is less visible than public.
    Please have a look here: [Controlling Access to Members of a Class|http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html]

  • 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

  • Question: about accessing Nik collection through Lightroom

    I have recently purchased and installed Nik collection. I have no problem reaching the plug-ins in Photoshopbut can't in Lightroom (except for HDR)

    Thanks!  Technology senses my fear and always makes it difficult for me! Ha! I can use the Nik programs so if I have to, I'll work on them there.
    Date: Sat, 13 Apr 2013 16:27:45 -0700
    From: [email protected]
    To: [email protected]
    Subject: question: about accessing Nik collection through Lightroom
        Re: question: about accessing Nik collection through Lightroom
        created by trihelm2 in Photoshop Lightroom - View the full discussion
    I suspect there are potential issues on some systems with the Google "combined" version, when I installed it I had recieved a copy of the Google complete version free because of my purchase history. I was advised that I could install over the Individual Plugins I had. I never got as far as trying it with Lightroom, only in Elements. The plugins appeared but the load times were abysmal and I mean realy bad , far longer than the Nik individual Installs, so I dumped it and reverted to the originals. Pity cos I wanted the Sharpen Plugin that I have'nt got. After reverting all was back to normal The Nik individual Plugins load very quickly. It was this that convinced me there is a difference in the Google setup.There are people on here with far more knowledge than me, hopefully one of them can help you, alternatively you might try asking Google for help with this.
         Please note that the Adobe Forums do not accept email attachments. If you want to embed a screen image in your message please visit the thread in the forum to embed the image at http://forums.adobe.com/message/5232723#5232723
         Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page: http://forums.adobe.com/message/5232723#5232723
         To unsubscribe from this thread, please visit the message page at http://forums.adobe.com/message/5232723#5232723. In the Actions box on the right, click the Stop Email Notifications link.
         Start a new discussion in Photoshop Lightroom by email or at Adobe Community
      For more information about maintaining your forum email notifications please go to http://forums.adobe.com/message/2936746#2936746.

  • "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.
    ~

  • 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

Maybe you are looking for