I have class A
I have class B which extends class A
I have class C which extends class B
I have one method which sets a value - this is used by class B and class C (NOT class A); therefore class C will inherit it if it was declared as an abstract method in class A because class B will inherit it from class A(I hope I am right so far with my understanding?)
- however, if it is a fixed value throughout then that's where an interface is better. As it is used by more than one Class?
Have I got a choice here? i.e. is it better to declare class A as abstract including the method to set a value or to use an Interface? Or can I actually use either? Which one is better and why?
Thanking you in advance!
If this doesn't seem to line up with your thinking, please give us a little more detail (perhaps a concrete example).
i do not mean to criticize,
but i am also learning java...
i have seen that you provide very little detail when you post your questions here...that it makes difficult for me to follow how others are guiding...
i think we all are here to learn...
please try to post your entire source...
people like me will not follow much...
this is a kind request to you and all members of this wonderful family!!!
Prashanth Lingala, sorry for not posting code - hopefully I will take your comments on board and my questions will be easier to follow going forward.
I have a class called Current
The Current class has many methods i.e.
public double getBalance ( );
public String getAccName ( );
public String get AccNo ( );
public void setAccName (String name);
public void setAccNo (String accountNo);
public void dispDetails ( );
public void setBalance (double newBalance);
All of these methods are used in another class called Platinum
therefore Platinum extends Current
All of these methods are used in another class called Student
therefore Student extends Platinum
(NOT Current for reasons you will see later).
I then wish to have another method:
public void setInterest (double newInterest);
Now setting the interest is used for both the Plantinum and Student account. But for Plantinum to implement the setInterest method I would need to declare it in Current as abstract, for Plantinum to implement it then Student to inherit it. Am I right so far?
Is this the best way? i.e. I acknowledge that Current doesn't use it but it would need to declare it for Plantinum to implement it.
Or is it better to have an interface? As the setInterest method would be the same for both Plantinum AND Student; therefore if Plantinum and Student Implement setInterest as an interface they could both use it and Current doesn't need to be bothered?
I hope the above sounds less confusing; and hopefully Prashanth I have made myself clearer?
Next part of the question:
I have an additional method called applyInterest:
public void applyInterest ( )
The Current class doesn't use this method.
Both the Platinum and Student class do use this method.
However, the Student method varies from the Platinum. Here, surely I would use an abstract class? The Current class would need to declare it then both the Platinum and Student class can apply it how they wish?
Gosh, I hope this sounds clear
You are asking great questions here and in your other posts. It shows that you are trying to understand the underlying concepts of Java and Object Oriented Programming (and Design) and not just trying to get syntax correct so your code will compile. you should be commended for that.
Your questions here about when to use an abstract class, abstract methods, interfaces, or concrete classes, where methods should be declared, etc are all great questions; but are also hefty topics. I mentioned the book Head First Java in one of your other posts (be sure to read that post for a caveat if purchasing the book at Amazon). That book does a great job on these type of topics. In fact, if you go to the publisher's website (link above), the sample chapter 8 available there is on interfaces and polymorphism. I think it will answer many of the questions you ask in this post. It also talks about when to use an interface, a concrete superclass, or an abstract superclass. Give that a look. (After reading Head First Java, take a look at Head First Design Patterns. Based on the questions you are asking, I think you would find it interesting, and I think you would be ready (in your studies) to learn such material.)
In the meantime, I will take a look at your questions here (as I am sure others will also) and see if I can help you a bit with them.
I hope that helps...
[ March 05, 2005: Message edited by: Mark Vedder ]
Instead of declaring an abstract method setInterest in Current -- which has no concept of interest -- you should simply add the method to Platinum, and Student will inherit it. You can declare new methods in subclasses without using an interface or declaring the same method as abstract in the parent.
Originally posted by Maureen Charlton:
Now setting the interest is used for both the Plantinum and Student account. But for Plantinum to implement the setInterest method I would need to declare it in Current as abstract, for Plantinum to implement it then Student to inherit it.
There are some cases where you want to declare an abstract method in the parent. However, in those cases you're making the parent class abstract as well, so you cannot instantiate a Current -- only a concrete subclass.
For example, let's assume that Current is already abstract and that it also has an interest; therefore it defines setInterest, and the others inherit it. But each type of account calculates the interest for the billing period differently. In this case you'd declare an abstract calculateInterest method in Current and implement them in Platinum and Student with different calculations.
Whether or not you define an interface with the setInterest method for Platinum to implement is a separate question. If you think you'll create other classes not extending Platinum that will have interest-related methods, this may be a good choice. You can, of course, always split out an interface (called refactoring) when you actually need it.