Problems with abstract and interfaces

I want to do this:

One principal abstract class that has two methods

abstract method 1

abstract method 2

two secoundary class

class A implements method 1

class B implements method 2

How can I do this?

Abstract force me to implemenst all the methods in a class?

[315 byte] By [jorgiria] at [2007-11-26 15:24:00]
# 1
Do method1 and method2 do the same thing ? Perhaps in different ways but do the same thing?If they don't, and ClassA and ClassB don't want both pieces of functionality then you don't have an inheritance issue.PS.
puckstopper31a at 2007-7-8 21:39:21 > top of Java-index,Java Essentials,Java Programming...
# 2

> I want to do this:

>

> One principal abstract class that has two methods

> abstract method 1

> abstract method 2

>

> two secoundary class

> class A implements method 1

> class B implements method 2

>

> How can I do this?

> Abstract force me to implemenst all the methods in a

> class?

Abstract classes can declare non-abstract methods. Declare

method1() and method2() in the abstract class, and, neither

extending class will be required to implement it.

Also, ==puckstopper. Consider hard if this problem

is properly solved by inheritance.

es5f2000a at 2007-7-8 21:39:21 > top of Java-index,Java Essentials,Java Programming...
# 3
The only way is to have 2 abstract classes. Each only implements 1 method. What you are trying is not possible.Although, I don't understand why you would what want to do this. I would take a look at your design and redo it.Ted.
ted_trippina at 2007-7-8 21:39:21 > top of Java-index,Java Essentials,Java Programming...
# 4
class A and class B do almost the same. That is why I need inheritance( I think)
jorgiria at 2007-7-8 21:39:21 > top of Java-index,Java Essentials,Java Programming...
# 5
I'm not saying it does, I was saying that I want to understand what he's trying to accomplish and what is going on before I make a recommendation.Message was edited by: puckstopper31
puckstopper31a at 2007-7-8 21:39:21 > top of Java-index,Java Essentials,Java Programming...
# 6

> class A and class B do almost the same. That is why I

> need inheritance( I think)

Ok that's fine, what about method1() and method2() do they do the same thing.

As an example, I might have two or more different ways to calculate the sale price on an item depending on the circumstances. Is that the case you have or are the functions carried out by method1() and method2() entirely different?

PS.

puckstopper31a at 2007-7-8 21:39:21 > top of Java-index,Java Essentials,Java Programming...
# 7

I have a principal class that call 10 methods called:

method A

method B

method C

..

method J

I need to implement this methods in diferent classes because each one of this method have to be implemented by a different person.

So I think to create a abstract class with the ten methods to and after that, each person will extends the abstract class and implement one method?

Have I expained well?

jorgiria at 2007-7-8 21:39:21 > top of Java-index,Java Essentials,Java Programming...
# 8
I'm still not entirely tracking, must be dumber than usual today. What do methods a .. j do? Do you plan to have behavior for each of them at the abstract (super) class level, or only when they are implemented down the line?PS
puckstopper31a at 2007-7-8 21:39:21 > top of Java-index,Java Essentials,Java Programming...
# 9
All of the method have the same behavior, and each one of the method are implemented by a different person.
jorgiria at 2007-7-8 21:39:21 > top of Java-index,Java Essentials,Java Programming...
# 10
If each method has the same behaviour, then it should be onemethod which each subclass implements for itself.
es5f2000a at 2007-7-8 21:39:21 > top of Java-index,Java Essentials,Java Programming...
# 11

Then you don't need ten different methods not at all you need one method, and now I think I understand your problem. Inheritance from an abstract class isn't the answer to your problem.

Consider the following:

public abstract class SuperClass

{

public void methodA()

{

// ... Method A functionality

}

public void methodB()

{

// ... Method A functionality

}

public void methodC()

{

// ... Method A functionality

}

}

class A extends SuperClass

{

public void doStuff()

{

methodA() ;

methodB() ;

methodC() ;

}

}

class B extends SuperClass

{

public void doStuff()

{

methodA() ;

methodB() ;

methodC() ;

}

}

You have two classes that inherit from SuperClass and they ALL have access to all of its member functions (methodA() ... methodC()). Since methodA() ... methodC() all do the same thing there's absolutely no point to this at all.

So, if methodA() ... methodX() all have the same functionality then you only need it in one place [SuperClass] and this is a case where inheritance is the answer that you want.

Or, if methodA() ... methodX() have different functionality but the same method signature and have NO default behavior then you want an interface which the other developers would implement.

Or, if methodA() ... methodX() have default behavior but MIGHT be changed by the developer implementing the functionality then a superclass which you would override in the subclass is the correct answer.

Making sense now?

PS.

puckstopper31a at 2007-7-8 21:39:21 > top of Java-index,Java Essentials,Java Programming...