Hello people. I need an explanation about inheritance I got a little confused in class:

This is the modelling problem:

There are two objects: doctors and patients. They are expected to have only the following attributes:

Doctors have: name, age, id, and salary
Patients have: name, age, id, and disease type

Since three of the 4 attributes are repeated, we were told to use inheritance. So we created a People class containing name, age and id as attributes and one method for setting those variables to a given value.

Here comes the problem:

In class we instantiated the parent class together with the other 2 children. Then, the teacher used the set method OF THE PARENT CLASS to take care of the 3 common variables and then used the children's methods to set the remaining variables that were still not set. Finally, he accessed the age, id and name variables of the parent class, and the salary and disease type variables of the children classes

Is this truly inheritance? Can't we use only the children, since they will also have the setter method created in the parent along with the parents three attributes?

Let me know if you don't undertand my question. I admit it's a little confusing

inheritance is really handy in eliminating duplicate code, and making the overall code more compact, since doctor and patient have things in common, its handy to put the common stuff in a superclass.

as regards

Can't we use only the children, since they will also have the setter method created in the parent along with the parents three attributes?

class people {

    private String name;
    private int age,id;

    public void setName(String name){
        this.name = name;
    }

    public void setAge(int age){
        this.age = age;
    }
    public void setId(int id){
        this.id = id;
    }

    public void display(){
        System.out.println("name: " + name + " age: " + age + " id: " + id);
    }

}

class patient extends people{

    String disease;

    public void setDisease(String d){
        this.disease = d;
    }
    public String getDisease(){
        return disease;
    }
}

class doctor extends people{

    int salary;

    public void setSalary(int s){
        this.salary = s;
    }
    public int getsalary(){
        return salary;
    }
}


public class peopleTester {

    public static void main(String[] args){
        doctor d = new doctor();
        patient p = new patient();

        d.setName("kurt");
        d.setAge(40);
        d.setSalary(3000);
        d.setId(10);

        p.setName("jhonny");
        p.setId(4505);
        p.setAge(23);
        p.setDisease("fever");

        d.display();
        p.display();
    }

}

here, i did just use the children, and all the inheritance worked quite alright :)

the teacher used the set method OF THE PARENT CLASS to take care of the 3 common variables and then used the children's methods to set the remaining variables that were still not set.

when you use the setters of the superclass (ie, people) , superclasses variables are set, and on inheriting the class, you have to overwrite the variables to the child's specifics.

hope it helps.

Edited 3 Years Ago by somjit{}

Thaks for your help. So:

1 - This is used for referring to this class' variable whose name is similar to the method's parameter?
2 - Children inherit their parent's methods and attributes BUT using the paren't setter doesn't set the children's variables? If so, then using the childrens setter will only set the age and, id and name of ONE child and not the parent's or the other child's?
3 - Does this mean that in our example, my teacher wasn't truly using inheritance because he actually used the parent for the calculations (he rather extracted the parent's age, id, and name and used them as though they were the childrens')?

G_S ...
your explanation of 2 makes no sense at all. what exactly are you trying to say there?
for your point 3 - since we don't know what your teacher did, we can't really answer that. we also haven't seen any of your code, so we can't really comment on what it is you are doing.

@ G_S:

This is used for referring to this class' variable whose name is similar to the method's parameter?

a methods parameter, also called a local/static variable can be same or different to a class' variable, also called a instance variable. WHY? because they have different scopes... atleast in the above example it does. also, u should know, that a local variable lives in the stack , and an instance variable lives in the heap. so that further adds to why having the same name really doesnt matter too much.

Children inherit their parent's methods and attributes BUT using the paren't setter doesn't set the children's variables? If so, then using the childrens setter will only set the age and, id and name of ONE child and not the parent's or the other child's?

you can always download eclipse or netbeans, install it, and put this code there, and check ur ideas. thats one of the benefits of coding... that way, u can see 1st hand what effects these tweaks result in. whatever doubts you have then, you can always post here. :)

also, i agree that since we dont know what your teacher did, it would be a bad idea to compare with it. maybe you can post the code here. :)

Thank you everybody for your responses and sorry for the late reply. Turns out my teacher is really bad, so I simply passed the fisr three tests to ensure a passing grade for the course and stopped going to class regularly. The last time I checked they were accessing the second and third elements of a stack without first poping the ones above them...

Hahaha, look at my other thread "can objects be attributes", where I asked (or tried to) if an object can contain other objects inside itself in the form of an attribute. What I was asking was actually if composition (I didn't know the concept at that time) was possible in Java. My teacher said it was not possible... That's why I just got the passing grade and forgot about it.

This question has already been answered. Start a new discussion instead.