Java Abstract Classes
2014-11-28 17:09
246 查看
感觉分析的很好 http://tutorials.jenkov.com/java/abstract-classes.html
Abstract classes in Java are classes which cannot be instantiated, meaning you cannot create new instances of an abstract class. The purpose of an abstract class is to function as a base
for subclasses. This text gets into the purpose of abstract classes in more detail towards the end of this text.
That is all there is to it. Now you cannot create instances of
If you try to compile the code above, the Java compiler will generate an error.
Here is how that looks:
An abstract method has no implementation. It just has a method signature.
If a class has an abstract method, the whole class must be declared abstract. Not all methods have to be abstract, even if the class is abstract. An abstract class can have a mixture of abstract
and non-abstract classes.
Subclasses of an abstract class must implement (override) all abstract methods of its abstract superclass. The non-abstract methods of the superclass are just inherited as they are. They
can also be overridden, if needed.
Here is an example subclass of
Notice how
its superclass
The only time a subclass of an abstract class is not forced to implement all abstract methods of its superclass, is if the subclass is also an abstract class.
steps:
The step before the action.
The action.
The step after the action.
If the steps before and after the action are always the same, the 3-step process could be implemented in an abstract superclass like this:
Notice how the
now extend
When the
of the subclass.
Of course, the
have to be abstract either. You could have just used an ordinary class. However, by making the method to implement abstract, and thus the class too, you signal clearly to the programmer, that this class should not be used as it is, but be used as a base class
for a subclass, and that the abstract method should be implemented in the subclass.
The above example did not have a default implementation for the
a default implementation for the method that subclasses are supposed to override. In that case, you cannot make the method abstract. You can still make the superclass abstract though, even if it contains no abstract methods.
Here is a more concrete example that opens a URL, processes it and closes the connection to the URL afterwards.
Notice how the
to implement this method.
Subclasses of
to the URL. This is done by the
This makes it easier to implement classes that processes data from URL's.
Here is an example subclass:
Notice how the subclass only implements the
Here is an example of how to use the
The
This method in turn calls the
Method design pattern provides a partial implementation of some process, which subclasses can complete when extending the Template Method base class.
Abstract classes in Java are classes which cannot be instantiated, meaning you cannot create new instances of an abstract class. The purpose of an abstract class is to function as a base
for subclasses. This text gets into the purpose of abstract classes in more detail towards the end of this text.
Declaring an Abstract Class
You declare that a class is abstract by adding theabstractkeyword to the class declaration. Here is an example:
public abstract class MyAbstractClass { }
That is all there is to it. Now you cannot create instances of
MyAbstractClass. Thus, the following Java code is no longer valid:
MyAbstractClass myClassInstance = new MyAbstractClass(); //not valid
If you try to compile the code above, the Java compiler will generate an error.
Abstract Methods
An abstract class can have abstract methods. You declare a method abstract by adding theabstractkeyword in front of the method declaration.
Here is how that looks:
public abstract class MyAbstractClass { public abstract void abstractMethod(); }
An abstract method has no implementation. It just has a method signature.
If a class has an abstract method, the whole class must be declared abstract. Not all methods have to be abstract, even if the class is abstract. An abstract class can have a mixture of abstract
and non-abstract classes.
Subclasses of an abstract class must implement (override) all abstract methods of its abstract superclass. The non-abstract methods of the superclass are just inherited as they are. They
can also be overridden, if needed.
Here is an example subclass of
MyAbstractClass:
public class MySubClass extends MyAbstractClass { public void abstractMethod() { System.out.println("My method implementation"); } }
Notice how
MySubClasshas to implement the abstract method
abstractMethod()from
its superclass
MyAbstractClass.
The only time a subclass of an abstract class is not forced to implement all abstract methods of its superclass, is if the subclass is also an abstract class.
The Purpose of Abstract Classes
The purpose of abstract classes is to function as base classes which can be extended by subclasses to create a full implementation. For instance, imagine that a certain process requires 3steps:
The step before the action.
The action.
The step after the action.
If the steps before and after the action are always the same, the 3-step process could be implemented in an abstract superclass like this:
public abstract MyAbstractProcess { public void process() { stepBefore(); action(); stepAfter(); } public void stepBefore() { //implementation directly in abstract superclass } public abstract void action(); // implemented by subclasses public void stepAfter() { //implementation directly in abstract superclass } }
Notice how the
action()method is abstract. Subclasses of
MyAbstractProcesscan
now extend
MyAbstractProcessand just override the
action()method.
When the
process()method of the subclass is called, the full process is executed, including the
action()method
of the subclass.
Of course, the
MyAbstractProcessdid not have to be an abstract class to function as a base class. Nor did the
action()method
have to be abstract either. You could have just used an ordinary class. However, by making the method to implement abstract, and thus the class too, you signal clearly to the programmer, that this class should not be used as it is, but be used as a base class
for a subclass, and that the abstract method should be implemented in the subclass.
The above example did not have a default implementation for the
action()method. In some cases your superclass might actually have
a default implementation for the method that subclasses are supposed to override. In that case, you cannot make the method abstract. You can still make the superclass abstract though, even if it contains no abstract methods.
Here is a more concrete example that opens a URL, processes it and closes the connection to the URL afterwards.
public abstract class URLProcessorBase { public void process(URL url) throws IOException { URLConnection urlConnection = url.openConnection(); InputStream input = urlConnection.getInputStream(); try{ processURLData(input); } finally { input.close(); } } protected abstract void processURLData(InputStream input) throws IOException; }
Notice how the
processURLData()method is abstract. Subclasses of
URLProcessorBasehas
to implement this method.
Subclasses of
URLProcessorBasecan process data downloaded from URL's without worrying about opening and closing the network connection
to the URL. This is done by the
URLProcessorBase. Subclasses only need to worry about processing the data from the
InputStreampassed to the
processURLData()metod.
This makes it easier to implement classes that processes data from URL's.
Here is an example subclass:
public class URLProcessorImpl extends URLProcessorBase { @Override protected void processURLData(InputStream input) throws IOException { int data = input.read(); while(data != -1){ System.out.println((char) data); data = input.read(); } } }
Notice how the subclass only implements the
processURLData()method, and nothing more. The rest of the code is inherited from the
URLProcessorBasesuperclass.
Here is an example of how to use the
URLProcessorImplclass:
URLProcessorImpl urlProcessor = new URLProcessorImpl(); urlProcessor.process(new URL("http://jenkov.com"));
The
process()method is called, which is implemented in the
URLProcessorBasesuperclass.
This method in turn calls the
processURLData()in the
URLProcessorImplclass.
Abstract Classes and the Template Method Design Pattern
The example I showed you above with theURLProcessorBaseclass is actually an example of the Template Method design pattern. The Template
Method design pattern provides a partial implementation of some process, which subclasses can complete when extending the Template Method base class.
相关文章推荐
- Effective Java 18 Prefer interfaces to abstract classes
- Why do many Collection classes in Java extend the abstract class and implement the interface as well
- [Java] Abstract classes vs. interfaces
- Effective Java 18:接口优于抽象类 Prefer interfaces to abstract classes
- Java-Abstract classes and methods
- Java学习笔记(13)Abstract Classes and Interfaces
- [java] difference between Abstract Classes of Interface
- Java中的abstract,static,final修饰符
- Step by Step - Turning Ruby Files into Java Classes(中英文对照版)
- When I used MyEclipse Persistence Tools,abstract class generated by it all implements java.io.Serializable~~So Why?
- java abstract vs interface
- Loading Java Classes
- java.lang.NoClassDefFoundError: weblogic/security/subject/AbstractSubject
- java与模式 抽象工厂(Abstract Factory)
- java.net.MalformedURLException: no protocol: 5.0/webapps/ra/WEB-INF/classes/]异常处理
- Abstract Classes Vs Interfaces
- 关于Java中的abstract与interface的讨论
- Java中的abstract,static,final修饰符
- Java 学习笔记 (3) -抽象类 abstract 接口 interface 内部类 inner class 继承 extends 实现 implements 包 package
- java class classes文件路径