Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can I use abstract class methods

 
Ranch Hand
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
If you want to use the abstract methods in another class, you might consider making it into an interface.
 
Vas Miriyala
Ranch Hand
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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:
 
Vas Miriyala
Ranch Hand
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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?
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
sorry,I didnt get you can you give example using my code
 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator




That's what Rob's sample was about...
 
Vas Miriyala
Ranch Hand
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
But got the error which says
 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
what I have to implement...No idea about that
 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
okay thanks but got this error




Use.java
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Are you compiling using the correct classpath?
 
Vas Miriyala
Ranch Hand
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
yes I am compiling with cirrect classpath only this is is classpath

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
That's a path, not a classpath.
 
Vas Miriyala
Ranch Hand
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Here it is my Classpath correct me if this is wrong

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
thanks for your reply and what am I suppose to do to overcome this problem?
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Can somebody tell me the solution for my problem
Thanks in advance
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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

 
Vas Miriyala
Ranch Hand
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
thanks for you reply and I didn't get it can you explain with example
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You mean I need to crate one constructor then I need to instantiate the routeplan in this constructor right
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.

 
Vas Miriyala
Ranch Hand
Posts: 114
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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!
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic