I have a super-class which is named Account. The sub-classes of Account are Checking and Savings. I want to write a toString() method in Account that prints information about both Checking and Savings at the same time. Is there a way to do this or do I have to make separate toString methods in each of the sub-classes?
CodePudding user response:
You seem to misunderstand what superclass is all about, going by your question and particularly by your follow-up comment:
I have thought about doing that, but since Checkings and Savings are two separate objects, how am I able to remember the information provided from the two accounts in the super-class?
You appear to think that superclasses are containers or groupings. Something like: "An account consists of a checking and savings part; the superclass combines the two ideas of checking and savings together; it's like a container for them."
That's not how that works.
It's much simpler: A subclass is a more specific take on its superclass. "An apple is a more specific kind of fruit". An apple is a fruit. If you ask me for some fruit and I hand you an apple, that's fine. If a 'Checking' is an 'Account', then there's no need to involve savings in this anymore. A checking clearly doesn't also have a savings to go with it, and given that "A is a subclass of B" means "A has and does everything B does, and some more to boot", what you want makes no sense.
Terms and english are a bit confusing here. Often one speaks of 'a checking account' and 'a savings account' and yet also one speaks of 'your bank account, containing a checkings account and a savings account'. The word 'account' here means different things. Computers are very literal and such ambiguity just isn't going to work there, so you're going to have to be more clear.
Let's call everything you have at the bank (which presumably includes a checking and savings account and perhaps more) a 'BankUser', and the accounts that are available for any bankuser an 'Account'.
Note that now, 'I want both the checkings and savings in order to print both balances' makes total sense as a thing that BankUser
might want to do, and makes no sense whatsoever as a think Account
might want to do. The whole point of Account is to talk about a single account - either the checkings, OR the savings.
NOW you can say that 'Savings is a more specific kind of Account; anything that is sensible to ask about an Account, you can ask about a Savings'.
You'd have 3 objects: A bankUser object, which has 2 fields: One Savings field and one Checking field.
CodePudding user response:
It's possible, but it sounds to me like Account
is not the right place for doing that. A Checking
is also an Account
, so having Account
depend on features specific to Checking
(let alone features specific to Savings
) feels a bit backwards - that kind of functionality might be a better fit for a kind of AccountHolder
class, which could then contain a Checking
and a Savings
without itself being either (or even being an Account
at all)
CodePudding user response:
In the superClass try to write a constructor with parameter ( String for example ), in this constructor put a switch case ; if the String is the name of SubClass1 do something ( increment a static counter ) if is the name of a second SubClass2 increment another static counter and like that you know hwo many instance from each SubClass you have created , all the subClasses must have in the first ligne of their constructors a call for the constructor created in the superClass.
class Person{
static int studentCounter;
static int teacherCounter;
public Person(String typeOfInstance)
{
if(typeOfInstance.equals("Student"))
studentCounter ;
if(typeOfInstance.equals("Teacher"))
teacherCounter ;
}
}
class Student extends Person{
public Student(){
super("Student");
...
}
}
class Teacher extends Person{
public Teacher(){
super("Teacher");
...
}
}
class Person{
String informationsOfChild;
public Person(String informations)
{
this.informationsOfChild= informations;
}
}
class Student extends Person{
String name;
int age;
public Student(){
super("" name " " age);
...
}
}
class Teacher extends Person{
String subject;
double salary;
public Teacher(){
super("" subject " " salary);
...
}
}
After that play with informations attribute in the Person class ..