?
Solved

Java OOP Extending Classes and Inheritance

Posted on 2009-04-23
2
Medium Priority
?
259 Views
Last Modified: 2013-11-23
Given an abstract ItemBase class, two subclasses A and B are extended from it with methods specific to each one.  For example:

ItemBase has generic method V, W
subclass A has specfic method X
subclass B has specfic method Y

How can the above design be implemented so that ItemBase be used as an Array that contains objects of A or B calling their individual method X or Y? Can methods X and Y be omitted in Itembase or should they be defined in it? How?
Thanks for any help.
0
Comment
Question by:ksfok
2 Comments
 
LVL 20

Accepted Solution

by:
Venabili earned 1500 total points
ID: 24224666
If you need to call them and they are not in ItemBase, you will need to cast the objects...
What I would do would be:
Make a method someMethod in ItemBase and make it abstract
Then implement it in A and B instead of implementing differently named methods. And make this implementation call the individual X and Y methods
Then when you invoke the someMethod to an ItemBase object, it will fall to the proper method based on what the real type is.

That's the cleanest way I can think of.
0
 

Author Comment

by:ksfok
ID: 24229753
  1. Make a method someMethod in ItemBase and make it abstract
  2. Then implement it in A and B instead of implementing differently named methods. And make this implementation call the individual X and Y methods
Please kindly provide code syntax for the above.
Thanks much.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question