Sei sulla pagina 1di 4

Understanding the Abstract

Understanding The Abstract


This document is a compendium of compiled research on the purposes of the following:

Abstract Classes Abstract Methods The Final Method Private and Protected Variables

This research is applicable to the Java programming language. It should not be considered a definitive or accurate explanation of these pieces of the language, but an introduction to their complex use within Object-Oriented Programming. One should consider the use of an Interface class in certain instances, as opposed to the creation of an Abstract class. The Other References section of this document links to an appropriate source where the differences are explained in greater detail.

Page 1 of 4 || Created: 11/24/07 || Author: harkeyahh.com

Understanding the Abstract Abstract Classes The abstract class is extended in another class(e.g.Circle extends AbstractShape), because it is not instantiated the abstract class will implement its hierarchical parent/interface(Shape see Fig.1). It is however not essential for a parent to be present in the class, see Fig 2. With the Shape class the AbstractShape class is not necessarily, but in hierarchy the child or associate of Shape if present. Therefore AbstractShape literally contains the abstract programming details of Shape. Rect and Circle inherit duplicated code from the AbstractShape, this can be compared to sharing common DNA in biology. Though each has essentially the same makeup and are shapes they are not essentially directly related, excepting that they are both shapes. The same applies as people can have common DNA as people, but are not necessarily related with a slight variance in DNA. Rect and Circle are known as subclasses, which are classes that inherit attributes and behavior from other classes. Fig. 1

Fig. 2

Abstract Methods There may be abstract methods declared in an abstract class, but the class itself must be declared Abstract. An example of an abstract method
abstract class GraphicObject { int x, y; ... void moveTo(int newX, int newY) { ... }

Page 2 of 4 || Created: 11/24/07 || Author: harkeyahh.com

Understanding the Abstract


abstract void draw(); abstract void resize();

What is a final method? A final method is a method which cannot be overridden by a subclass. The nextMove(...) method in the following class is a final method:
class ChessAlgorithm { ... final void nextMove(ChessPiece pieceMoved, BoardLocation newLocation) { ... } ... }

What is a protected variable? Java uses encapsulation, which is the ability to make variables and methods private or public. When a variable or method is protected it can only be accessed by its ancestors or in Java by other classes within the same package. At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.( .accesscontrol.html) Access Levels Modifier public protected No modifier private Class Y Y Y Y Package Y Y Y N Subclass Y Y N N World Y N N N

Page 3 of 4 || Created: 11/24/07 || Author: harkeyahh.com

Understanding the Abstract Tips on Choosing an Access Level: If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this.

Use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to. Avoid public fields except for constants. (Many of the examples in the tutorial use public fields. This may help to illustrate some points concisely, but is not recommended for production code.) Public fields tend to link you to a particular implementation and limit your flexibility in changing your code.

References: http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html http://www.javalobby.org/java/forums/t77911.html http://mindprod.com/jgloss/protectedscope.html http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html Other References: http://www.jjdreamteam.com/JJDTsubset2.html#intabsclass http://www.jjdreamteam.com/JJDTsubset2.html#subclass http://mindprod.com/jgloss/interfacevsabstract.html (Interface vs. Abstract Classes)

Page 4 of 4 || Created: 11/24/07 || Author: harkeyahh.com

Potrebbero piacerti anche