0% found this document useful (0 votes)
7 views

1.unit 1 Java

The document discusses object-oriented concepts like classes, objects, inheritance, and polymorphism. It uses the example of Chris asking a florist Fred to send flowers to a friend. Fred is an instance of the Florist class. The Florist class inherits from more general classes like Shopkeeper and Mammal. This allows Chris to make assumptions about Fred based on knowledge of those parent classes. However, inheritance can be overridden, as seen in the example of a platypus laying eggs despite being a mammal. The document explains how method binding and overriding works in object-oriented programming to allow different behaviors for subclasses.

Uploaded by

Adiba Fatima
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

1.unit 1 Java

The document discusses object-oriented concepts like classes, objects, inheritance, and polymorphism. It uses the example of Chris asking a florist Fred to send flowers to a friend. Fred is an instance of the Florist class. The Florist class inherits from more general classes like Shopkeeper and Mammal. This allows Chris to make assumptions about Fred based on knowledge of those parent classes. However, inheritance can be overridden, as seen in the example of a platypus laying eggs despite being a mammal. The document explains how method binding and overriding works in object-oriented programming to allow different behaviors for subclasses.

Uploaded by

Adiba Fatima
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 69

Unit-I

Object-Oriented Thinking
&
Inheritance

Divyavani Asst.Prof CSE 1


A Way of Viewing The World

• Suppose an individual named Chris wishes to send flowers to a friend named


Robin, who lives in another city.
• Because of the distance, Chris cannot simply pick the flowers and take
them to Robin in person.
• Nevertheless, it is a task that is easily solved.
• Chris simply walks to a nearby flower shop, run by a florist named Fred.
• Chris will tell Fred the kinds of flowers to send to Robin, and the address to
which they should be delivered.
• Chris can then be assured that the flowers will be delivered expediently and
automatically.

Divyavani Asst.Prof CSE 2


Agents and Communities

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.

Divyavani Asst.Prof CSE 3


A Way of Viewing The World

Agents and Communities

The community of agents helping delivery flowers

Divyavani Asst.Prof CSE 4


1st Principle of object-oriented problem solving

An object-oriented program is structured as a community of interacting agents,


called objects.

Each object has a role to play.

Each object provides a service, or performs an action, that is used by other


members of the community.

Divyavani Asst.Prof CSE 5


Messages and Methods

2nd Principle of object-oriented problem solving

Action is initiated in object-oriented programming by the transmission of a message to an


agent (an object) responsible for the action.

The message encodes the request for an action and is accompanied by any additional
information (arguments) needed to carry out the request.

The receiver is the object to whom the message is sent.

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.

Divyavani Asst.Prof CSE 7


Responsibilities
A fundamental concept in object-oriented programming is to describe behavior in terms of
responsibilities.
Chris's request for action indicates only the desired outcome ( flowers sent to Robin).
Fred is free to pursue any technique that achieves the desired objective, and in doing so will not be
hampered by interference from Chris.
By discussing a problem in terms of responsibilities it increases the level of abstraction.
This permits greater independence between objects, a critical factor in solving complex problems.
The entire collection of responsibilities associated with an object is often described by the term
protocol.
A traditional program often operates by acting on data structures, for example changing fields in an
array or record.
In contrast, an object-oriented program requests data structures (that is, objects) to perform a service.

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.

The method invoked by an object in response to a message is determined by the class of


the receiver.
All objects of a given class use the same method in response to similar messages.

Divyavani Asst.Prof CSE 9


Class Hierarchies-Inheritance
Chris has more information about Fred - not necessarily because Fred is a Florist but
because he is a shopkeeper.
Chris knows, for example, that a transfer of money will be part of the transaction, and that
in return for payment Fred will offer a receipt. These actions are true of grocers, stationers,
and other shopkeepers.
Since the category Florist is a more specialized form of the category Shopkeeper, any
knowledge Chris has of Shopkeepers is also true of Florists and hence of Fred.
Furthermore, a Shopkeeper is also a Human; so, Chris knows, for example, that Fred is
probably bipedal. A Human is a Mammal, and a Mammal is an Animal, and an Animal is a
Material Object. Thus, quite a lot of knowledge that Chris has that is applicable to Fred is
not directly associated with him, or even with the category Florist.
The principle that knowledge of a more general category is also applicable to a more
specific category is called inheritance.
The class Florist will inherit attributes of the class Shopkeeper.

Divyavani Asst.Prof CSE 10


Class Hierarchies-Inheritance
This technique shows classes listed in a hierarchical tree-like structure, with more
abstract classes listed near the top of the tree, and more specific classes, and finally
individuals, are listed near the bottom.
This same hierarchy also includes Elizabeth, Chris's dog Fido, Phyl the platypus who lives
at the zoo, and the flowers the Chris is sending to Robin.

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.

Divyavani Asst.Prof CSE 15


OOP Principles
Encapsulation:

Inheritance:

Polymorphism:

Divyavani Asst.Prof CSE 16


OOP Principles
Encapsulation: Encapsulation is the mechanism that binds
together code and the data it manipulates.
In Java, the basis of encapsulation is the class.
A class defines the structure and behavior that will
Inheritance: be shared by a set of objects.

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.

Divyavani Asst.Prof CSE 17


OOP Principles
Encapsulation:

Inheritance: Inheritance is the process by which one object acquires


the properties of another object.

Polymorphism:

Divyavani Asst.Prof CSE 18


OOP Principles
Encapsulation:

Inheritance:

Polymorphism: Polymorphism is a feature that allows one interface


to be used for a general class of actions.
The specific action is determined by the exact
nature of the situation.

Divyavani Asst.Prof CSE 19


Divyavani Asst.Prof CSE 20
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

Divyavani Asst.Prof CSE 21


Characteristics of Java
• Java Is Simple Because Java inherits the C/C+
• Java Is Object-Oriented + syntax and many of the
• Java Is Distributed object-oriented features of C+
• +, learning Java will be easier.
Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

Divyavani Asst.Prof CSE 22


Characteristics of Java
• Java Is Simple Java is inherently object-oriented.
• Java Is Object-Oriented Although many object-oriented languages
began strictly as procedural languages,
• Java Is Distributed Java was designed from the start to be
• Java Is Interpreted object-oriented. Object-oriented
programming (OOP) is a popular
• Java Is Robust programming approach that is replacing
• Java Is Secure traditional procedural programming
techniques.
• Java Is Architecture-Neutral
• Java Is Portable One of the central issues in software
development is how to reuse code. Object-
• Java's Performance oriented programming provides great
• Java Is Multithreaded flexibility, modularity, clarity, and
reusability through encapsulation,
• Java Is Dynamic inheritance, and polymorphism.

Divyavani Asst.Prof CSE 23


Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented Distributed computing involves
several computers working
• Java Is Distributed together on a network. Java is
• Java Is Interpreted designed to make distributed
computing easy.
• Java Is Robust Java also supports Remote Method
• Java Is Secure Invocation(RMI)
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

Divyavani Asst.Prof CSE 24


Characteristics of Java
• Java Is Simple Java enables the creation of cross-
• Java Is Object-Oriented platform programs by compiling into
an intermediate representation called
• Java Is Distributed Java bytecode. This code can be
• Java Is Interpreted executed on any system that
implements the Java Virtual Machine
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

Divyavani Asst.Prof CSE 25


Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
Java compilers can detect many problems
• Java Is Distributed that would first show up at execution time
• Java Is Interpreted in other languages.
• Java Is Robust Java has eliminated certain types of error-
• Java Is Secure prone programming constructs found in
other languages.
• Java Is Architecture-Neutral
• Java Is Portable Java has a runtime exception-handling
feature to provide programming support
• Java's Performance for robustness.
• Java Is Multithreaded
• Java Is Dynamic

Divyavani Asst.Prof CSE 26


Characteristics of Java
• Java Is Simple By using a Java-compatible web
browser, we can safely download Java
• Java Is Object-Oriented applets without fear of viral infection
• Java Is Distributed or malicious intent.
• Java Is Interpreted Java achieves this protection by
confining a Java program to the Java
• Java Is Robust execution environment and not
• Java Is Secure allowing it access to other parts of the
computer.
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

Divyavani Asst.Prof CSE 27


Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
Write once, run anywhere
• Java Is Interpreted With a Java Virtual Machine (JVM), you
• Java Is Robust can write one program that will run on
any platform.
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

Divyavani Asst.Prof CSE 28


Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
Because Java is architecture neutral, Java
• Java Is Interpreted programs are portable.
• Java Is Robust They can be run on any platform
without being recompiled.
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

Divyavani Asst.Prof CSE 29


Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
The Just-in-time compilers allow the
• Java Is Interpreted platform-independent Java programs to be
• Java Is Robust executed with nearly the same run-time
performance as conventional compiled
• Java Is Secure programs
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

Divyavani Asst.Prof CSE 30


Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
Multithread programming is smoothly
• Java Is Distributed integrated in Java, which allows you to write
• Java Is Interpreted programs that do many things
simultaneously. Whereas in other languages
• Java Is Robust you have to call procedures specific to the
• Java Is Secure operating system to enable multithreading.
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

Divyavani Asst.Prof CSE 31


Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed Java programs carry with them substantial
amounts of run-time type information that is
• Java Is Interpreted used to verify and resolve accesses to objects
• Java Is Robust at run time. This makes it possible to
dynamically link code in a safe and expedient
• Java Is Secure manner.
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

Divyavani Asst.Prof CSE 32


First Java Program

Divyavani Asst.Prof CSE 33


JVM(Java Virtual Machine)

Divyavani Asst.Prof CSE 34


Divyavani Asst.Prof CSE 35
Compiling Java Source Code
•Java was designed to run object programs on any platform.
•With Java, you write the program once, and compile the source program
into a special type of object code, known as bytecode.
•The bytecode can then run on any computer with a Java Virtual Machine.
•Java Virtual Machine is a software that interprets Java bytecode.

Java Bytecode

Java Virtual
Machine

Any
Computer

Divyavani Asst.Prof CSE 36


Java Translation and Execution
Java source code

Java compiler

Java bytecode

Java interpreter Java interpreter Java interpreter Java interpreter


for Windows for Mac for Razr v2 for Linux

Divyavani Asst.Prof CSE 37


37
Java is Compiled and Interpreted

Hardware and
Programmer
Operating System

Source Code Byte Code

Text Editor Compiler Interpreter


.java file .class file
Notepad, javac java
editplus, appletviewer
Edit etc netscape

Divyavani Asst.Prof CSE 38


Divyavani Asst.Prof CSE 39
Divyavani Asst.Prof CSE 40
Java Installation

Divyavani Asst.Prof CSE 41


Java Installation

Divyavani Asst.Prof CSE 42


Java Installation

Divyavani Asst.Prof CSE 43


Setting Path for Java

Divyavani Asst.Prof CSE 44


Setting Path for Java

Divyavani Asst.Prof CSE 45


Setting Path for Java

Divyavani Asst.Prof CSE 46


Setting Path for Java

Divyavani Asst.Prof CSE 47


Setting Path for Java

Divyavani Asst.Prof CSE 48


Setting Path for Java

Divyavani Asst.Prof CSE 49


Check in Command Prompt

Divyavani Asst.Prof CSE 50


Check in Command Prompt

Divyavani Asst.Prof CSE 51


Check in Command Prompt

Divyavani Asst.Prof CSE 52


First Java Program

Divyavani Asst.Prof CSE 53


First Java Program

public class Hello {


......
}

The basic unit of a Java program is a class.

A class called "Hello" is defined via the keyword "class".

The braces {......} encloses the body of the class.

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".

Hence, this file MUST be saved as "Hello.java" - case-sensitive.

Divyavani Asst.Prof CSE 54


First Java Program

main() method, is the entry point for program execution.

Again, the braces {......} encloses the body of the method, which contains
programming statements. Divyavani Asst.Prof CSE 55
First Java Program

Method in Java

Divyavani Asst.Prof CSE 56


Divyavani Asst.Prof CSE 57
Program Execution

Divyavani Asst.Prof CSE 58


Divyavani Asst.Prof CSE 59
Divyavani Asst.Prof CSE 60
Divyavani Asst.Prof CSE 61
//Example for printf

public class Hello


{
public static void main(String[] args)
{
int a=10;
float b=123.543f;
System.out.printf("%d",a);
System.out.printf("\n%3.2f",b);
System.out.printf("\n%3.5f",b);
}
}

Divyavani Asst.Prof CSE 62


//Example

public class Hello


{
public static void main(String[] args)
{
float pi=3.14f;
System.out.println("PI Value " + pi);
System.out.print("PI Value " + pi);
}
}

Divyavani Asst.Prof CSE 63


Naming Conventions
• Variables and method names:
– Use lowercase. If the name consists of several words, concatenate all
in one, use lowercase for the first word, and capitalize the first letter
of each subsequent word in the name.
For example:- radius, area, computeArea.
• Class names:
– Capitalize the first letter of each word in the name.
Example:- FirstProgram, Sample, DemoBoxWeight.
• Constants:
– Capitalize all letters in constants and use underscores to connect words.

Example:- PI, MAX_VALUE.

Divyavani Asst.Prof CSE 64


Identifiers

• An identifier is a sequence of characters that consist of uppercase and


lowercase letters, digits, underscores ( _ ), and dollar signs ($).
• An identifier must start with a letter, an underscore (_), or a dollar sign
($).
• It cannot start with a digit.
• An identifier cannot be a reserved word.
• An identifier cannot be true, false, or null.
• An identifier can be of any length.

Divyavani Asst.Prof CSE 65


Comments
Three types of comments

• Single-line //
• Multi-line /* */
• Documentation comment /** */
This type of comment is used to produce an HTML file that documents your program.

Divyavani Asst.Prof CSE 66


Separators
( ) Method definition and invocation, defining precedence in expressions, in
control statements and type casting.
{} Initializing arrays, define blocks of code for classes, methods and local
scopes.
[ ] Declare array types, dereference array types.
; Terminates statements.
, In variable declaration, in for statement.
. To separate package names from sub packages and classes and to separate
a variable or method from a reference variable.

Divyavani Asst.Prof CSE 67


Keywords
Keywords are reserved words recognized by Java that cannot be used as
identifiers.
Java defines 50 keywords as follows:

enum

Divyavani Asst.Prof CSE 68


Data Types

Divyavani Asst.Prof CSE 69

You might also like