Results 1 to 15 of 15
  1. #1
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Preforming a method on an instance inside an abstract class

    How do I create an instance of a class in a method?

    I am a bit rusty whenever I think of instances. I always think of main method and objects when I see instance which gets me confused on what to do when I am not in a main method. The example:

    I have a abstract class, School, and inside School I have some methods that must preform some action on an instance. For example, there is a move() method that must move the instance of School. Another method named, personOld(), which returns whether or not an instance of School surpassed some determined age.
    How do I do this and create this instance?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Preforming a method on an instance inside an abstract class

    You can't create an instance of an abstract class. You have to extend the abstract class and create an instance of that (ignore anonymous classes for now). You mention a move() method to move an instance of School. You can either declare it abstract and let the subclass provide the details or provide an implementation in the abstract class. Either way, the signature will probably look something like this:

    Java Code:
    abstract public void move(School school);
    // or
    public void move(School school) {
       // implementation code
    }
    Also, a method in an abstract class (or super class in general) cannot create an instance of the subclass since the super class knows nothing about its descendants. Imho, if you're set on using a method to create an instance just declare a static method in your subclass to return an instance of the subclass. You can still pass this instance to the move method since MySchool inherits from School.

    Java Code:
    public class MySchool extends School {
        public static MySchool createInstance() {
          return new MySchool();
        }
    }
    Regards,
    Jim
    Last edited by jim829; 03-12-2015 at 04:14 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Preforming a method on an instance inside an abstract class

    Quote Originally Posted by jim829 View Post
    You can't create an instance of an abstract class. You have to extend the abstract class and create an instance of that (ignore anonymous classes for now). You mention a move() method to move an instance of School. You can either declare it abstract and let the subclass provide the details or provide an implementation in the abstract class. Either way, the signature will probably look something like this:

    Java Code:
    abstract public void move(School school);
    // or
    public void move(School school) {
       // implementation code
    }
    Also, a method in an abstract class (or super class in general) cannot create an instance of the subclass since the super class knows nothing about its descendants. Imho, if you're set on using a method to create an instance just declare a static method in your subclass to return an instance of the subclass. You can still pass this instance to the move method since MySchool inherits from School.

    Java Code:
    public class MySchool extends School {
        public static MySchool createInstance() {
          return new MySchool();
        }
    }
    Regards,
    Jim
    What if my move method cannot have parameters? Will my only chose then will make the move method abstract and provide the details in the subclass? If that is my only choice then how can I create the instance of House? Will I do the following:

    Java Code:
    public class Student extends School {
        
        public void move() {
            House newHouse = new House(int xPos, yPos, Rectangle bounds);
            for (int i = 0; i < 10; i ++) {
                 this.xPos +=
                 ....
            }
        }
    }
    Last edited by Robben; 03-12-2015 at 04:33 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Preforming a method on an instance inside an abstract class

    public class Student extends School {
    Something is heading in altogether the wrong direction here. A student is not a school, he or she is certainly not any particular type of school. Any code that follows this will be contrived at best.

    It might be easier if you said what you were tying to do, and not how you were trying to do it with this collection of classes.

  5. #5
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Preforming a method on an instance inside an abstract class

    Quote Originally Posted by pbrockway2 View Post
    Something is heading in altogether the wrong direction here. A student is not a school, he or she is certainly not any particular type of school. Any code that follows this will be contrived at best.

    It might be easier if you said what you were tying to do, and not how you were trying to do it with this collection of classes.
    Alright, I will rephrase things. I have a super class which is abstract named, House (we can forget about School because this will be a better example and what I am trying to code). The House class has other members (which are subclasses of House) that compete with one another, they also reproduce, get old and much more. In my abstract super class House, I have methods that preforms some type of action on instances of House. These methods are: move(), collideswithHouse(House otherHouse), canReproduceWithHouse(House otherHouse), die(), isOld(), and more.

    I have a constructor in House which takes in three parameters, i.e.

    Java Code:
    public House(int xPos, int yPos, Rectangle bound) {
    }
    My problem lies in using instances. For example, the move() method, in House, will move the instance of a House in some made up random manner (the GUI is in another class which is already finished). Another example will be, isOld(), which returns whether or not an instance of a House has surpassed some determined maximum age. My question is, how do I create these instances to move the instance of a House and returns whether or not an instance of a House surpassed max age?

    Also, I am almost positive move() should not be abstract because all subclasses will move the same way.
    Last edited by Robben; 03-12-2015 at 05:26 AM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Preforming a method on an instance inside an abstract class

    My question is, how do I create these instances to move the instance of a House and returns whether or not an instance of a House surpassed max age?
    You don't. That is to say, you must already have an instance of House before you can tell it to move, or ask it if it is old. Instances of classes are pretty much like things of a given type in this respect. Likewise methods like collideswithHouse(House otherHouse) cannot be called unless you have two instances of House.

    It's up to your program (and hence your intent) how the instances of House get created. I would imagine since you mention that reproduce() is a method, that this method could return instances (your other thread discussed this, and how to deal with the fact that House is abstract). You may also find it useful to construct various instances of House when the program starts.

    Abstract classes can have ordinary (nonabstract) methods. Generally this is the case when you intend the abstract class to define behaviour you want all subclasses to implement in the same way, or at least provide them with a default behaviour.

  7. #7
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Preforming a method on an instance inside an abstract class

    Quote Originally Posted by pbrockway2 View Post
    You don't. That is to say, you must already have an instance of House before you can tell it to move, or ask it if it is old. Instances of classes are pretty much like things of a given type in this respect. Likewise methods like collideswithHouse(House otherHouse) cannot be called unless you have two instances of House.
    But how can I create this instance of House, in the House class, in order for it to move or ask it if it is old?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Preforming a method on an instance inside an abstract class

    That's where your (for want of a better term) World logic comes in.
    Your Houses have very limited logic, all based around themselves, which is correct. They're terribly insular.
    But those Houses "live" somewhere, and that somewhere is your application (or World, if you prefer thinking in game terms).

    So, where do these Houses live?
    Create a class that models that place.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Preforming a method on an instance inside an abstract class

    Am I the only one that is confused about a School class having a move() method? Generally schools, or buildings in general, stay put.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Preforming a method on an instance inside an abstract class

    Quote Originally Posted by gimbal2 View Post
    Am I the only one that is confused about a School class having a move() method? Generally schools, or buildings in general, stay put.
    I tend to filter out that sort of thing...:)
    Anyway, castle's can move:
    Howl's Moving Castle (film) - Wikipedia, the free encyclopedia
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Preforming a method on an instance inside an abstract class

    Such A brilliant fantasy movie.

    When your programs start to mimic fantasy movies, something is horribly wrong :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Preforming a method on an instance inside an abstract class

    Thank you guys. I used polymorphism to do this, so I had to tweak my code a bit but I want to make it more efficient.

    Now in my abstract House class I want an efficient way to go through each instance of my subclass so that it can preform the method move() on the object. Instead of doing

    Java Code:
    House subClass = new subClass(this.xPos, this.yPos, bounds);
    for each subclass of house, how can I put those subclass in a list or an array efficiently? I searched online and found something like

    Java Code:
    private ArrayList<House> houseObjects = new ArrayList<House>();
    but I don't know how that works exactly and not sure how to apply it?




    Java Code:
    private ArrayList<House> houses = new ArrayList<House>();
    What does this code do?

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Preforming a method on an instance inside an abstract class

    As soon as people start to just copy/paste stuff, I immediately give up. When you start to do things the right way again, study and understand first, write code as a result of it, then I'll start to take an interest again.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Preforming a method on an instance inside an abstract class

    I used polymorphism, and as you can see it isn't very efficient. I was wondering is there some efficient way to go about doing this? I want an efficient way to go through each instance of my subclass so that it can preform the method move() on the object. Instead of doing:

    Java Code:
    public void move() {
            House subclass1 = new subclass1(this.xPos, this.yPos, bounds);
            House subclass2 = new subclass2(this.xPos, this.yPos, bounds);
            House subclass3 = new subclass3(this.xPos, this.yPos, bounds);
            House subclass4 = new subclass4(this.xPos, this.yPos, bounds);
            House subclass5 = new subclass5(this.xPos, this.yPos, bounds);
            House[] houseObjects = new House[4];
            houseObjects[0] = subclass1;
            houseObjects[1] = subclass2;
            houseObjects[2] = subclass3;
            houseObjects[3] = subclass4;
            houseObjects[4] = subclass5;
            for (House obj : houseObjects) {
                obj.helperMove();
            }       
        }
    
        private void helperMove() {
            for (int i = 0; i < 20; i++) {
                int randomNum = rand.nextInt(5) + 1;
                xPos+= i + randomNum;
                yPos+= i + randomNum;
            }
        }

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Preforming a method on an instance inside an abstract class

    Quote Originally Posted by Robben View Post
    I used polymorphism, and as you can see it isn't very efficient. I was wondering is there some efficient way to go about doing this? I want an efficient way to go through each instance of my subclass so that it can preform the method move() on the object. Instead of doing:

    Java Code:
    public void move() {
            House subclass1 = new subclass1(this.xPos, this.yPos, bounds);
            House subclass2 = new subclass2(this.xPos, this.yPos, bounds);
            House subclass3 = new subclass3(this.xPos, this.yPos, bounds);
            House subclass4 = new subclass4(this.xPos, this.yPos, bounds);
            House subclass5 = new subclass5(this.xPos, this.yPos, bounds);
            House[] houseObjects = new House[4];
            houseObjects[0] = subclass1;
            houseObjects[1] = subclass2;
            houseObjects[2] = subclass3;
            houseObjects[3] = subclass4;
            houseObjects[4] = subclass5;
            for (House obj : houseObjects) {
                obj.helperMove();
            }       
        }
    
        private void helperMove() {
            for (int i = 0; i < 20; i++) {
                int randomNum = rand.nextInt(5) + 1;
                xPos+= i + randomNum;
                yPos+= i + randomNum;
            }
        }
    One doesn't just choose to use polymorphism. It is simply a way of describing normal aspects of OO programming. And stop worrying about efficiency.

    And this will cause a problem:

    Java Code:
     House[] houseObjects = new House[4]; // four elements
      ...       
     houseObjects[4] = subclass5; // adding fifth element
    Why not populate the array the same way your print it out (except use the normal for loop)?

    And read up on lists --> The List Interface (The Java™ Tutorials > Collections > Interfaces)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 6
    Last Post: 09-21-2012, 09:12 PM
  2. How to invoke method present in abstract class?
    By java_no1 in forum New To Java
    Replies: 3
    Last Post: 06-01-2011, 07:13 AM
  3. abstract method-abstract class
    By durgaprasad1407 in forum New To Java
    Replies: 1
    Last Post: 04-29-2011, 06:58 PM
  4. Replies: 4
    Last Post: 04-14-2011, 11:42 AM
  5. Replies: 6
    Last Post: 02-11-2009, 10:51 AM

Posting Permissions

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