• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Creating an abstract Superclass and a concrete Subclass

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!


 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Dinner will be steamed monkey heads with a side of tiny ads.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic