Reputation: 109
I am trying to implement a calendar coupled with activities, for example:
I was thinking of two abstract classes, the first GroupCalendar would be like this:
public abstract class GroupCalendar {
HashMap<TimeFrame, Activity> entries = new HashMap<TimeFrame, Activity>();
public abstract void addActivity(TimeFrame t, Activity a);
public abstract void removeActivity(TimeFrame t, Activity a);
public abstract void findActivity(TimeFrame t);
public abstract void printAll();
}
and the second would be Activity class:
public abstract class Activity extends GroupCalendar {
private String name, location;
public Activity() {
}
public Activity(String name, String location) {
this.name = name;
this.location = location;
}
public abstract String getName();
public abstract void setName(String name);
public abstract String getLocation();
public abstract void setLocation(String location);
}
After that there would be the concrete classes that extend Activity:
public class Sport extends Activity {
private String name, location;
private boolean outdoors;
public Sport(String name, String location, boolean outdoors) {
this.name = name;
this.location = location;
this.outdoors = outdoors;
}
@Override
public void addActivity(TimeFrame t, Activity a) {
}
@Override
public void removeActivity(TimeFrame t, Activity a) {
}
@Override
public void findActivity(TimeFrame t) {
}
@Override
public void printAll() {
}
}
The problem is that I cannot add an activity properly, if I try to add a SPort, all I get is the name and location, no outdoors. If anyone could give me some hints/pointers about what is wrong with my approach that would mean a lot to me.
Upvotes: 0
Views: 259
Reputation: 7638
I don't see why Activity
should extend GroupCalendar
, there is no parent/child relationship between them.
If you want to get outdoors
just create a getter for it in Sport
.
Also, what probably your teacher want you to do is to have a method print
in Activity
, so any subclass would have to implement it accordingly; for example Sport
would print name
+ location
+ outdoor
.
Then you would be able to call a.print()
where a
is an Activity
Upvotes: 1
Reputation: 4213
First, I'm not sure why you want to use abstract classes for this?
Second, your calendar is made up of activities, an activity should not posses the qualities of a calendar. A single activity does need to have a collection of other time frames and activities, unless activities are made up of other activities.
I would have a concrete GroupCalendar
class with a concrete Activity
class and, if need be, override Activity
methods in sub classes. The Activity
class should handle the accessing and mutating of Activity
members, you don't want to override those for every sub class of Activity
.
On another note, Activity
has an overloaded constructor that takes name
and location
. Therefore, in your Sport
class' overloaded constructor, you should make a call to super(name, location)
and remove the not needed assignments.
Upvotes: 1