Java inheritance and access modifier (public protected default private)

Posted by buildakicker on Sun, 02 Jan 2022 18:31:38 +0100

Inheritance and access modifier (public protected default private)

In Java, all inheritance is public. That is, all properties and methods in the parent class, no matter what access modifiers are used to modify them, are inherited by the child class.

For the protected modifier, it should be noted that the attributes and methods modified by this keyword are visible in subclasses of different packages, which means that the subclass inherits the attributes and methods modified by protected in the parent class. It does not mean that the object of the parent class can be used to access the properties or methods decorated by a specific access modifier in the parent class. Even if the parent class object is new in the subclasses of different packages, it is impossible to access the protected modified properties and methods of the parent class object. Because it's invisible.

Modifier attribute

  • public

    • Subclasses can be accessed internally, that is, through the subclass member variable this Age access
    • Subclasses can be accessed externally, that is, through the new parent class () Member variable access
    • It can be accessed through the public method of the parent class, such as public int getAge()
  • protected

    • Subclasses can be accessed internally, that is, through the subclass member variable this Age access
    • Subclasses cannot be accessed externally, that is, through the new parent class () Member variable access
    • It can be accessed through the public method of the parent class, such as public int getAge()
    // Different packages
    public class Inner {
        protected int age = 10;
    }
    public class Inner3 extends Inner {
        public static void main(String[] args) {
            Inner3 var = new Inner3();
            System.out.println(var.age);// Can access
            System.out.println(new Inner().age);// No access, which is equivalent to external access, not internal access     
        }
        void fun(){
            System.out.println(this.age);// Can access
        }
    }
    
  • default

    • Subclasses cannot be accessed inside subclasses, that is, the subclass's member variable this Age access
    • Subclasses cannot be accessed externally, that is, through the new parent class () Member variable access
    • It can be accessed through the public method of the parent class. For example, public int getAge() means that the attribute of default is inherited, but it cannot be accessed directly
    // Different packages
    public class Inner {
        int age = 10;
    		public int getAge() {
            return age;
        }
    }
    public class Inner3 extends Inner {
        public static void main(String[] args) {
            Inner3 var = new Inner3();
            System.out.println(var.age);// Not accessible
            System.out.println(new Inner().age);// No access, which is equivalent to external access, not internal access     
    		    System.out.println(var.getAge());// Can access
    		}
        void fun(){
            System.out.println(this.age);// Not accessible
    				System.out.println(this.getAge());// Can access
        }
    }
    
  • private

    • Subclasses cannot be accessed internally, that is, through the subclass member variable this Age access
    • Subclasses cannot be accessed externally, that is, through the new parent class () Member variable access
    • It can be accessed through the public method of the parent class. For example, public int getAge() means that the private attribute is inherited, but it cannot be accessed directly
    // Different packages
    public class Inner {
        private int age = 10;// private
    		public int getAge() {
            return age;
        }
    }
    public class Inner3 extends Inner {
        public static void main(String[] args) {
            Inner3 var = new Inner3();
            System.out.println(var.age);// Not accessible
            System.out.println(new Inner().age);// No access, which is equivalent to external access, not internal access     
    		    System.out.println(var.getAge());// Can access
    		}
        void fun(){
            System.out.println(this.age);// Not accessible
    				System.out.println(this.getAge());// Can access
        }
    }
    

Common methods of modification

  • public
    • Subclasses can access this internally fun()
    • Subclasses can access new parent () externally fun()
    • The method is inherited
    • The subclass can override the method of the parent class
    • However, the modifier of subclass methods can only be public (looseness ≥ public)
  • protected
    • Subclasses can access this internally fun()
    • Subclasses cannot access new parent() externally fun()
    • The method is inherited
    • The subclass can override the method of the parent class
    • However, the modifier of subclass methods can only be public protected (looseness ≥ protected)
  • default
    • Subclasses cannot access this internally fun()
    • Subclasses cannot access new parent() externally fun()
    • The method is inherited, but is invisible inside the subclass
    • The statement that there is no replication method in a natural subclass is equivalent to redefining a method. All four access modifiers can be used
  • private
    • Subclasses cannot access this internally fun()
    • Subclasses cannot access new parent() externally fun()
    • The method is inherited, but is invisible inside the subclass
    • The statement that there is no replication method in a natural subclass is equivalent to redefining a method. All four access modifiers can be used

Modification construction method

  • public
    • Both inside and outside the package can be inherited
    • You can instantiate (new) anywhere
  • protected
    • Both inside and outside the package can be inherited
    • In package instantiation (i.e. new) is allowed, while out of package subclasses cannot be instantiated (i.e. new)
  • default
    • Can inherit in package
    • It can be instantiated inside the package, but not outside the package
  • private
    • Cannot be inherited
    • Cannot be instantiated (i.e. new)

static properties / methods can be inherited

  • The parent class reference points to the child class instance. You can call the child class to override the methods of the parent class and the methods derived from the parent class. You cannot call the methods unique to the child class
  • Static methods in a class cannot be overridden by subclasses (two different methods). Therefore, after the upward transformation, if both parent and child classes have this static method, only the original static method of the parent class can be called