i want to use superclass instance variable as arguments in the method of the superclass, after changing the values of instance variable in subclasses

StackOverflow https://stackoverflow.com/questions/22803363

  •  26-06-2023
  •  | 
  •  

Question

i am new to java , i want to declare instance variable in the superclass and then change their values in the subclasses, and after that use those instance variable as arguments in methods of the superclass , but the value of instance variable are not changing . Please explain me how to do this !!!!!!

    abstract class Crop {
boolean yieldFlower;
String ColorOfFlower;
public boolean isUseful;       
public String[] Products1;

public void eat() {
System.out.println("it consumes sunlight , minerals , water etc");
System.out.println(isUseful); 
}

public void theyGrow(){
System.out.println("they grow slowly but steadily");
}

public void theyRemainStill(){
System.out.println("they do not move");
}

public void ReleaseCarbonDioxide() {
System.out.println("they release CarbonDioxide");
}

public abstract void FavourableSeason();


public void UsefulProducts(String[] Products2){
for(String a:Products2){
System.out.println("One of it's useful product is " + a );
} 
}
}

abstract class Kharif extends Crop {
public void FavourableSeason() {
System.out.println("it is grown in rainy season ");
}
}

abstract class Rabi extends Crop {
public void FavourableSeason() {
System.out.println("it is grown in winter season");
}
}

class millets extends Kharif {

boolean YieldFlower =false;

boolean isUseful=true;    //value of isUseful changed to true

String[] Products1={"food","food","food"}; //values of arrays defined

}

class cotton extends Kharif {

boolean YieldFlower =false;
boolean isUseful=true;  //value of isUseful changed to true
String[] Products1={"raw material","raw material","raw material"};   //values of arrays defined

}

class mustard extends Rabi {

boolean YieldFlower =false;
boolean isUseful=true;    //value of isUseful changed to true
String[] Products={"oil","oil","oil"};   //values of arrays defined

}

class maize extends Rabi {

boolean YieldFlower =false;
boolean Useful=true;  //value of isUseful changed to true
String[] Products1={"food","food","food"};  //values of arrays defined
}

public class CropTester {
public static void main(String[] args) {

Crop[] crops=new Crop[4];
crops[0]=new millets();
crops[1]=new cotton();
crops[2]=new mustard();
crops[3]=new maize();

for(Crop b:crops) {
b.eat();  //it prints false for isUseful , but it should print true
b.theyGrow();
b.UsefulProducts(b.Products1); //it results in an nullpointerexception error at runtime , but i have defined the values in the subclasses
b.FavourableSeason();
}
/*
millets mil=new millets();
//System.out.println(mil.isUseful);
mil.eat(mil.Useful);
mil.theyGrow();
mil.UsefulProducts(mil.Products1);
mil.FavourableSeason();
*/
}
}
Was it helpful?

Solution

You're doing it wrong.

When you declare boolean isUsefull in superclass Crop its default value is false. Then when you create subclass millets with declared boolean isUsefull=true you don't overwrite the superclass variable value, you create a new one. If you want to owerwrite the value of superclass variable either use a constructor

millets() { isUsefull=true;}

or use a init block

{isUsefull=true;}

outside of any method body in millets class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top