aspose file tools*
The moose likes Beginning Java and the fly likes public void abstract methodName() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public void abstract methodName()" Watch "public void abstract methodName()" New topic
Author

public void abstract methodName()

Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 261
Okay I'm looking at java.awt.Graphics and there is a method I'd like to use for this exercise I'm doing out of Java the UML way but the method is abstract. Is it a valid method, since there is a definition/explanation for what the method does, or is it an empty abstract method that would need to be overrided in order to use.

I guess my question is can a method be implemented and still be abstract?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Matt,

If a method is abstract, then somebody has to implement it before you can use it. The key thing to note is that you don't really care who. If you're writing (say) paintComponent(), and you get a Graphics object as an argument, then you can go ahead and call that method, and you know that somebody has implemented the method for you!

The big secret here is that the argument won't actually be a java.awt.Graphics object -- it'll be a (say) sun.awt.win32.Graphics object, or some such thing. It'll be a subclass of Graphics which does, in fact, implement that method.


[Jess in Action][AskingGoodQuestions]
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 261
Originally posted by Ernest Friedman-Hill:
Hi Matt,

If a method is abstract, then somebody has to implement it before you can use it. The key thing to note is that you don't really care who. If you're writing (say) paintComponent(), and you get a Graphics object as an argument, then you can go ahead and call that method, and you know that somebody has implemented the method for you!

The big secret here is that the argument won't actually be a java.awt.Graphics object -- it'll be a (say) sun.awt.win32.Graphics object, or some such thing. It'll be a subclass of Graphics which does, in fact, implement that method.


wow...thats confusing. I'm used to abstract declarations not being implemented until I do it. Why the implicit documentation though? There isn't any mention of how a subclass of Graphics, one that you aren't specifically calling, is the one implementing the method.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

There are actually quite a few things like that in the API. It's easy to spot them: abstract classes, of course, but also classes with no public constructors can be like this.

The entire JDBC (Java database) API is made of interfaces, which are similar to abstract classes. Every database vendor provides their own implementation of these classes, each with a different name; but you refer to them just by the interface names, so you don't need to know about. This lets you write database code that works with any database -- just switch to a different driver, and your code doesn't change at all!
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 261
Originally posted by Ernest Friedman-Hill:
(1)There are actually quite a few things like that in the API. It's easy to spot them: abstract classes, of course, but also classes with no public constructors can be like this.

(2)The entire JDBC (Java database) API is made of interfaces, which are similar to abstract classes. Every database vendor provides their own implementation of these classes, each with a different name; but you refer to them just by the interface names, so you don't need to know about. This lets you write database code that works with any database -- just switch to a different driver, and your code doesn't change at all!


1) What are some examples of classes without public constructors?

2) This would be an example of the "program to an interface" principle right? JDBC is about a week away (less if I get my head out of my butt) so I haven't gotten my hands dirty yet but it would seem that is what you're talking about.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

1) Well, the first one that pops into my head is the java.util.regex.Pattern class. You only get them from the static Pattern.compile() method. But there are plenty of others.

2) Yes, absolutely. "Programming to interfaces" is exactly what we're talking about.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: public void abstract methodName()