Results 1 to 7 of 7

Thread: inheritance

  1. #1
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default inheritance

    let say I have two class

    class Student{...}
    class Graduate extends Student{..}

    in the program I want to change a student to graduate

    is it posible - and how ?

    Java Code:
    Student std1=new Student (args..);
    .
    .
    std1.someMethod(args);
    .
    .
    [COLOR="Red"]make std1  into Graduate class [/COLOR]
    .
    .

  2. #2
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    Another option - can I extend the class inside its own constructor
    for example if the cosntrcutor of Student is like this

    Java Code:
    public Student(int ID, String name, boolean graduate)
    {
    this.ID=ID;  this.name=name;
    if (graduate==true)
        [COLOR="Red"]make it a Graduate class[/COLOR]
    };

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    No, Student as the parent class should have no dependencies on a child class. This is a very bad plan.

    You can't really "change" a Student object into a Graduate object, but you can construct a Student variable as a Graduate object:
    Java Code:
    Student myStudent = new Graduate(); // this is ok

  4. #4
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    You can't really "change" a Student object into a Graduate object, but you can construct a Student variable as a Graduate object:
    Must be someting . let say other there are other classes that extends Student ( DumbedStudent or whatever ) and I dont when I define the student if he will graduate or fail so what I do

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not sure that I'd use inheritance to solve this problem. What you are describing sounds more to me like a property of a Student object, and perhaps you need to ad a field or two to your Student class such as boolean graduated.

  6. #6
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    you know that student and graduate is just an example/

    The child classes has other methods

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    But still, what you are describing is a state-changing system, and I'm not sure that inheritance is the way to solve this. There are probably design patterns that would solve this much better such as the strategy pattern.

Similar Threads

  1. aggrigation and inheritance
    By ramakrishna.tata in forum New To Java
    Replies: 6
    Last Post: 07-08-2008, 09:11 AM
  2. Method inheritance
    By JT4NK3D in forum New To Java
    Replies: 7
    Last Post: 06-02-2008, 07:18 PM
  3. Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 07:08 PM
  4. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 01:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •