Is there a way to override class variables in Java?

后端 未结 17 1062
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 09:49
class Dad
{
    protected static String me = \"dad\";

    public void printMe()
    {
        System.out.println(me);
    }
}

class Son extends Dad
{
    protected         


        
相关标签:
17条回答
  • 2020-11-22 10:28

    Variables don't take part in overrinding. Only methods do. A method call is resolved at runtime, that is, the decision to call a method is taken at runtime, but the variables are decided at compile time only. Hence that variable is called whose reference is used for calling and not of the runtime object.

    Take a look at following snippet:

    package com.demo;
    
    class Bike {
      int max_speed = 90;
      public void disp_speed() {
        System.out.println("Inside bike");
     }
    }
    
    public class Honda_bikes extends Bike {
      int max_speed = 150;
      public void disp_speed() {
        System.out.println("Inside Honda");
    }
    
    public static void main(String[] args) {
        Honda_bikes obj1 = new Honda_bikes();
        Bike obj2 = new Honda_bikes();
        Bike obj3 = new Bike();
    
        obj1.disp_speed();
        obj2.disp_speed();
        obj3.disp_speed();
    
        System.out.println("Max_Speed = " + obj1.max_speed);
        System.out.println("Max_Speed = " + obj2.max_speed);
        System.out.println("Max_Speed = " + obj3.max_speed);
      }
    
    }
    

    When you run the code, console will show:

    Inside Honda
    Inside Honda
    Inside bike
    
    Max_Speed = 150
    Max_Speed = 90
    Max_Speed = 90
    
    0 讨论(0)
  • 2020-11-22 10:30

    Of course using private attributes, and getters and setters would be the recommended thing to do, but I tested the following, and it works... See the comment in the code

    class Dad
    {
        protected static String me = "dad";
    
        public void printMe()
        {
            System.out.println(me);
        }
    }
    
    class Son extends Dad
    {
        protected static String me = "son";
    
        /* 
        Adding Method printMe() to this class, outputs son 
        even though Attribute me from class Dad can apparently not be overridden
        */
    
        public void printMe()
        {
            System.out.println(me);
        }
    }
    
    class Tester
    {
        public static void main(String[] arg)
        {
            new Son().printMe();
        }
    }
    

    Sooo ... did I just redefine the rules of inheritance or did I put Oracle into a tricky situation ? To me, protected static String me is clearly overridden, as you can see when you execute this program. Also, it does not make any sense to me why attributes should not be overridable.

    0 讨论(0)
  • 2020-11-22 10:34

    Just Call super.variable in sub class constructor

    public abstract class Beverage {
    
    int cost;
    
    
    int getCost() {
    
        return cost;
    
    }
    
    }`
    
    public class Coffee extends Beverage {
    
    
    int cost = 10;
    Coffee(){
        super.cost = cost;
    }
    
    
    }`
    
    public class Driver {
    
    public static void main(String[] args) {
    
        Beverage coffee = new Coffee();
    
        System.out.println(coffee.getCost());
    
    }
    
    }
    

    Output is 10.

    0 讨论(0)
  • 2020-11-22 10:35

    You cannot override variables in a class. You can override only methods. You should keep the variables private otherwise you can get a lot of problems.

    0 讨论(0)
  • 2020-11-22 10:37

    Why would you want to override variables when you could easily reassign them in the subClasses.

    I follow this pattern to work around the language design. Assume a case where you have a weighty service class in your framework which needs be used in different flavours in multiple derived applications.In that case , the best way to configure the super class logic is by reassigning its 'defining' variables.

    public interface ExtensibleService{
    void init();
    }
    
    public class WeightyLogicService implements ExtensibleService{
        private String directoryPath="c:\hello";
    
        public void doLogic(){
             //never forget to call init() before invocation or build safeguards
             init();
           //some logic goes here
       }
    
       public void init(){}    
    
    }
    
    public class WeightyLogicService_myAdaptation extends WeightyLogicService {
       @Override
       public void init(){
        directoryPath="c:\my_hello";
       }
    
    }
    
    0 讨论(0)
提交回复
热议问题