Access a private variable from the superclass (JAVA)

后端 未结 6 2079
终归单人心
终归单人心 2021-02-08 20:19

Ok so I have studied java all semester and thought I had a clear understanding about inheritance and super/sub classes. Today we were given as assignment for making a superclass

相关标签:
6条回答
  • 2021-02-08 20:57

    When you inheritance other class, you cannot access your private attributes directly. So, if you have a class named "A" and other called "B", and make B extends A, B cannot access private attributes of A.

    Think this like a protection. This way, you can write some attributes in class "A" that you dont want others classes access it through inheritance.

    The "B" class can access only public, protected and default attributes directly in "A" class. But if you want to access a private attribute in "A" class for any reasons, you can write a method in "A" to return this attribute.

    public class A{
        private int foo;
        public int getFoo(){
           return this.foo;
        }
    }
    
    public class B extends A{
        public void doSomething(){
           getFoo(); //return the private foo attribute of superclass
        }
    }
    
    0 讨论(0)
  • 2021-02-08 21:00
    package com.action.product;
    
    public class ProductAction{
      public static String strCategoryName;
    
      public String getStrCategoryName() {
        return strCategoryName;
      }
      public void setStrCategoryName(String strCategoryName) {
        this.strCategoryName = strCategoryName;
      }
    
    }
    
    -----------------------------------------------------------------------
    
    package com.DAO.product;
    
    public class ProductDAO extends ProductAction {
    
       @SuppressWarnings("static-access")
       public String addnewProductValidation(){
           System.out.println("======through_name======="+super.strCategoryName);
    
           System.out.println("======through_getters======="+super.getStrCategoryName());
    
       }
    }
    

    Can initialize variables as static. No need to create object

    0 讨论(0)
  • 2021-02-08 21:10

    The part

    Any access to an a variable must be done through protected methods in the sub classes.
    

    ... just means that the subclasses have to call protected methods that are defined in the superclass. Since these methods are protected they can be accessed by the subclasses.

    First you would define a base class like this:

    public class Base {
    
        private int x;  // field is private
    
        protected int getX() {  // define getter
            return x;
        }
    
        protected void setX(int x) {  // define setter
            this.x = x;
        }
    }
    

    Then you would use it in your child class like this:

    class Child extends Base{
    
        void foo() {
            int x = getX(); // we can access the method since it is protected.
            setX(42);  // this works too.
        }
    }
    
    0 讨论(0)
  • 2021-02-08 21:20

    Those are just the constraints of the assignment, not Java itself. You could give the superclass protected data members, and then access those directly from the subclass. However, the professor likely wishes to teach you about how a superclass can protect its data members from direct access by a subclass, and protected methods would be a way to do that.

    0 讨论(0)
  • 2021-02-08 21:21

    Probably the sentence is not worded correctly, from what I understand it makes perfect sense to me :
    1. the superclass has private fields and protected methods to access them
    2. the subclasses access the fields by using that methods.

    0 讨论(0)
  • 2021-02-08 21:23

    You are right in your thinking that, literally speaking, you can't access a superclass's private field. The text of your assignment uses wording which is not 100% strict, but is 100% customary in Java parlance: the so-called "getter" methods, also called "accessor" methods, are seen as "accessing the field", even though, strictly speaking, they merely return the current value of the field—which is definitely not the same as giving access to the field itself. You just need to get used to this (and many more) conventions in the Java jargon.

    0 讨论(0)
提交回复
热议问题