简体   繁体   中英

how to modify parent class variable with the child class and use in another child class in java

I have a class Test which has a variable count as 0. Test class was extended by Classes A and B. I set the count in A as 50. Now i want to access the count in B, which should return the value as 50, But i'm getting as 0 in B. I'm new to java and i dont know how it works :( can any one help me how to implement this? :(

Public Class Extend { 
    public int count =0; 
} 


Public class a extends Extend { 
    this.count = 50; 
 }  

public class b extends Extend { 
   system.out.println ( " count is " + count); 
} 

Each instance of Extend (including instances of any class that extends it) gets its own copy of count . The values are not shared between instances.

You can change b to extend class a instead of class Extend . Then any instance of b would see whatever initialization was done when the a constructor executed.

If you want the same count to be shared by all instances of a class or its subclasses, then you need to make the field static :

public class Extend {
    public static int count = 0;
}

In subclasses, you then would refer to the field name without this. as a qualifier.

Note that the code you posted is not legal Java. The first two classes should be public class , not Public Class or Public class . Also, you cannot have code like this.count = 50; outside a method or initializer block. I'm assuming that this is not your real code.

Try starting out with this website for Java inheritance.

You are getting 0 as a value because in class B you are not setting the value of class B's local variable count . Therefore the value of count you are trying to read gets inherited from the Extend superclass.

You are however setting the value of count in class A to 50, but that variable is local only to class A and cannot be accessed by class B in this relationship.

in class b you are getting the count from Extend , that's why it is 0 . You could do:

class b extends a {

    System.out.println("count: " + count);

 }
Public Class Extend { 
    public int count = 0; 
} 

Public class A extends Extend { 
    this.count = 50; 
}  

public class B extends Extend {
   public void showMessage() {
       //this will print 0 if you don't change count value in your main method
       system.out.println ("count is " + this.count); 
   } 
} 

try to use this :

Public Class Extend { 
    public int count = 0; 
}

Public class A extends Extend { 
    this.count = 50; 
}  

public class B extends Extend { 
   private Extend myExtend;

   public B(Extend myExtend) {
       this.myExtend = myExtend;
   }

   public void showMessage() {
       system.out.println (" count is " + this.myExtend.count);
   }  
} 

now in your main class you can use this

Extend eA = new A();

Extend eB = new B(eA);

eb.showMessage(); //this will print 50

You can make it static . static fields are shared between instances; and if you change one static variable in child class, the father , and thus, the instances of all child class created afterwards will share the changed value.

Consider these classes:

Father.java

class Father {                              
    protected static int i;                 
    public Father() {                       
        System.out.println("Father: " + i); 
    }                                       
}

Child.java:

class Child extends Father {
    public Child() {
        i = 4;
        System.out.println("Child: i: " + i);
        System.out.println("Child: this.i: " + this.i);
        System.out.println("Child: super i: " + super.i);

    }
}

Daughter.java:

public class Daughter extends Father {
    public Daughter() {
        System.out.println("Daughter: i: " + Daughter.i);
    }
}

Main.java:

class Main {
    public static void main(String[] args) {
        System.out.println("First daughter");
        Daughter daughter = new Daughter();
        Father father = new Father();
        Child child = new Child();
        System.out.println("Second daughter");
        Daughter daughter2 = new Daughter();
    }
}

Output:

First daughter         
Father: 0              
Daughter: i: 0         
Father: 0              
Father: 0              
Child: i: 4            
Child: this.i: 4       
Child: super i: 4      
Second daughter        
Father: 4              
Daughter: i: 4        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM