Results 1 to 6 of 6
Thread: Help with abstraction
- 11-21-2010, 08:55 PM #1
Member
- Join Date
- Nov 2010
- Location
- Manchester
- Posts
- 2
- Rep Power
- 0
Help with abstraction
Hey guys,
I've been given an assignment in which I have to create two classes, the first is a Question superclass which includes an abstract method which returns the type of question i.e. getQuestionType(). Along with this I have to put two methods a getIntro() and setIntro() in this superclass.
This I have written like this:
For the next part we have to write an EssayQuestion subclass, one of three types of question and includes a getQuestionType() which returns the string "Essay Question". The other methods in this class are ones to get/set min and max words. This i've written like this:PHP Code:public abstract class Question { private String intro; Question(){} public abstract String getQuestionType(); String getIntroduction(String intro) { return intro; } String setIntroduction(String newIntroduction) { intro = newIntroduction; return intro; } }
My question is the correct way of using abstract, and does the entire subclass need to be abstract, i've only really declared it so because the IDE i'm using, NetBeans states that its an error if don't.PHP Code:public abstract class EssayQuestion extends Question { private int minimum, maximum; EssayQuestion(int minimum, int maximum){ this.minimum = minimum; this.maximum = maximum; } public String getQuestionType() { return "Essay Question"; } int getMinWords(int minimum){ System.out.println("Minimum word count: "); return minimum; } int setMinWords(int newMinimum){ minimum = newMinimum; return minimum; } int getMaxWords(int maximum){ System.out.println("Maximum word count: "); return maximum; } int setMaxWords(int newMaximum){ maximum = newMaximum; return maximum; } }
Thanks in advance.
- 11-21-2010, 10:39 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
If you declare a class abstract, you can' create an instance of it, like this:
It's ok if you declare a superclass abstract if it only provides a template for other classes, but if you want a usable class, you should not declare it abstract. Another, maybe even better way to do this, is to turn the Question class into an Interface. Read up on it, see for yourself what you want to use.Java Code:public abstract class Example {} public class Test { public static void main(String[] args) { Example e = new Example(); //will not compile } }Ever seen a dog chase its tail? Now that's an infinite loop.
- 11-22-2010, 12:17 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
There doesn't seem to be anything abstract about your EssayQuestion class. As m00nchile says you won't be able to create a "new Essay(min,max)" if you say its abstract.
The methods should be public (if that's your intention) including those in the abstract parent. Additionally you might want to consider making the get/setIntroduction() methods final. (If you actually want subclasses to be able to define their own introduction methods then you could make them abstract and consider making Essay an interface.)
Returning from the setter methods departs from common usage. I can't think of any reason for that since you are returning a value that the caller already knew anyway.
- 11-28-2010, 03:07 PM #4
Member
- Join Date
- Nov 2010
- Location
- Manchester
- Posts
- 2
- Rep Power
- 0
Ok, I'll have a look into these
- 03-05-2011, 09:41 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I don't know who was supposed to PM whom - but I'll point out that Shephard posted twice last Novemeber and since then has migrated wherever his flock might have taken him. It's like that in an electronic forum like this: easy accessibility leads to a lot of (perfectly legitimate) drive by questions.
For all that it is a forum: the question and all of the responses were intended for the entire forum membership, current and future. As such, PMing seems inappropriate.
-
Similar Threads
-
Are abstraction and encapsulation concepts not in C?
By hitesh_public in forum New To JavaReplies: 1Last Post: 08-20-2010, 02:23 PM -
Abstraction
By maya700 in forum New To JavaReplies: 4Last Post: 07-04-2010, 06:26 PM -
diff between abstraction and inheritance
By sandeepsai17 in forum New To JavaReplies: 4Last Post: 05-24-2010, 06:04 AM -
Abstraction
By anurag in forum New To JavaReplies: 6Last Post: 05-15-2010, 06:15 PM -
[SOLVED] Abstraction: Pillar of OOP?
By leiferouis in forum New To JavaReplies: 7Last Post: 01-31-2009, 08:31 PM


LinkBack URL
About LinkBacks


Bookmarks