This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Help in java logic

 
kademane guru
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kademane guru
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
James Howerton
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic