
Normal, or concrete, classes in Java can be instantiated using the new keyword. Concrete classes both define the functionality, that is what the class does, and the interface, that is what methods are available for you to call. Most of the classes you write will be concrete classes like the one below.
public class MyConcreteClass {
public void perform() {
...
}
}
At the other end of the spectrum we have an interface which only defines which methods we can call on it – there is no functionality in there. The interface in the example below specifies that a class implementing the interface will at least have the 4 listed methods. Notice that there is no method bodies (the compiler will enforce this).
public interface Car {
public void increaseSpeed();
public void decreaseSpeed();
public void turnRight();
public void turnLeft();
}
By creating a class and inserting the keyword “abstract” after the class visibility modifier (public, private, “friendly”) makes the class into an abstract class. An abstract class is somewhere in between an interface and a class, that is a class where some of the functionality is specified but not all. Think of an abstract class as a class with some blanks that needs to be filled in by any, non-abstract, class that extends it. An abstract class cannot be instantiated using the new keyword.
public abstract class MyAbstractClass {
public class perform() {
this.doStuff();
}
public abstract void doStuff();
}
How do I use an interface?
To use an interface you have your class implement it. Implementing an interface means making sure that the class specifies method bodies for all the methods in the interface. If you do not implement all the methods in the interface the compiler will make you mark the class as abstract.
public class Ford implements Car {
public void increaseSpeed() {
...
}
public void decreaseSpeed() {
...
}
public void turnRight() {
...
}
public void turnLeft() {
...
}
}
How do I use an abstract class?
An abstact class cannot be instantiated because some of the functionality (for example the doStuff() method in the example above) isn’t implemented. To use the class we need to extend it and implement any missing, abstract, methods.
public class MyClass extends MyAbstractClass {
public void doStuff() {
...
}
}
So when should I use which?
Well the real answer is it depends…
An abstract class is a good choice when you do most of the programming in the abstract class and leave only one or a couple of methods to sub-classes. An example could be a class that reads data from a file. The abstract class defines the actual reading and processing of the file and concrete sub-classes define how to get the file (from the local disk, via HTTP, via FTP etc.):
public abstract class FileProcessor {
// declarations
private File file = null;
public process() {
// get file
this.file = this.getFile();
// open file
this.openFile();
// decrypt file
this.decryptFile();
// do other stuff with the file
...
...
}
private void openFile() {
...
}
private void decryptFile() {
...
}
private abstract File getFile() throws Exception;
}
public class HttpFileProcessor extends FileProcessor {
private abstract File getFile() throws Exception {
// get file via HTTP and return it
...
}
}
public class FtpFileProcessor extends FileProcessor {
private abstract File getFile() throws Exception {
// get file via FTP and return it
...
}
}
An interface is really nice when all you need to do it define what methods are available but you want to leave the entire implementation up to the implementer. The interface in the example below only describes that a class implementing the interface knows how to process a Document object. You don’t care how the class does it but you know that you can call the process() method supplying the Document that should be processed.
public interface DocumentProcessor {
public void process(Document doc) throws Exception;
}
Another really neat thing about interfaces is that a class may implement multiple interfaces whereas it can only extend a single class. This means you can use an interface simply to signal something (for an example of this look at the java.lang.Cloneable interface in the JDK).
public class MyCar implements Car, Cloneable, Serializable {
...
}
With class/abstract class as polymorphic type, the objects that you pass to it must be from the same inheritence tree.
With interface, the objects can come from anywhere in the inheritence tree as long as the objects are from the class that implements the interface. So you get to treat the objects based on the role they play, rather than class type.
Serializable and Runnable interface available in JDK are some examples.
Another naming convention I have learnt is to prefix the Interface name with a capital I. That way, when you use the interface in a class it’s easy to identify it as an interface quickly.
Interface is always and can only be public (no surpise here though)
Sorry, in my previous comment I made a mistake. Only Interface methods are always public, but Interface can be public/default.
It’s not the language of Java that gives me the willies, but rather all the terms that are used to describe what it can do. Polymorphic, inheritance, serializable…..these are terms I have never used when describing Notes applications. To make me have to learn a new language just so I can program in Java is yet another stumbling block to getting me to use it. Of course, I know that I will then be able to speak about many other languages once I get the lingo down, but it’s still daunting.
Sean—
Your reaction is why I’m trying to keep my posts on level ground. I agree that the comments has a tendency to “slip”… 🙂 Did you read my post where I try to explain inheritance and polymorphism using a bird analogy?
Trying to explain inheritance and polymorphism.
Unfortunately, I didn’t catch the earlier article. However, I have been able to play along at home and follow what you are talking about so far.
Sometimes, I think that “programmers” like to throw around the big words just to make them feel better about themselves. I spend most of my time trying to describe Notes to business users in painfully simple terms to get them to understand what can and can’t be done. If I bring out anything more technical than the word field or folder, their eyes glaze over and they start to tune me out.
Sean—
Sean, I only used terms that are very commonly used in Java. Infact if you read “Head First Java” book, you will find them. That book is supposed to be for beginners!
I’m sorry, I never thought my comments would look scary/slip. I will surely think twice before I post comments in future. I’m really sorry sean & Mikkel.
Your comments are quite okay with me. I think what Sean is trying to say is that it can be a daunting task to get started with Java and that there is a language barrier that needs to be crossed.
@Gana – I wasn’t trying to imply that you were doing anything wrong, just that the terminology that is used when talking about Java is so different than what I use when building most Notes apps that the transition can be difficult. Maybe the fact that Java gurus can’t write a beginners book without pulling in all of that terminology is the reason why more Notes developers aren’t using Java. I can talk to you about building a Notes app with just forms and views. Maybe a Java beginner book needs to do that same sort of thing.
Once a developer has a certain level of comfort with the basics, then they can look to extend their knowledge to the really cool stuff. If you don’t give them time to grow, overload will kill their zest for learning the new language.
Sean—
Sean, I’m pretty much with you here, but … listen to Ganapathiram’s advice and get yourself a copy of Head First Java. This plain IS the book you’ve been looking for. And even if in the end you decide you don’t need that stuff (yet), it’s still real fun to read.
@harkpabst Advice? No, you got me wrong. I never give advices; I don’t consider myself wise enough to do that.
Give credit to those trying to make life easier for us by showing us shortcuts to java.
Java is a programming LANGUAGE. As for any language you need a minimum of syntax and a few words to make you understood in javaland. And this is what Mikke is doing, as in a tourist guide.
If you like the country and wish to discover more in java customs and culture, then you need to really learn the language. When you love something things get easy. Java is fascinating.
I’ve never understood the agression against technical terminology used in Java.
It has nothing to do with guru and stuff and all with providing eficient means of communications for people to talk about programming. Its not a special java terminology, but borrowed any term from c++, pascal, etc.
Heck. If we speak about english we use special terms, too (like subject, predicat, object for example).
And there aren’t so many terms used in java books.
Something different: I like this parody about Head First series:
http://blogs.relevancellc.com/articles/2006/04/12/relevance-review-4-head-rush-ajax