• Post Reply 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

Abstact Class

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Erik Pragt
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And when it already extends one? (it does actually....)
Erik
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic