Jenny
Jenny

Reputation: 45

How to create an object from a subclass in Java

So I have this class:

public class parent {
     private int id;
     private int row;
     public parent(int id,int row) {
        this.id=id;
        this.row=row
     }
}

and then I have this class which extends parent

public class son extends parent {
    public son(int id,int row) {
        super(id,son);
    }
}

the question is how do i create an object for class son.Do I have to call it like this:

son x=new son(int id,int row); 

I am really confused.

Upvotes: 0

Views: 216

Answers (1)

Mshnik
Mshnik

Reputation: 7032

Yep, you're just about spot on! To be absolutely clear, you wouldn't use the type of id and row in calling the constructor, you would just give values of that type.

So this is wrong:

son x = new son(int 5, int 6); //Incorrect

And this is correct:

son x = new son(5, 6); //Correct

You can also pass variables of the correct type, like this:

int id = 5;
int row = 6;
son x = new son(id, row);

Also, I just noticed that you wrote:

public class parent {
     private int id;
     private id row;
     //....

instead of

public class parent {
     private int id;
     private int row;  //Note the change id --> int here
     //....

If this was a typo, don't worry about it. Otherwise you may have a conceptual misunderstanding. id isn't a type, but int is. So we can't declare row as an id, but we can declare it as an int. Unlike in C and friends you can't create synonyms of types with a typedef, so you're stuck with the base types (int, boolean, etc).


Since it appears that you're new to Java, the convention is for classes to have Pronoun Case (capitalized first letter of each word) names. Thus it would be better style to use the following formatting for your classes:

public class Parent {
     private int id;
     private int row;
     public Parent(int id,int row) {
        this.id=id;
        this.row=row
     }
}

public class Son extends Parent {
    public Son(int id,int row) {
        super(id,son);
    }
}

public class ThisClassHasManyWordsInItAndItShouldBeFormattedLikeThis {
   //.....
}

Which then makes the construction:

Son x = new Son(5,6);

Once you've constructed a Parent object like Parent p = new Parent(4,5);, there's no way to change p into a Son. It's not possible. However, you can copy p into a new Son, and you can make some modifications to the classes to make it easier to make these copies:

public class Parent {
     private int id;
     private int row;
     public Parent(int id,int row) {
        this.id=id;
        this.row=row
     }

     public int getId() {
         return id;
     }

     public int getRow() {
         return row;
     }
}

public class Son extends Parent {
    public Son(int id,int row) {
        super(id,son);
    }

    public Son(Parent p) {
        super(p.getId(), p.getRow());
    }
}

Now we can create a Parent, and copy it into a new Son:

Parent p = new Parent(4,5);
Son s = new Son(p);  //will have id of 4 and row of 5

It's worth noting that while all this is well and good for learning how class extension works, you're not actually using it quite correctly. By saying Son extends Parent, you're saying that Son is a type of Parent, which isn't true in the elementary school model of a Family. A better way to model a family would probably be:

public class Person {
    private Person mother;
    private Person father;

    public Person(Person mother, Person father) {
        this.mother = mother;
        this.father = father;
    }
 }

If you're still looking for a way to include class extension, then Man and Woman make sense as extensions of class Person, because a Man is a type of Person. (i.e. All men are people, not all people are men).

public class Person {
    private Man father;
    private Woman mother;

    public Person(Man father, Woman mother) {
        this.father = father;
        this.mother = mother;
    }
}

public class Man extends Person {
    public Man(Man father, Woman mother) {
        super(father, mother);
    }
}

public class Woman extends Person {
    public Woman(Man father, Woman mother) {
        super(father, mother);
    }
}

Upvotes: 5

Related Questions