Results 21 to 21 of 21
- 09-28-2013, 09:57 PM #21Member
- Join Date
- Sep 2013
- Rep Power
Re: Method in subclass is not being implemented.
I think you are simply misudnerstading how inheritance works. A subclass inherits the methods and variables of the class you inherit from. When you create an object you are creating a reference to the class.
For example lets say you have a Person class and a Student class that extends Person. Your person class holds name and phone number while the student class has the additional field of a student id. Now lets say you create a reference to Person naturally you have access both name and phone number but not student id because it knows nothing of it. Now you create a different reference that is a Student. Eventhough your student class has no name and phone number fields it still has access to them because it is inheriting them from Person. You did not have to write additional code into this student class other than that code to handle the student id.
Person class -name : String -phone : String +Person() +Person(name : String, phone : String) +setName(name : String) : void +getName() : String +setPhone(phone : String) : void +getPhone() : String +toString() : StringJava Code:
Student extends Person -studentId : int +Student(name : String, id : int) +Student(name : String, phone : String, id : int) +setId(id : int) : void +getId() : int +toString() : StringJava Code:
Tester class Person p = new Person("Henry", "555-123-4567"); Person s = new Student("Anna", 155423); S.o.p(p); //Displays: //Name: Henry //Phone: 555-123-4567 S.o.p(s); //Displays //Name: Anna //Phone: //Student id: 155423
Hopefully I helped make it a little easier to understand and not more confusing.
- By Randron in forum New To JavaReplies: 2Last Post: 07-11-2013, 04:09 AM
- By konman795 in forum New To JavaReplies: 2Last Post: 02-24-2012, 09:44 AM
- By iliyapolak in forum Advanced JavaReplies: 23Last Post: 01-17-2012, 07:16 PM
- By samanesh in forum New To JavaReplies: 4Last Post: 11-24-2011, 05:26 PM
- By pppriyadarshi in forum New To JavaReplies: 1Last Post: 09-10-2011, 06:44 PM