• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance - Disabling parent methods in a child class ?  RSS feed

 
David S Hansen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I extended an ArrayList to make my own class. I don't want the users of this child class to be able to use some methods of the parent class. To do that, I overloaded the parent methods with do-nothing code
and added a print message to show that the operation is disabled in child class. Of course, the child class can be referenced by a parent and the parent method can still be called. But, i thought that at least the something is
better than nothing.

Is it ok to do what i did ?

 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A subclass object IS‑A superclass object. If a superclass object has an add() method, then users will expect to be able to use the add() method. You are therefore breaching the principles and conventions of inheritance. Google for Liskov substitution principle.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a wrapper class round your List.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David S Hansen wrote:I extended an ArrayList to make my own class. I don't want the users of this child class to be able to use some methods of the parent class. To do that, I overloaded the parent methods with do-nothing code...

Campbell's pretty much covered the basics. Simply put: if you don't want people using superclass methods, don't subclass.

However, there may be an alternative:
Rather than subclassing ArrayList, subclass AbstractList, and use it to wrap an array. Most methods of AbstractList are defined to throw UnsupportedOperationException unless you specifically override them, which I suspect is closer to what you'd like.

Winston
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Rather than subclassing ArrayList, subclass AbstractList, and use it to wrap an array.


I know it would be quite obvious for you, but can you show how to wrap an array.. through one or two lines of code. I didn't understand that wrap thing written by you and ritchie.
Like I know we wrap classes to get more functionality like we wrap InputStreamReader into a BufferedReader. but still i am not clear with the "wrap" term that you both used here.

thankyou.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya wrote:
Winston Gutkowski wrote:
Rather than subclassing ArrayList, subclass AbstractList, and use it to wrap an array.


I know it would be quite obvious for you, but can you show how to wrap an array.


What's meant in this case is something like this:



Then all the abstract methods that you need to implement would operate on that array.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody else is trying to create himself an array list class; look through this forum and you should find it easily. I think that thread will provide lots of useful advice.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Then all the abstract methods that you need to implement would operate on that array


which array did you mean Jeff??

@Ritchie- i read about the "Liskov Substitution Principle" as you said and i got it most probably. but still i didnt got what did you meant by
Create a wrapper class round your List.

can you give me more details that how it is done?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya wrote:
Jeff Verdegan wrote:
Then all the abstract methods that you need to implement would operate on that array


which array did you mean Jeff??


This one, in my previous post:

I wrote Object instead of Object[] initially. Sorry for any confusion.

 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this concludes that MyList wrap's backingArray.. is it Jeff??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya wrote:So this concludes that MyList wrap's backingArray.. is it Jeff??


Yes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!