Basic Ideas
Comparisons: Interface, Abstract, Concrete
Keywords
static versus instance variables and methods
abstract class
A class that contains one or more abstract
methods, and therefore can never be instantiated. Abstract classes are defined
so that other classes can extend them and make them concrete by implementing the
abstract methods.
abstract method
A method that has no implementation.
A type that defines the implementation of a particular kind of object. A class definition defines instance and class variables and methods, as well as specifying the interfaces the class implements and the immediate superclass of the class. If the superclass is not explicitly specified, the superclass will implicitly be Object.
A method that is invoked without reference to a particular object. Class methods affect the class as a whole, not a particular instance of the class. Also called a static method.
A data item associated with a particular
class as a whole--not with particular instances of the class. Class variables
are defined in class definitions. Also called a static field. See also
instance variable.
extends
Class X extends class Y to add functionality, either by adding fields or methods to class Y, or by overriding methods of class Y. An interface extends another interface by adding methods. Class X is said to be a subclass of class Y. A CLASS CAN EXTEND ONLY 1 ABSTRACT OR CONCRETE CLASS.
final
A Java keyword. You define an entity once and cannot change it or derive from it later. More specifically: a final class cannot be subclassed, a final method cannot be overridden and a final variable cannot change from its initialized value.
implements
A Java keyword included in the class declaration to specify any interfaces that are implemented by the current class. A CLASS CAN IMPLEMENT SEVERAL INTERFACES.
import
A Java keyword used at the beginning of a source file that can specify classes or entire packages to be referred to later without including their package names in the reference.
Instance
An object of a particular class. In programs written in the Java programming language, an instance of a class is created using the new operator followed by the class name.
Any item of data that is associated with a particular object. Each instance of a class has its own copy of the instance variables defined in the class. Also called a field. See also class variable.
interface
Keyword used to define a collection of
method definitions and constant values. It can later be implemented by classes
that define this interface with the "implements" keyword.
new
Keyword used to create an instance of a
class.
object
The principal building blocks of
object-oriented programs. Each object is a programming unit consisting of data (instance
variables) and functionality (instance methods).
See also class.
object-oriented design
A software design method that models the
characteristics of abstract or real objects using classes and objects.
overriding
Providing a different implementation of a method in a subclass of the class that originally defined the method.
protected
A Java keyword used in a method or
variable declaration. It signifies that the method or variable can only be
accessed by elements residing in its class, subclasses, or classes in the same
package.
public
A Java keyword used in a method or
variable declaration. It signifies that the method or variable can be accessed
by elements residing in other classes.
Interface Classes
You do not need interfaces in small programs. However, they are invaluable in large projects with many classes for managing the complicated interactions between the classes. Managing a large set of classes is a bit like managing a giant bureaucracy. You need formal job descriptions and formal descriptions of who reports what information to whom, and who can ask what services of whom. If you try to manage large collections of interacting classes without using interfaces, every class becomes inextricably intertwined with all the other classes and you can't change anything without breaking everything.
Abstract versus Concrete Classes
Abstract classes are "abstraction" of a class as the name suggests. As an example: The Human class can be an abstract class and in its concrete classes "man" and "woman", some of the functionalities are common. For example "human" can have a function "walks". Now "walks" is something that belongs to all humans. You would, therefore, provide a definition.
But "dances" is a function that might be different for "man" and "woman" or maybe to anyone who belongs to this class. So we over ride the "dances" functionality to provide seperate implementation to different implementors.
Taking the same analogy further, any class that is abstract provides some basic functionality for that class. But more specific implementors have to over ride their specific functionalities.
An abstract class has an advantage in that that you can attach some default behavior to it (e.g. an implementation). The downside is that any given object can only subclass 1 abstract (or concrtete) class in Java, so using an abstract class exlusively for typing is severely limiting your users