Results 1 to 14 of 14
  1. #1
    srinivas2828 is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default How can I call abstract class methods from another class

    Hi experts I have a problem that I couldn't able to figureit so I thought to post in a forum so the problem is I have one abstract class(class only abstract) not any of methods in this calss are anstract ,then I want to call some of methods in my own class which extends from abstract class.But the thing is that I called the methods but its returning null for all methods which I am calling . So tell me whats wrong with this code.

    MovingObject.java which is a abstract class and I have written one more class init which extends from abstract class
    Java Code:
    package traffic.object;
    import java.io.*;
    import java.util.*;
    import traffic.*;
    import rescuecore.OutputBuffer;
    import rescuecore.RescueConstants;
    
    public abstract class MovingObject extends RealObject implements Obstruction {
        public MovingObject(int id) {
            super(id);
        }
    
        private int m_position;
        private double m_positionExtra;
        // private int m_direction;
        // private int[] m_positionHistory;
        private Route m_routePlan;
    
        public void setRoutePlan(Route route) {
            m_routePlan = route;
        }
    
        public Route routePlan() {
    	System.out.println("routeplan = " + m_routePlan);       
    	return m_routePlan;
        }
    }
    
    class MyMovingObject extends MovingObject{
        public MyMovingObject(int id) {
    	super(id);
        }
    
        public MyMovingObject() {
    	this(0);
        }
        public int type() {
    	rerurn 1;
        }
    
        public void Calculate() {
    	MovingObject mv = new MyMovingObject();
    	mv.routePlan();
    	System.out.println(mv.routePlan());
        }
    }
    Now I am calling some methods from this class names Simulator.java
    Java Code:
    package traffic;
    import java.net.*;
    import java.util.*;
    import traffic.object.*;
    
    
    public class Simulator extends MyMovingObject implements Constants {
        private final IO io;
    	private int id;
    
        public Simulator(InetAddress kernelAddress, int kernelPort) {
            io = new TCPIO(kernelAddress, kernelPort);
            io.sendConnect();
            id = io.receiveConnectOk();
            System.out.println("Connected. Simulator ID is " + id);
            io.sendAcknowledge();
         
        }
    
    
    
        public void simulate() {
            System.out.println("start");
            while (true) {
    	    System.out.println(" receiving commands from kernel"); 
               io.receiveCommands();
                System.out.println("time: " + WORLD.time());
                move();
    	    System.out.println("sending updates to kernel");
                io.sendUpdate();
    	    System.out.println("receiving updates from kernel");
                io.receiveUpdate();
            }
        }
        
        public void move() {
    	MyMovingObject mo = new MyMovingObject();
    	mo.Calculate();
    	System.out.println("working on that");
        }
    }
    experts please help me out this problem..when I am callingmethods its returning Null...
    Thanks in Advance

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    312
    Rep Power
    10

    Default

    Java Code:
        public void Calculate() {
    	MovingObject mv = new MyMovingObject();
    	mv.routePlan();
    	System.out.println(mv.routePlan());
        }
    Why are you creating a new instance of MyMovingObject, just use the current one you are in. And where do you set the m_routePlan variable?

  3. #3
    srinivas2828 is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default How can I call abstract class methods from another class

    well Thanks for your reply I did not set the variable because just calling that method will return the routeplan and it will print(I guess so) if I am set the variable it is giving erorrs

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    312
    Rep Power
    10

    Default

    I'm guessing its the routePlan() method is the method returning null right?

    What method/s are returning null? Usually this is because something isn't set correctly.

  5. #5
    srinivas2828 is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default How can I call abstract class methods from another class

    exactly Stormy Waters
    the methods which I am calling from abstract class all re returning Null values...
    this is what I am trying to figure it out..

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    312
    Rep Power
    10

    Default

    Is it possible that its returning null because the value is actually null?

    As far as I can tell, you are never setting the m_routePlan variable anywhere, so it is indeed null. The method is getting called correctly.

  7. #7
    srinivas2828 is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default How can I call abstract class methods from another class

    I called saperately from main program it returning list of objects but m_routePlan will initialize in the abstract class itself no(correct me fi I am wrong) ,In that abstract class we have two methods which one is use to set the routeplan and another one is to return the routeplan, btw I am calling only routePlan method from abstract class,Do I need to call setRoutePlan method before call to routePlan method?

  8. #8
    srinivas2828 is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default How can I call abstract class methods from another class

    The thing is that I need to call setRoutePlan method before calling routePlan so I did it.but this time I am getting zero instead of Null ..

    changes I made to the Code
    Java Code:
     public void move() {
    	MyMovingObject mo = new MyMovingObject();
    	Route route = new Route(1);
           	mo.setRoutePlan(route);
    	//	System.out.println(route);
    	System.out.println(" this is the routeplan =" + mo.routePlan());
    	System.out.println("working on that");

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

  10. #10
    srinivas2828 is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default How can I call abstract class methods from another class

    Because I want to use methods in abstract class . Actually I want to call some methods in Abstract class and I have written one class which extends abstract class so sub-class will have all the methods which are in abstract class.And I have written one more class Simulator.java which extends from sub-class,and I am calling sub-class methods in this class only.
    there are the codes

    MovingObjec.java
    Java Code:
    public abstract class MovingObject extends RealObject implements Obstruction {
        public MovingObject(int id) {
            super(id);
        }
    
        private int m_position;
        private double m_positionExtra;
        private Route m_routePlan;
    
        public void setRoutePlan(Route route) {
    	m_routePlan = route;
        }
    // I want to call this method and I want to use this method returned value in Simulator.java
        public Route routePlan() {
    	// System.out.println("then second this to get route plan");
    // 	System.out.println("routeplan = " + m_routePlan);       
    	return m_routePlan;
        }

    I have written sub-class which extends from super class(MovingObjec.java) Now this class will contain all the methods which are in superclass
    MyMovingObject.java
    Java Code:
    public class MyMovingObject extends MovingObject {
        public MyMovingObject(int id) {
    	super(id);
        }
        
        public MyMovingObject() {
    	this(0);
        }
        public int type() {
    	return 1;
        }
        
    }
    Now I have written one more class which extends from MyMovingObject.java so I am calling some methods to perform tasks
    Simulator.java
    Java Code:
    public class Simulator extends MyMovingObject implements Constants {
        private final IO io;
        private int id;
        
    
        public Simulator(InetAddress kernelAddress, int kernelPort) {
            io = new TCPIO(kernelAddress, kernelPort);
            io.sendConnect();
            id = io.receiveConnectOk();
            System.out.println("Connected. Simulator ID is " + id);
            io.sendAcknowledge();
    	
        }
        
        
        
        public void simulate() {
            System.out.println("start");
            while (true) {
    	    //System.out.println(" receiving commands from kernel"); 
    	    io.receiveCommands();
                System.out.println("time: " + WORLD.time());
                move();
    	    //System.out.println("sending updates to kernel");
                io.sendUpdate();
    	    //    System.out.println("receiving updates from kernel");
                io.receiveUpdate();
            }
        }
        
        public void move() {
    	MyMovingObject mo = new MyMovingObject();
    	Route route = new Route(1);
           	mo.setRoutePlan(route);
    	//	System.out.println(route);
    	System.out.println(" this is the routeplan =" + mo.routePlan());
    	System.out.println("working on that");
    	
    	
        }
    }

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

    Default

    Quote Originally Posted by srinivas2828 View Post
    Because I want to use methods in abstract class .
    But is Simulator a MovingObject, or should it have MyMovingObject objects? I fear that you're mistaken on what inheritance is for as having Simulator subclass MyMovingObject may not be how you do this. Instead perhaps you should use a "has-a" relationship, not an "is-a" relationship.
    Last edited by Fubarable; 03-12-2010 at 03:29 PM.

  12. #12
    srinivas2828 is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default How can I call abstract class methods from another class

    No,Simulator Have MyMovingObjects. I did a silly mistake which I am not able to figureit out for the couple of days..., I have one silly question before I aks,I should say sorry for this.fogive my poor java knowledge

    First I am extending sub-clss from super class this is okay,but I am again extending one more class from sub-class,is this Right?

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

    Default

    Quote Originally Posted by srinivas2828 View Post
    First I am extending sub-clss from super class this is okay,but I am again extending one more class from sub-class,is this Right?
    In many situations, yes this is fine.

    Does it apply to your situation? Myself, I don't have enough information to answer this.

    Best of luck.

  14. #14
    srinivas2828 is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default How can I call abstract class methods from another class

    okay thanks for your support

Similar Threads

  1. How can I call method from class in other class??
    By Hisham in forum New To Java
    Replies: 6
    Last Post: 02-14-2010, 04:49 PM
  2. Replies: 6
    Last Post: 02-11-2009, 11:51 AM
  3. Abstract Class with Static Methods
    By bugger in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 01:20 AM
  4. Replies: 0
    Last Post: 04-02-2008, 08:04 AM
  5. Replies: 0
    Last Post: 01-02-2008, 02:52 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
  •