Results 1 to 2 of 2
  1. #1
    romina is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default I want to add function

    Hi, i have following code:

    Java Code:
    public class Building {
    
    protected String name;
    
    A(String name){
    this.name = name;
    }
    public void printSpecs() {}
    }
    and two inherited classes :


    Java Code:
    public class House extends Building {
    protected double maxDimension;
    House (String name, double maxDimension) {
    super(name);
    this.maxDimension= maxDimension;
    }
    
    public void printSpecs()
    {
    System.out.println(maxDimension);
    }
    }
    
    public class Castle extends Building {
    
    protected double maxWeight;
    
    Castle (String name, double maxWeight) {
    super(name);
    this.maxDimension= maxWeight;
    }
    
    public void printSpecs()
    {
    System.out.println(maxWeight);
    }
    }
    I have array of objects
    Java Code:
    HashMap <int, Building > builds = new HashMap<int, Building >();
    and some of them are of type "House" and some "Castle"

    when i do following:
    Java Code:
    Building myBuild = builds .get(x);
    myBuild.printSpecs();
    everything works: specs are printed according to class type.

    Now, the question: I want to add function
    Java Code:
    int GetSpecs() {}
    to "father" class "Building" so that result will be returned by inherited class (similar to function printSpecs)
    But, when i add such empty function i get error "function GetSpecs must return results".
    So, what's the right syntax/way to do it ?
    Thanks

  2. #2
    trill is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    The reason it asks for a return type is because you can't have an empty method with a return type of int.

    Java Code:
    int GetSpecs() {}
    You could only do this if the class was abstract (if I remember right). but if it was abstract then you would want to implement it in the child classes.

    Greetings.

Similar Threads

  1. use of onclick function
    By m4tt in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 04:03 AM
  2. How to use a function in java
    By olikhvar in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 03:57 AM
  3. Need a little help with a function!
    By Nuluvius in forum New To Java
    Replies: 3
    Last Post: 02-08-2008, 12:33 AM
  4. function name
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 09:56 PM
  5. Help with the Dummy Function
    By Felissa in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-06-2007, 06:03 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
  •