my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Abstact Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstact Class" Watch "Abstact Class" New topic
Author

Abstact Class

Erik Pragt
Ranch Hand

Joined: Sep 08, 2001
Posts: 125
Hello everyone,
I have a small question, but I can't figure out the answer. My question is the following:
I want to make an abstract class, which e.g. displays a message.
The Class is in another package.

I want to call this class above in my Java program...

Now how do I call the method of the testclass file??? I have no idea, so if someone could help me??
Thanks in Advance,
Erik
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Erik
In the class that extends your abstract class each instance of that class will have al of the methods contained in the abstract class, so you would just call it like a normal method. Like this:

hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
Don Gardner
Greenhorn

Joined: Oct 09, 2001
Posts: 25
Your abstract class textClass does not have any abstract methods that the user must define.
An abstract class is sort of like a template for subclasses. It defines one or more abstract methods that must be defined before it can be instanciated. Your sample code may be valid (I'm not sure), but it doesn't define any method that must be overridden.
code:

Then you would define the abstract method in a new subclass.

Then to use the method you would instanciate the class and use them method, something like:
TestClassB a = new TestClassB() ;
a.printMsg() ;
or
new TestClassB.printMsg() ;
I hope this helps.
Erik Pragt
Ranch Hand

Joined: Sep 08, 2001
Posts: 125
hmmm, I think I'm really of course then, cause all I wanted was calling a method in another class without making a reference to it.
Can anyone explain me how to do that than??
Erik
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
Simply have TestClassB extend the class whose methods you want to use. Then any object of TestClassB can call the methods of the super class.
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
http://www.geocities.com/mjbruesch


Michael J Bruesch<br /><i>I code, therefore I am.</i>
Erik Pragt
Ranch Hand

Joined: Sep 08, 2001
Posts: 125
And when it already extends one? (it does actually....)
Erik
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
Ok, say you have the following code:

This program would print "Inside superMethod." when run. If the child class extends the super class, then objects of type Child can call methods of class Super. Is that what you're asking? Also, you can implement an abstract class, but not all the methods in the abstract class can be defined in the abstract class, it is not an abstract class if all methods are implemented. And the extending class must define all methods that are not implemented in the abstract class.
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
http://www.geocities.com/mjbruesch
Erik Pragt
Ranch Hand

Joined: Sep 08, 2001
Posts: 125
Well, I can give you a small example with what I really would like.

And

So what I'd really like to do, is to call the printMSG from the abstractClass class, without making a reference to it. Now my question is: is this possible??
Erik
(PS, the above code fails ofcourse, else I would already have the solution...)
Erik Pragt
Ranch Hand

Joined: Sep 08, 2001
Posts: 125
At last, I found it.
This is my solution, the thing I really wanted...I was completly wrong with my abstract class idea, it had nothing to do with it. The solution was to use a static modifier. Sorry sorry sorry, I must be stupid!
here the working code:

And...

This did it...I want to thank you all for your kind help!!
Greetings, Erik

[This message has been edited by Erik Pragt (edited October 23, 2001).]
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
Well, as Cindy said before, you're not stupid when you discover the answer to your own question, that's called learning . Glad you found what you were looking for .
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
http://www.geocities.com/mjbruesch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstact Class