1.unit 1 Java
1.unit 1 Java
Object-Oriented Thinking
&
Inheritance
The mechanism that was used to solve this problem was to find an appropriate agent
(namely, Fred) and to pass to this agent a message containing a request.
It is the responsibility of Fred to satisfy the request.
There is some method some algorithm or set of operations used by Fred to do this.
Chris does not need to know the particular method that Fred will use to satisfy the request;
indeed, often the person making a request does not want to know the details.
This information is usually hidden from inspection.
An investigation, however, might uncover the fact that Fred delivers a slightly different
message to another florist in the city where Robin lives.
That florist, in turn, perhaps has a subordinate who makes the flower arrangement.
The florist then passes the flowers, along with yet another message, to a delivery person,
and so on.
Earlier, the florist in Robin's city had obtained her flowers from a flower wholesaler who, in
turn, had interactions with the flower growers, each of whom had to manage a team of
gardeners.
The message encodes the request for an action and is accompanied by any additional
information (arguments) needed to carry out the request.
If the receiver accepts the message, it accepts the responsibility to carry out the indicated
action.
In response to a message, the receiver will perform some method to satisfy the request.
Divyavani Asst.Prof CSE 6
Messages and Methods
The important principle of information hiding in regard to message passing is that the
client sending the request need not know the actual means by which the request will be
honored.
There is another principle, that is implicit in message passing.
If there is a task to perform, the first thought of the client is to find somebody else he or she
can ask to do the work.
Frequently, a difficult hurdle to overcome is the idea in the programmer's mind that he or
she must write everything and not use the services of others.
An important part of object-oriented programming is the development of reusable
components, and an important first step in the use of reusable components is a
willingness to trust software written by others.
This difference between viewing software in traditional, structured terms and viewing it from an object-
oriented perspective can be summarized by a twist on a well-known quote:
Ask not what you can do to your data structures, but rather ask what your data structures can do
for you.
Divyavani Asst.Prof CSE 8
Classes and Instances
Although Chris has only dealt with Fred a few times, Chris has a rough idea of the
transaction that will occur inside Fred’s flower shop.
Chris can make certain assumptions based on previous experience with other florists,
and hence Chris can expect that Fred, being an instance of this category, will fit the
general pattern.
The term Florist is used to represent the category (or class) of all florists.
3rd principle of object-oriented programming:
All objects are instances of a class.
Information that Chris knows about Fred because he is a Mammal is applicable to Fido as
well.
Information about all members of Material Object is equally applicable to Fred and to his
flowers.
The idea of inheritance:
Classes can be organized into a hierarchical inheritance structure.
A child class will inherit attributes from a parent class higher in the tree.
An abstract parent class is a class for which there are no direct instances; it is used only to
create subclasses.
Divyavani Asst.Prof CSE 11
The categories surrounding Fred
Divyavani Asst.Prof CSE 12
A class hierarchyDivyavani
for various
Asst.Prof CSE
material objects 13
Method Binding and Overriding
Phyl the platypus presents a problem for our simple organizing structure. Chris knows that mammals
give birth to live children, and Phyl is certainly a Mammal, yet Phyl lays eggs.
This is done by decreasing the information contained in a subclass can override information inherited
from a parent class.
Implementations of this approach takes the form of a method in a subclass having the same name as a
method in the parent class, combined with a rule for how the search for a method to match a specific
message is conducted:
The search for a method to invoke in response to a given message begins with the class of the
receiver. If no appropriate method is found, the search is conducted in the parent class of this class.
The search continues up the parent class chain until either a method is found, or the parent class chain
is exhausted. In the former case the method is executed; in the latter case, an error message is issued.
If methods with the same name can be found higher in the class hierarchy, the method executed is said
to override the inherited behavior.
Even if a compiler cannot determine which method will be invoked at run time, in many object-oriented
languages, such as Java, it can determine whether there will be an appropriate method and issue an
error message as a compile-time error diagnostic rather than as a run-time message.
The fact that both Elizabeth and Fred will react to Chris's messages, but use different methods to
respond, is one form of polymorphism. As explained, that Chris does not, and need not, know exactly
what method Fred will use to honor the requestDivyavani
is anAsst.Prof
example CSE
of information hiding. 14
Summary of Object-Oriented Concepts
1. Everything is an object.
2. Computation is performed by objects communicating with each other, requesting that
other objects perform actions. Objects communicate by sending and receiving
messages. A message is a request for action bundled with whatever arguments may be
necessary to complete the task.
3. Each object has its own memory, which consists of other objects.
4. Every object is an instance of a class. A class simply represents a grouping of
similar objects, such as integers or lists.
5. The class is the repository for behavior associated with an object. That is, all objects
that are instances of the same class can perform the same actions.
6. Classes are organized into a singly rooted tree structure, called the inheritance
hierarchy. Memory and behavior associated with instances of a class are automatically
available to any class associated with a descendant in this tree structure.
Inheritance:
Polymorphism:
Polymorphism:
The data defined by the class are referred to as
member variables or instance variables.
The code that operates on that data is referred to as
member methods or just methods.
Polymorphism:
Inheritance:
Java Bytecode
Java Virtual
Machine
Any
Computer
Java compiler
Java bytecode
Hardware and
Programmer
Operating System
In Java, the name of the source file must be the same as the name of the class with a
mandatory file extension of ".java".
Again, the braces {......} encloses the body of the method, which contains
programming statements. Divyavani Asst.Prof CSE 55
First Java Program
Method in Java
• Single-line //
• Multi-line /* */
• Documentation comment /** */
This type of comment is used to produce an HTML file that documents your program.
enum