0

I have an abstract class A where I have lets say 3 variables, String username, String password, char sex. In this class I have a constructor to create an Object of that class with all these variables.

I have another two classes B and C that extend the class A, and each of them adds one of their variables to the Object Person. I.e. class B should add a variable isAdmin, the class C should add a variable isUser.

I've tried to make it with constructors, but I don't want to add any of the variables from classes B and C to A, I just want to create these variables in their classes separately and if I call the constructor from these classes B or C, that one of the variables is added to the Object User.

public class abstract class A {
private String name;
private String password;
private char sex;
public A(String name, String password, char sex){
this.name = name; this.password = password; this.sex = sex;}
}

public class B extends A {
public int isUser = 1;
public B(String username, String password, char sex){
super(username, password, sex)}
}

Is that even possible? Thanks in advance!

3
  • 1
    I don't understand your problem. Did you try it? Commented Oct 16, 2013 at 19:31
  • I've tried it, but if I put the int isUser in the constructor of the class B I will get an error, because in the superclass constructor I have 3 variables, and in the class B constructor I have 4 of them. That is the problem Commented Oct 16, 2013 at 19:35
  • You can put an int parameter in the B constructor and not in the A constructor. That's fine. Just keep calling the super the way you are doing. Commented Oct 16, 2013 at 19:36

2 Answers 2

1
but I don't want to add any of the variables from classes B and C to A, I just want
to create these variables in their classes separately

That is what inheritance is for!

Is that even possible?

Yes it is perfectly valid.

I've tried it, but if I put the int isUser in the constructor of the class B I will get
an error, because in the superclass constructor I have 3 variables, and in the class B
constructor I have 4 of them. That is the problem

You only need to have isUser instance variable in your class B. As there is no need to have this variable in class A you don't need to have it in the constructor. You can do something like below

public class B extends A {
public int isUser = 1;
public B(String username, String password, char sex,int isUser){
super(username, password, sex);
this.isUser = isUser;}
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have to create empty constructor in class A :

protected A(){}

Then you can call in your B and C class this :

super()

If NO contructor exists, when creating new object, empty constructor is created automatically. However if some constructor exists, no constructor is automatically created, so if you want empty constructor, you have to add there manualy.

I've tried to make it with constructors, but I don't want to add any of the variables from classes B and C to A

I am not sure, if you understands inheritence properly. If you have B extends A, it means, that when you create instance B, it has all the methods and properties that has class A. However instance B will be instance B, there will be no instance A. It will be instance B with properties of both - class A and class B.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.