A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
inheritance problem
ajithsasi kumar
Greenhorn
Joined: Apr 06, 2008
Posts: 21
posted
May 05, 2008 05:10:00
0
import java.io.*; class A { public String name[]=new String[3]; int i; public void read1() { BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); try { for(i=0;i<3;i++) { System.out.print("Enter NAME OF STUDENT :"); name[i]=b.readLine(); } } catch(IOException e) { System.out.println("Error"); } //System.out.println(name[0]); //System.out.println(name[1]); //System.out.println(name[2]); } } public class B extends A { int i; public void display() { for(i=0;i<3;i++) { System.out.println(name[i]); } } public static void main(String args[]) { A a=new A(); B b=new B(); a.read1(); b.display(); } }
the names are displayed as null in the display method of the main class. Why it is not possible to get the values. Sir Help me...
Sandeep Mukherji
Ranch Hand
Joined: Mar 23, 2008
Posts: 46
posted
May 05, 2008 05:45:00
0
Make a slight change:
public class B extends A { int i; A a; B(A a){ this.a = a; // referance of A is to be taken } public void display() { for (i = 0; i < 3; i++) { System.out.println(a.name[i]); } } public static void main(String args[]) { A a = new A(); B b = new B(a); // referance of a passed a.read1(); b.display(); } }
You were using the overrided version of the name in class B. You had to take the name of the Class A only.
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
I like...
posted
May 05, 2008 05:54:00
0
Thanks Sandeep
Sandeep Mukherji
Ranch Hand
Joined: Mar 23, 2008
Posts: 46
posted
May 05, 2008 06:08:00
0
I said:
You were using the overrided version of the name in class B
Actually you were hiding the field 'name' in your class B. Pardon me for saying 'Overriding'.
ajithsasi kumar
Greenhorn
Joined: Apr 06, 2008
Posts: 21
posted
May 05, 2008 21:37:00
0
Thank You 'Sandeep Mukherji' for your reply
I agree. Here's the link:
http://aspose.com/file-tools
subject: inheritance problem
Similar Threads
copying arrays - please h e l p!
please help with simple problem
comparing elements in one hash set to the elements in another
writer's block on assignment, any help?
Array IndexOutOfBounds Exception
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter