Results 1 to 14 of 14
- 03-11-2010, 01:36 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
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()); } }
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"); } }
Thanks in Advance
- 03-11-2010, 03:24 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 312
- Rep Power
- 12
Java Code:public void Calculate() { MovingObject mv = new MyMovingObject(); mv.routePlan(); System.out.println(mv.routePlan()); }
- 03-11-2010, 04:28 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
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
- 03-11-2010, 04:32 PM #4
Senior Member
- Join Date
- Feb 2009
- Posts
- 312
- Rep Power
- 12
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.
- 03-11-2010, 04:35 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
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..
- 03-11-2010, 04:37 PM #6
Senior Member
- Join Date
- Feb 2009
- Posts
- 312
- Rep Power
- 12
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.
- 03-11-2010, 04:42 PM #7
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
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?
- 03-12-2010, 02:47 PM #8
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
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");
-
Why does Simulator extend MyMovingObject? Perhaps you need to show more code.
- 03-12-2010, 03:13 PM #10
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
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; } }
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"); } }
-
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.
- 03-12-2010, 03:25 PM #12
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
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?
-
- 03-12-2010, 03:33 PM #14
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
How can I call method from class in other class??
By Hisham in forum New To JavaReplies: 6Last Post: 02-14-2010, 04:49 PM -
Difference between Abstract class having only abstract method and a Interface class
By Santoshbk in forum New To JavaReplies: 6Last Post: 02-11-2009, 11:51 AM -
Abstract Class with Static Methods
By bugger in forum New To JavaReplies: 7Last Post: 09-05-2008, 01:20 AM -
what is the Priority for execution of Interface class and a Abstract class
By Santoshbk in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 08:04 AM -
How can I call java class methods in Oracle 10g PL/SQL
By searcher34 in forum JDBCReplies: 0Last Post: 01-02-2008, 02:52 PM
Bookmarks