Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java: Creating a ref/(pointer?) to abstract class

Options
  • 29-11-2006 5:37pm
    #1
    Registered Users Posts: 695 ✭✭✭


    take this simple program:
    public class tester
    {
      public static void main(String[] args)
      {
      Apple apple1;
      // apple1 = new Apple(); // cant do this because Apple is abstract
      }
    }
    
    
    public abstract class Apple
    {
    }
    

    Ok, so I cant create a new Apple object, but I can declare a (is it a pointer or a reference? or are they the same thing?) to the abstract class.

    Question is, what is it pointing too? (null)?

    And whats the purpose of this? So I can point it too objects created in some of Apples subclasses?


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Yes, it's pointing to null.

    Look at it this way (very simplistic view)
    public class tester
    {
      public static void main(String[] args)
      {
    	Apple apple1;
    	// apple1 = new Apple(); // cant do this because Apple is abstract
    	if(args[0] == "grannysmith") 
    		apple1 = new GrannySmith();
    	else 
    		apple1 = new GoldenDelicious();
    	
    	apple1.getColour();
    	apple1.eat();
    	
    	//etc
    
      }
    }
    
    public abstract class Apple
    {
    }
    
    public class GrannySmith implements Apple {
    }
    
    public class GoldenDelicious implements Apple {
    }
    

    Abstract classes are tough to explain until you start to use them - Generally think of an abstract class as a framework that you want people to be able to base their own classes on, but not a class that they can actually use on its own.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    I am pretty sure you must have at the very least one abstract method contained within the abstract class. Concrete methods are optional.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    public abstract class Pedant {
       public abstract string findError(String code);
    }
    
    public class Hobbes implements Pedant {
       public string findError(String code) {
          return "BTW....";
       }
    }
    

    :p


  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    Yep,

    if you have an abstract class, it can't be instaciated ( ie you can't create an object from it ), you can only inherit from it, and in the child class you must implement the abstract method(s)... an abstract class must have atleast one abstract method...

    Hope this helps....


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    seamus wrote:
    public abstract class Pedant {
       public abstract string findError(String code);
    }
    
    public class Hobbes implements Pedant {
       public string findError(String code) {
          return "BTW....";
       }
    }
    

    :p

    You extend classes. You implement Interfaces. :p;)


  • Advertisement
  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    On another note. The best way to think of it is as a Car

    You have an class called Car. It's abstract and has concrete methods for speed up and slow down.

    However these call a method setEngineSpeed(int value)

    Now because the class is top level we don't want to stick the same engine into all the cars but all cars must obey speed up and slow down.

    So you make the method abstract, then when you subclass you build the engine method.

    eg.
    abstract Class Car {
       public SpeedUp(int value) {
          setEngineSpeed(value);
       }
    
       public SlowDown(int value) {
          setEngineSpeed(value * -1);
       }
    
       public abstract void setEngineSpeed(int value);
    }
    
    
    
    Class BMW extends Car {
       public void setEngineSpeed(int value) {
          // add code related to BMW
       }
    }
    
    Class Lada extends Car {
       public void setEngineSpeed(int value) {
          // add code related to Lada
       }
    }
    
    Both need to be able to set the Engine but have different engines.

    Hopefully that made some kind of sense.


Advertisement