Results 1 to 5 of 5
  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default Abstract class question

    I made a an application that declares an abstract class Fruit and 4 subclasses Apple, Banana, Strawberry, and Orange. Then basically I just display all of their colors. It is working fine...but I'm a little confused. I thought with abstract classes, you could have no implementation. I put in a getColor() method in Fruit with return color;. This is what made the app work properly. But I'm confused as to why that works when the Fruit class is abstract. Here is all of my code --
    Java Code:
    abstract public class Fruit {
    	
    	String color;
    	String getColor()
    	{
    		return color;
    	}
    
    }
    Java Code:
    public class Apple extends Fruit{
    
    	String color;
    	
    	public Apple(String color)
    	{
    		this.color = color;
    	}
    	
    	String getColor()
    	{
    		return color;
    	}
    }
    the same code for the other subclasses but with different name obviously...

    and

    Java Code:
    public class SHowingoff {
    	
    	public String toString(Fruit fruit)
    	{
    		String x;
    		x = fruit.getClass() + ", " + fruit.getColor();
    		return x;
    	}
    	
    	public static void main(String args[])
    	{
    		SHowingoff x = new SHowingoff();
    		Fruit a = new Apple("red");
    		Fruit b = new Banana("yellow");
    		Fruit o = new Orange("orange");
    		Fruit s = new Strawberry("red");
    		System.out.println(x.toString(a));
    		System.out.println(x.toString(b));
    		System.out.println(x.toString(o));
    		System.out.println(x.toString(s));
    		
    		
    		
    	}
    	
    
    }

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    for abstract class, general it should have abstract method
    abstract void getColor();

    then, all subclass implement getColor

    subclass override superclass methods.
    Last edited by mtyoung; 02-27-2009 at 05:13 AM.

  3. #3
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default

    so does return not count as implementation?

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    abstract classes can implement methods.

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

Similar Threads

  1. Replies: 6
    Last Post: 02-11-2009, 10:51 AM
  2. abstract class
    By makpandian in forum New To Java
    Replies: 1
    Last Post: 12-12-2008, 03:11 PM
  3. Replies: 0
    Last Post: 04-02-2008, 07:04 AM
  4. Abstract Class question
    By maa11235 in forum New To Java
    Replies: 1
    Last Post: 01-05-2008, 10:30 PM
  5. question about abstract methods
    By osval in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:36 PM

Posting Permissions

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