Reputation: 11
Say that the user gives you a phone number (as an object of the class PhoneNumber or a subclass of it) which he first states if it's a Mobile Phone or a House Phone. In the main program you should be able to know (way afterwards) if it refers to a Mobile Phone or to a House Phone.
And here's what you can't do:
Should you make two different classes that extend the class PhoneNumber and have the exact same content (in my example they are actually empty, but in my real problem they're not)? If you do so you can make objects of those classes and use instanceof in the main program, but I wonder if there's another solution, one that seems better to the eye.
EDIT: What seems wrong to me are the 2 classes with the same contents, not instanceof, because we actually only know that for figuring out an object's class.
Example:
public class PhoneNumber{
private int Number;
public PhoneNumber(int Number){
this.Number = Number;
}
}
public class Mobile extends PhoneNumber{}
public class HousePhone extends PhoneNumber{}
Upvotes: 0
Views: 98
Reputation: 303
Since you can't create any variable in the main program, your options are obviously limited.
You could use OOP concepts to solve this problem as you have attempted. And you don't need to duplicate the contents since inheriting from the same parent which already has all that content would automatically grant them access to the content. This includes all properties and fields except those specified as private. To solve this, you could make those properties protected.
The following should work for you:
public abstract class PhoneNumber
{
private int Number;
public PhoneNumber(int Number)
{
this.Number = Number;
}
protected int getNumber()
{
return Number;
}
public void doSmthnWithNumber();
}
public class Mobile extends PhoneNumber
{
@Override
public void doSmthnWithNumber()
{
int Number = getNumber();
// Do whatever you want with mobile numbers
}
}
public class HousePhone extends PhoneNumber
{
@Override
public void doSmthnWithNumber()
{
int Number = getNumber();
// Do whatever you want with HousePhone numbers
}
}
So with this, you know that there is no redundancy in your code, you are not repeating yourself and you are following SOLID principles. Makes it easier on the eye and better for other devs to understand you. Also you can make changes to PhoneNumber and it will reflect in the child classes.
Hope I have been able to solve your problems.
Upvotes: 0
Reputation: 4587
Alternate to instanceof
, you could use isAssignableFrom
e.g.
PhoneNumber phoneNum = new Mobile(123);
if (phoneNum.getClass().isAssignableFrom(Mobile.class)) {
Mobile mobile = (Mobile) phoneNum;
} else {
HousePhone housePhone = (HousePhone) phoneNum;
}
Upvotes: 0
Reputation: 41097
You can't make any variable (in the classes or in the main Program) that states the answer. But you can make a method to get the type. In your classes Mobile
and HousePhone
create a method getPhoneType
that return an enum
of some kind. The getPhoneType
method should be abstract method in base class.
I think this better than instanceof
.
Upvotes: 1