Unit - I
Unit - I
Biswajit Senapati,
Faculty,
Department of CSE,
NIT AP,
Tadepalligudem, AP.
Program paradigms
A WAY OF VIEWING THE WORLD
• A way of viewing the world is an idea to illustrate the object-oriented programming concept with an example of a real-world
situation.
• Let us consider a situation, I am at my office and I wish to get food to my family members who are at my home from a hotel.
Because of the distance from my office to home, there is no possibility of getting food from a hotel myself. So, how do we
solve the issue?
• To solve the problem, let me call zomato (an agent in food delevery community), tell them the variety and quantity of food
and the hotel name from which I wish to delever the food to my family members. Look at the following image.
Agents and Communities
• In our example, the online food delivery system is a community in which the agents are
zomato and set of hotels. Each hotel provides a variety of services that can be used by other
members like zomato, myself, and my family in the community.
Messages and Methods
• To solve my problem, I started with a request to the agent zomato, which led to still more
requestes among the members of the community until my request has done. Here, the
members of a community interact with one another by making requests until the problem has
satisfied.
In object-oriented programming, every action is initiated by passing a message to an
agent (object), which is responsible for the action. The receiver is the object to whom the
message was sent. In response to the message, the receiver performs some method to
carry out the request. Every message may include any additional information as
arguments.
• In our example, I send a request to zomato with a message that contains food items, the
quantity of food, and the hotel details. The receiver uses a method to food get delivered to my
home.
Responsibilities
• In object-oriented programming, behaviors of an object described in terms of responsibilities.
• In our example, my request for action indicates only the desired outcome (food delivered to
my family). The agent (zomato) free to use any technique that solves my problem. By
discussing a problem in terms of responsibilities increases the level of abstraction. This
enables more independence between the objects in solving complex problems.
Classes and Instances
• In object-oriented programming, all objects are instances of a class. The
method invoked by an object in response to a message is decided by the
class. All the objects of a class use the same method in response to a
similar message.
• In our example, the zomato a class and all the hotels are sub-classes of it.
For every request (message), the class creates an instance of it and uses a
suitable method to solve the problem.
Classes Hierarchies
• A graphical representation is often used to illustrate the relationships
among the classes (objects) of a community. This graphical
representation shows classes listed in a hierarchical tree-like structure. In
this more abstract class listed near the top of the tree, and more specific
classes in the middle of the tree, and the individuals listed near the
bottom.
In object-oriented programming, classes can be organized into a
hierarchical inheritance structure. A child class inherits properties
from the parent class that higher in the tree.
0
Method Binding, Overriding, and Exception
• In the class hierarchy, both parent and child classes may have the same method which
implemented individually. Here, the implementation of the parent is overridden by the child.
Or a class may provide multiple definitions to a single method to work with different
arguments (overloading).
• The search for the method to invoke in response to a request (message) begins with the class
of this receiver. If no suitable method is found, the search is performed in the parent class of it.
The search continues up the parent class chain until either a suitable method is found or the
parent class chain is exhausted. If a suitable method is found, the method is executed.
Otherwise, an error message is issued.
SUMMARY OF OBJECT-ORIENTED
CONCEPTS
Encapsulation
Encapsulation is the process of wrapping up data (properties) and methods
(behavior) of into a single units and unit here is class.
• Encapsulation enables data hiding, hiding irrelevant information from the users of
a class and exposing only the relevant details required by the user. We can expose
our operations hiding the details of what is needed to perform that operation.
Abstraction
Abstraction is the act of representing the essential feature without knowing the
background details.
• Abstraction captures only those details about an object that are relevant to the
current perspective, so that the programmer can focus on a few concepts at a time.
• Java provides interfaces and abstract classes for describing abstract types.
– An abstract class is a class that cannot be instantiated, but has all the properties of a class
including constructors. Abstract classes can have state and can be used to provide a
skeletal implementation.
Polymorphism
Polymorphism is the ability of an object to take on many forms
• The method Overriding occurs between super class and subclass. Overloading occurs between the
methods in the same class.
• Overriding methods have the same signature i.e. same name and method arguments. Overloaded
method names are the same but the parameters are different.
• With Overloading, the method to call is determined at the compile-time. With overriding, the method
call is determined at the runtime based on the object type.
• If overriding breaks, it can cause serious issues in our program because the effect will be visible at
runtime. Whereas if overloading breaks, the compile-time error will come and it’s easy to fix.
Inheritance
Inheritance describes the parent child relationship between two class
• A class can get some of its characteristics from a parent class and then add more unique features
of its own. For example, consider a Vehicle parent class and a child class Car. Vehicle class
will have properties and functionalities common for all vehicles. Car will inherit those common
properties from the Vehicle class and then add properties which are specific to a car.
• In the above example, Vehicle parent class is known as base class or super class. Car is known
as derived class, Child class or subclass.
• Java supports multiple inheritance (multiple parents, single child) only through interfaces. This
is done to avoid some confusions and errors such as diamond problem of inheritance.
Class
• It is a logical entity.
• A class can also be defined as a blueprint from which you can
create an individual object.
• Class does not consume any space.
Example : Animal is a class
Object
An entity that have data (state) and method (behavior) is
known as known as an object.
• Object can be defined as instance of a class
• Object can be physical or logical
Example : lion, deer, fox, bird, elephant and giraffe.
JAVA
.Overview of Java:
• James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991.
The small team of sun engineers called Green Team.
• Initially it was designed for small, embedded systems in electronic appliances like set-top boxes.
• Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
• After that, it was called Oak and was developed as a part of the Green project.
• Why Oak?
Oak is a symbol of strength and chosen as a national tree of many countries like the U.S.A., France,
Germany, Romania, etc.
Why Java is named "Java"?
• They wanted something that reflected the essence of the technology: revolutionary, dynamic, lively, cool, unique, and easy to
spell, and fun to say.
Java is an island in Indonesia where the first coffee was produced (called Java coffee). It is a kind of espresso bean. Java name was
chosen by James Gosling while having a cup of coffee nearby his office.
• Initially developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle Corporation) and released in
1995.
• In 1995, Time magazine called Java one of the Ten Best Products of 1995.
• JDK (Java Development Kit) 1.0 was released on January 23, 1996.
In java, we use the if statement to test a condition and decide the execution of a block
of statements based on that condition result. The if statement checks, the given
condition then decides the execution of a block of statements. If the condition is True,
then the block of statements is executed and if it is False, then the block of statements
is ignored. The syntax and execution flow of if the statement is as follows.
IF-ELSE STATEMENT IN JAVA
In java, we use the if-else statement to test a condition and pick the execution of a block of statements out of two blocks based on that condition result. The if-else statement checks the given
condition then decides which block of statements to be executed based on the condition result. If the condition is True, then the true block of statements is executed and if it is False, then the
false block of statements is executed. The syntax and execution flow of if-else statement is as follows.
NESTED IF STATEMENT IN JAVA
IF-ELSE IF STATEMENT IN JAVA
SWITCH STATEMENT IN JAVA
Using the switch statement, one can select only one option from more number of options very easily. In the switch statement, we provide a value that is to be compared with a value
associated with each option. Whenever the given value matches the value associated with an option, the execution starts from that option. In the switch statement, every option is
defined as a case.
The switch statement has the following syntax and execution flow diagram.
WHILE STATEMENT IN JAVA
The while statement is used to execute a single statement or block of statements repeatedly as long as the
given condition is TRUE. The while statement is also known as Entry control looping statement. The
syntax and execution flow of while statement is as follows.
DO-WHILE STATEMENT IN JAVA
The do-while statement is used to execute a single statement or block of statements repeatedly as
long as given the condition is TRUE. The do-while statement is also known as the Exit control
looping statement. The do-while statement has the following syntax.
FOR STATEMENT IN JAVA
The for statement is used to execute a single statement or a block of statements repeatedly as long as the given condition
is TRUE. The for statement has the following syntax and execution flow diagram.
FOR-EACH STATEMENT IN JAVA
The Java for-each statement was introduced since Java 5.0 version. It provides an approach to traverse through an array or collection in Java. The for-each
statement also known as enhanced for statement. The for-each statement executes the block of statements for each element of the given array or collection.
Java Jump Statements
Polymorphism
Abstraction
Inheritance
Single Inheritance
Multi-level Inheritance
Hierarchical Inheritance
Interface
Interface
Implementing an Interface
Nested Interfaces
Extending an Interface
Packages
Access protection in java packages
Importing Packages