Results 1 to 6 of 6
  1. #1
    Shephard is offline Member
    Join Date
    Nov 2010
    Location
    Manchester
    Posts
    2
    Rep Power
    0

    Post 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:
    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;
        }
    }
    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 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;
        }
    }
    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.

    Thanks in advance.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    If you declare a class abstract, you can' create an instance of it, like this:
    Java Code:
    public abstract class Example {}
    public class Test {
      public static void main(String[] args) {
        Example e = new Example(); //will not compile
      }  
    }
    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.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  4. #4
    Shephard is offline Member
    Join Date
    Nov 2010
    Location
    Manchester
    Posts
    2
    Rep Power
    0

    Default

    Ok, I'll have a look into these

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pbrockway2 View Post
    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.
    It was a reanimated zombie with a spam post that you saw and that I've removed.

Similar Threads

  1. Are abstraction and encapsulation concepts not in C?
    By hitesh_public in forum New To Java
    Replies: 1
    Last Post: 08-20-2010, 03:23 PM
  2. Abstraction
    By maya700 in forum New To Java
    Replies: 4
    Last Post: 07-04-2010, 07:26 PM
  3. diff between abstraction and inheritance
    By sandeepsai17 in forum New To Java
    Replies: 4
    Last Post: 05-24-2010, 07:04 AM
  4. Abstraction
    By anurag in forum New To Java
    Replies: 6
    Last Post: 05-15-2010, 07:15 PM
  5. [SOLVED] Abstraction: Pillar of OOP?
    By leiferouis in forum New To Java
    Replies: 7
    Last Post: 01-31-2009, 09:31 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •