permaculture playing cards*
The moose likes Java in General and the fly likes Need Help in java logic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need Help in java logic" Watch "Need Help in java logic" New topic
Author

Need Help in java logic

kademane guru
Greenhorn

Joined: Feb 20, 2007
Posts: 21
There is a class A with 10 methods,
class B is extending class A here we should allow to implement only 1st 5 methods thats it, remaining last 5 methods we should not allow to implement here,
class C extends class A this class has to implement the last 5 methods only and 1st 5 methods should not allow to implement here.

Please can anybody help me out ?
Regards
Guru
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

There is a way you can permit/prevent a method to be extended by using the keyword 'final' on the method.

But, I don't think there is a way to customize the methods based on a specific set of sub classes!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

It should be in 'Java General' forum and NOT in Struts. Any of the moderators would shift this thread to an appropriate forum.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Moved to the appropriate forum.

I don't think there's any way of disallowing class B from implementing the remaining 5 methods which you intend to override in class C...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Thanks Ankit.

Kadamane Guru,

If you wish to have, you might split those methods into two set of classes and decide the appropriate access modifiers of the methods in each class. There is one way I can think of.

Otherwise, there is no direct way (as per my knowledge) to cater to your needs as you quoted in your post.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
kademane guru wrote:
Please can anybody help me out ?


Why can't you just implement B and C the way you suggest? You can never make A dictate what specifically B and C should do. Maybe you could make it more or less suggestive that A should be handled in one of two ways, but never dictate it.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

Would this be a good case to "favor composition over inheritance" and use a simple Strategy design pattern?

For Example:

SuperClass has-a basicInterface
basicInterface is an interface with 5 mehods
basicInterfaceA implements the procedures for the first subclass
basicInterfaceB implements the procedures for the second subclass

SubClassA uses the basicInterfaceA (HAS-A) implementation
SubClassB uses the basicInterfaceB (HAS-A) implementation

This might not work..... but I think it could. It's a thought...

Janeice


When you do things right, people won't be sure you've done anything at all.
kademane guru
Greenhorn

Joined: Feb 20, 2007
Posts: 21
Hi Janeice,
Thanks.Your logic will work , i feel there is no option apart from this.
if you find any other way to solve this please help me.

Regards
Kademane Gururaja
Bhagat Singh Rawat
Ranch Hand

Joined: Apr 04, 2009
Posts: 93
Janeice DelVecchio wrote:Would this be a good case to "favor composition over inheritance" and use a simple Strategy design pattern?

For Example:

SuperClass has-a basicInterface
basicInterface is an interface with 5 mehods
basicInterfaceA implements the procedures for the first subclass
basicInterfaceB implements the procedures for the second subclass

SubClassA uses the basicInterfaceA (HAS-A) implementation
SubClassB uses the basicInterfaceB (HAS-A) implementation


Hi Janeice,
Can your write your thought in class/interface form please?


Brainbench Java 2.0 Fundamentals, J2EE 1.4
http://www.brainbench.com/transcript.jsp?pid=8192792
James Howerton
Ranch Hand

Joined: Mar 14, 2009
Posts: 82
Hello

I am learning super and extending class now (trying)

Does it make sense if Class a.java had two constructors ?
classA(String x) and ClassA(String y,String z)

you could control the method usage by what constructor class b and c use.

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

James, it seems to me like you might need a new thread for your question.

Bhagat, check out this Wiki on the Stategy Design Pattern. It has a basic code example in the "Java" section.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Help in java logic