Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance of a Variable

 
Ravi Pydi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam an amateur to JAVA Programming. Iam working on a task where i should be reading a TEXT file as input and do some validations on the data.
Approach:
I have created a class(Validation.java). I have used DataInputStream to read the data. And the data is stored onto a String variable (say allData) using the readLine() method.
I have declared the varible as an instance variable in the super class.
public allData;
Then i have instantiated the class and the input stream.
Validation val = new Validation();
val.allData = instr.readLine();
//Note:Assuming the input stream is declaread and instantiated.
System.out.println(val.allData);
Iam getting the output that was desired. Now i have done some validations on the data and everything is working fine.
Now i want to use the same variable "allData" in another class.
I have created a subclass and EXTENDED the super class where "allData" variable exists. I have put both the classes into the same PACKAGE.
When i instantiated the subclass (ValidationSub) and printed the "allData" value iam getting null as output. But i want to get the output similar to the output i got in my superclass.
Note: i havent declared any variable in my subclass since it is already declared in the superclass.
How do i get the data assigned to my variable in superclass into my subclass?
I will be glad if anybody could suggest on this.
Thanking you,
Ravi Pydi
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friend here's a sample test case:

How are you doing it?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic