Results 1 to 5 of 5
- 12-16-2011, 07:47 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
A question on Inheritance and fields
Hi people. Here's a question on Inheritance:
I have a Human Class:
and I have my Student class which extends the Human class:Java Code:public class Human { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
and my Test Class:Java Code:public class Student extends Human { private String name; public void setName(String a) { this.name=a+a; } }
I am expecting that student1 gets the name: JohnJohn. However here is the concole output: null.Java Code:public class Test { public static void main(String[] args) { Student student1 = new Student(); student1.setName("John"); System.out.println(student1.getName()); } }
Can anyone explain please?
Thank you.
- 12-16-2011, 07:49 PM #2
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: A question on Inheritance and fields
Answering my own question:
If I override the getName() method, I get JohnJohn...
Thanks.
- 12-16-2011, 07:57 PM #3
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: A question on Inheritance and fields
-
Re: A question on Inheritance and fields
- 12-16-2011, 08:39 PM #5
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Similar Threads
-
Inheritance Program Question
By simon.black325 in forum New To JavaReplies: 11Last Post: 09-08-2011, 03:01 AM -
inheritance question
By Jaevko in forum New To JavaReplies: 7Last Post: 08-31-2011, 07:30 AM -
A question about inheritance / polymorphism
By gabriels in forum New To JavaReplies: 2Last Post: 02-26-2011, 11:53 AM -
Question about inheritance
By hei1233212000 in forum New To JavaReplies: 3Last Post: 11-12-2010, 11:38 AM -
a question about inheritance
By litojs in forum New To JavaReplies: 8Last Post: 11-10-2008, 10:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks