J2EEOnline J2EE 






Java TM Fundamentals I  «Prev  Next»
Lesson 4Messages and inheritance
ObjectiveExplain the concepts of messages and inheritance.

Object Communication Mechanism

An object by itself is not incredibly useful. In general, it takes more than one object to really do anything meaningful. Since multiple objects typically need to communicate with each other, it stands to reason that an OOP language such as Java must provide some kind of object communication mechanism.
Object: An object is a collection of data and the procedures (methods) that act on that data.

Java Messages

Java objects use messages to communicate with each other. Messages simply tell an object what to do. In Java, the act of sending a message to an object involves calling one of the object's methods .
It is a common practice to pass additional information along with a message. This information is passed along with the message as message parameters. More specifically, the message information is passed into a method as method parameters.


Message passing is another way of saying "method calling." When an object sends a message to another object, it is really just calling a method on that object. The message parameters are really just the parameters to the method. The bottom line is that messages and methods are synonymous.

Inheritance

Inheritance is a common OOP feature that involves creating a new class based on an existing class, but with additional characteristics unique to the new class. A class that inherits characteristics from another class is said to derive from that class. A derived class automatically inherits the characteristics of its base class.
Remember the TicTacToe applet class you tried out in the previous module? The TicTacToe applet class inherited features from the standard Applet class, but also provided its own unique paint() method. This is an example of inheritance put to good use.
The main premise behind inheritance is code reuse. When a class is based on another class, it inherits all of the properties of that class, including the data and methods for the class. None of the code in the base class has to be duplicated in the new class.
The class doing the inheriting is known as the child class (subclass) while the class providing the information to inherit is known as the parent class (superclass), or base class. Inherited classes form a hierarchical relationship with each other that in many ways resembles a family tree.
Class : A class is a template that defines the implementation of an object, effectively acting as a blueprint for the object. A class defines data and methods and is a unit of organization in a Java program.