View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide

Object Oriented concepts

In object-oriented programming we classify objects into classes. Many objects can be created from a class. The class defines the fields (data) and methods (behavior) for the objects. An object-oriented program has many interacting objects. An example of this is GridWorld where you can have several bugs that all have the same fields and behaviors.

Inheritance means that a subclass inherits the object fields and methods from the parent class. But, the child class does not have direct access to private fields or methods. You can initialize inherited private fields using super() in the constructor. A good example of this is the BoxBug in GridWorld. It inherited many fields and behaviors from the Bug class. Inheritance should only be used when the subclass is something you can substitute for the parent class. Do not use inheritance (is-a or is-a-type-of) just to have access to a desired field or method. Use association (has-a) instead. A Java class can only inherit from one parent class.

Polymorphism means that the method actually executed depends on the run-time type of the object (the class that created the object). This lets us write general algorithms based on a common parent class, but specialize behavior based on the actual class. An example of this was the Shape class which had subclasses of Oval and Rectangle.

An abstract class can have fields and non-abstract methods. It can also inherit from another class or implement an inteface. Abstract classes often have at least one abstract method. If a class has an abstract method it must be an abstract class. Classes that inherit from abstract classes must provide the method bodies for the abstract method or else they must also be abstract. An example of this was the Shape class.

An interface is a special kind of abstract class that can only have abstract methods and constants defined in it. Interfaces are used to separate what from who. Examples of interfaces are java.util.List and java.lang.Comparable. Interfaces can inherit from other interfaces. An example interface was the Grid interface in GridWorld or the ShapeInterface.

A static field is also called a class field and it exists in the object that defines the class. Since all objects keep a reference to the class that created them all objects have access to a class field. It is useful for creating constants and things like identifiers that need to be unique for every object of the class. An example of this was the constants in Location in GridWorld.

Objects have a declared type and an actual type. In the example below the declared type is Animal and the actual type is Bear.
Animal a = new Bear(); 

The declared type is what the object is declared to be (Animal). It can actually be an object of that type or an object
of any subclass of that type (that is why you can set a to a Bear object). At compile time the declared type is used to check that the methods being called actually exist somewhere either in the declared
type or inherited from any superclass of the declared type.

The actual type is the class that actually creates the object. Objects always keep a reference to the class that created them. You can get this class using
Class c = "this".getClass();

Link to this Page