Classes/Objects  «Prev  Next»

Lesson 2Object-oriented programming and Java
ObjectiveReview the concepts and terms of object-oriented programming.

Object-oriented programming and Java

Since Java supports object-oriented programming, knowledge of object-oriented programming concepts is a requirement for a certified Java programmer. The object class in Java has the following characteristics, namely
  1. state (attributes) and
  2. behavior(methods).

All other classes are subclasses of Object. That is, Object is a superclass of all other classes. This means that a reference variable of type Object can refer to an object of any other class. If you exclude primitives, everything in Java is an object. Not just an object, but an Object with a capital O. Every 1) exception, 2) event, every array extends from java.lang.Objects.

Ojbects Methods

Method Description
protected Object clone() creates a copy of the object
public boolean equals (Object o) returns whether two objects have the same state
protected void finalize() called during garbage collection
public Class<?> getClass() info about the object's type
public int hashCode() A code suitable for putting this object into a hash collection
public String toString() Text representation of the object
public void notify()
public void notifyAll()
public void wait()
public void wait(...)
methods related to concurrency and locking

Objective: Reuse

The overall goal of object-oriented programming is to make programming simpler and more efficient by encouraging you to reuse the classes that you and others develop.

Classification and inheritance

  1. Classification also allows you to develop hierarchies of classes, where subclasses inherit the variables and methods of their parent classes. Classification is the organization of information into hierarchical categories.
  2. There are two types of inheritance : single inheritance and multiple inheritance.
    Inheritance the passing on of design information from a superclass to a subclass.
  3. Single inheritance occurs when a class is able to inherit variables and methods from a single-parent class.
    Single inheritance: The inheritance of class members from a single parent.
  4. Multiple inheritance: occurs when a class is able to inherit from multiple-parent classes. Since Java classes can only have one parent, Java only supports single inheritance.
    Multiple inheritance: The inheritance of class members from more than one parent.


Single inheritance versus multiple inheritance

Encapsulation

Encapsulation is a quality that exists in classes with well-defined and controlled interfaces.
When a class is fully encapsulated, it is possible to change the implementation of the class without impacting other objects that use the class. A fully encapsulated class declares all of its variables as private and provides methods for getting and setting the properties represented by field variables.

Encapsulation separates design from implementation

Method implementation may be changed with minimal changes to the class's design. All access to an object takes place through its methods.

Polymorphism

Polymorphism means "many forms."
It allows methods of the same name to have many forms. For example, the println() method of the PrintStream class has several different forms that support printing of objects of different types.
Click on the following link to view an example of polymorphsim in Java.
Java Polymorphism Example

Dynamic Binding

Dynamic binding [1] is a program compilation and execution feature that allows programs to delay, until runtime, decisions about the type of objects that they access. Dynamic binding is also referred to as late binding.

[1]Dynamic binding: A program compilation and execution feature that allows programs to delay until Runtime decisions about the type of objects that they access.