Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating an abstract Superclass and a concrete Subclass  RSS feed

 
Mark Holland
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

Can someone please help me with the following basic exericise about Superclass and Subclass. The exercise comes from the book OCAOCP Programmer Studyguide by Kathy Sierra and is as follows:

1. create an abstract Superclass called Fruit. This Superclass should belong to a package called food and is public.
create the Superclass like:

package food;
public abstract class Fruit{ any code you want}


2. The subclass must be in a seperate file and be called Apple. It should have default access. It belongs to the default package (not put explicitly in a certain package).
Create the subclass like:

import food.Fruit;
class Apple extends Fruit { any code you like}

3. create a directory called food off the directory in your class path setting

4. compile the two files. If you use the Apple class place the Fruit.class file in the food subdirectory.

I use Eclipse and I completed step 1. I have a file called Fruit.java in a project called Fruit and in a package called food, looking like this:
package food;

So far so good. Now, the trouble starts with step 2. I created a (sub?) class called Apple, it looks like:



I just have no idea how to combine the two files no matter how hard I try. With the import line in the subclass, I was already expecting an outcome like "I love bananas I like beer too", but nothing of the sort. Let alone combining these two files in a new file. Can somebody please help me with this one? I want to learn it.

Thanks!!


 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean when you say you have no idea "how to combine the two files"? How do you want to combine them? If your goal is just to see both outputs printed, then the reason you are having a problem is because "I like beer too" is printed in the constructor of Apple, and nowhere do you create an Apple object.

You will certainly see "I love bananas" printed because you've put that in your main method, which gets executed when you run the program.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!