aspose file tools*
The moose likes Beginning Java and the fly likes How can I use abstract class methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

How can I use abstract class methods

Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

Hi firends
I have a abstract class and I want to use someof methods from abstract class. I mean I want to to use abstract class methods in other class
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

If you want to use the abstract methods in another class, you might consider making it into an interface.


When you do things right, people won't be sure you've done anything at all.
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

well Thanks for your reply and Here class is only abstract and methods are not but defined methods in abstract class are returning something and I want to use that
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
You simply need to create a subclass which extends from the abstract class. Then using the instance of this subclass you can use the defined methods of the abstract class.


Thanks and Regards,
Suhas
http://www.xplore-java.blogspot.com/
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Suhas is right. You can call abstract methods of an abstract class without problems. This is because you can only instantiate concrete classes, and concrete sub classes of abstract classes have implemented the abstract methods.

Consider:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

thats quiet hepfull, and bit confused also and actually I have a file names Hello.java which is a abstract class and I have a one method init and I have onemore class called use.java and both are in same packeage and I want to use method in Hello.java which is a abstract class in Use.java How can I do that?
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
Did you understand Rob's sample? If not, read it again. If you did, your problem should be solved. But to help us to help you, you should post your code here...
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

oh.forgive my madness and Here is the code

I want to use routePlan method in below class named mySimulator.java



Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
Get a reference to an Object that extends MovingObject and call the method. Do you have a concrete class that can be instantiated?
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

sorry,I didnt get you can you give example using my code
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188




That's what Rob's sample was about...
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

But got the error which says
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
There is a method type() in RescueObject you have to implement in order to get a concrete class - as long as you don't you'll never ever get a class that not abstract.
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

what I have to implement...No idea about that
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
There is an abstract method in RescurObject: public abstract int type();

You have to implement this method in your subclass.

public int type(){
return 12345;
}

And that's only an example! May be should should read some tutorials about basic java concepts.
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

okay thanks but got this error




Use.java
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Are you compiling using the correct classpath?
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

yes I am compiling with cirrect classpath only this is is classpath

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That's a path, not a classpath.
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

Here it is my Classpath correct me if this is wrong

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yes; you have no reference to the classes you've already compiled.

Note that most here will encourage you *not* to set the classpath environment variable, as it can cause a lot of confusion. It's often easier to supply the classpath directly to the Java compiler.
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

sorry for late reply I have been quite busy with some other work ,as you said about supplying classpath to the java compiler, here there is a makefile to compile all java programs at once and I am confused how to supply the classpath at compilation time

here is makefile and makefile.in

Makefile



Makefile.in


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

A make file?!

Um... hmm. Interesting. Baffling, but interesting.

In any case, see where you pass CLASSPATH to javac? Put your classes in it.
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

I compiled the program successfulyy but I was unable to use the values returned by the method which is in abstract class. I dont no whats wrong with this code ..

MovingObject.java



So I created one class which extends MovingObject.java and named as myMovingObject.java
But Its not calling any methods in superclass!!!
myMovingObject.java



mySimulator.java



Any Idea whats wrong with this???
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

srinivas miriyala wrote:Any Idea whats wrong with this???

1) Classes myMovingObject and mySimulator should start with a capital M.

2) mySimulator uses the constant interface anti-pattern.

3) your only assignment to m_routePlan occurs on line 21: "m_routePlan = mv.routePlan();". mv's own copy of m_routePlan has never been assigned though, so it still returns null. mv gets created on line 20. Its constructor does not set its m_routePlan field so it stays null. You then ask for it on line 21, and since it's null your object's m_routePlan field is set to null as well.
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

thanks for your reply and what am I suppose to do to overcome this problem?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
You will have to find out about the constant interface anti-pattern and alter your interfaces accordingly.
You will have to alter the constructor to make sure the route plan is instantiated.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

The latter being the most important advice. The anti-pattern doesn't prevent your program from working, it's just bad style. And yes, Sun has used this bad style as well (in Swing among others. SwingConstants, sight...).
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

Can somebody tell me the solution for my problem
Thanks in advance
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Campbell wrote:You will have to alter the constructor to make sure the route plan is instantiated.
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

thanks for you reply and I didn't get it can you explain with example
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Instantiate (create, make a new one) the route plan in the constructor.

Rather than us just giving you the code, try something and see what you come up with--if you run into an issue, we can help with specific issues.
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

You mean I need to crate one constructor then I need to instantiate the routeplan in this constructor right
Rahul Kurup
Greenhorn

Joined: Dec 11, 2009
Posts: 9
When you create a new object as

MovingObject mo = new Use();

constructor for Use() will be called which will in turn invoke the parent class constructor(MovingObject). Now MovingObject does not have a zero argument constructor. Also
Use does not have a constructor that calls super().

Could this be the problem? Experts please confirm.



Rahul J.
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

I have been working on this past two weeks but no progress and I really appriciate if somebody could sort the problem..experts I really Need your valuable suggestions thanks in advance
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I would *strongly* suggest starting a new thread, posting your complete code, and asking some very specific questions--this thread has gotten very difficult to follow, and I'm not even sure what problem you're having at this point.

Normally I wouldn't, but I'm going to lock this thread to make you start over. Before posting the new thread, I'd make sure your code is as clean as possible, formatted nicely, and so on. It'll also give you an opportunity to look over your existing code and figure out exactly what's wrong and what you need to ask. You may be able to answer your own question, too--sometimes taking a step back is very helpful.

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I use abstract class methods
 
Similar Threads
What is an abstract class? (was: scjp)
Exact Use of Abstract Class And Interface
abstract class with concrete methods
abstract class execution
Regarding abstract class