3.O JAVA LANGUAGE
3.1 INTRODUCTION TO OBJECT-ORIENTED
PROGRAMMING
Learning Outcome
• Introduction to Object-oriented Programming
What is Object Oriented Programming?
• Object oriented programming (OOP)
is a programming technique that uses
“objects” to design the applications and
computer programs.
• In real life, things that you see such as
cars, trees, and mobile phones are
objects. Even, you as a student / lecturer
is an object.
• An object combines data and operations
on that data into a single unit
Reason of Using Java
i. Java is simple to learn – it is designed to have a similar syntax to
C/C++. Everyone can learn it. You don’t need university degree or
expensive courses – just continue reading this tutorials and you will be
set up in no time.
ii. Java is portable – write once, run anywhere. Your code will run on
Windows, Linux and Mac with no changes required.
iii. Java is reusable – you will find tons of libraries online. You don’t need
to be a guru about every programming topic, just find the right library
and use it.
Two Key Concept of Java
Class Object
Based on OOPs, class is Object is an entity
the blueprint/ template that has state and
of an object behavior.
>>>>>>>>>
>>>>>>>>>
Terms related to Object Oriented
Programming
Class
• Class is the blueprint/ template of an object.
• It provides the state (what the objects have) : and behavior (what the objects do) for an
object
DOG
• Example 1:
▪ Class : Student
State: first name, last name, age, address, course
Behaviour: enrol
• Example 2:
▪ Class : Dog
State: name, colour, breed
Behaviour : barking, wagging, fetching
Examples of Class: Student
In java program: state become
variable
In java program: behaviour
become method
Object
• Object is an entity that has state and behavior.
• An object represents a real-world entity. Any tangible or touchable entity
in the real-world can be described as an object.
• Following figure shows some real-world entities:
Three characteristics of object
01 STATE
Represents the data (value) of an object.
BEHAVIOUR It also reflects the properties of an
Represent the behavior of an object.
object such as deposit, withdraw
02
Example Of Object -DOG
OBJECT STATE BEHAVIORS
Dog Breed Bark
Age Sleep
Color Eat
Example Of Object- PEN
OBJECT STATE BEHAVIORS
Pen Colour - white Write
Recap
• A software object has state and State Behaviour
behavior.
Colour Drive
• ‘State’ refers to object’s Make Change gear
characteristics or attributes. Model Increase speed
Apply brakes
• ‘Behavior’ of the software object
comprises its actions.
• Following figure shows a software
object, a Car with its state and
behavior:
Example of Object and Class
Example of Object and Class
Example of Object and Class
Relation between Object and class
Relation between Object and class
Difference between class and object
Class Object
Class is a conceptual model Object is a real thing
Class describes an entity Object is the actual entity
Class consists of fields (data Object is an instance of a class
members) and functions
Four Characteristics of Java Language
Abstraction 1 Abstraction
Encapsulation 2 Abstraction is a process of hiding the implementation details
3 from the user. Only the functionality will be provided to the user.
Inheritance
Polymorphism 4 Encapsulation
Encapsulation is all about wrapping variables and methods in
one single unit with the sole purpose of data hiding from
external classes.
Inheritance
Inheritance is the OOP ability that allows Java classes to be
derived from other classes.
Polymorphism
Polymorphism in Java allows subclasses of a class to define their
own unique behaviors and yet share some of the same
functionality of the parent class.
Abstraction
• Abstraction is simplifying complex reality by modeling classes
appropriate to the problem.
• Abstraction means hiding the complexity and showing the easier form of
the system or
• Abstraction is mechanism to show only relevant data to the user.
▪ i.e: The client cares about what functionality a car offers, not about
how that functionality is implemented.
• Data abstraction can be used to provide security for the data from the
unauthorized methods.
Real Life Example of Abstraction in Java
• Abstraction shows only important things to the user and hides the
internal details,
• For example, when we ride a bike, we only know about how to ride bikes
but can not know about how it work? And also we do not know the
internal functionality of a bike.
Real Life Example of Abstraction in Java
• Another real life example of
abstraction is ATM Machine;
• All are performing operations
on the ATM machine like cash
withdrawal, money transfer,
retrieve mini-statement…etc.
but we can't know internal
details about ATM.
Real Life Example of Abstraction in Java
Encapsulation
• Encapsulation is all about wrapping variables and methods in one single
unit. Encapsulation is also known as data hiding.
Encapsulation
Encapsulation
• Encapsulation allows the programmer to group data and the methods
that operate on them together in one place, and to hide details from
the user.
Change Address AccountValue
Name Address
Benefits Of Encapsulation
o Encapsulation protects the integrity of
an object's data.
o Protects an object from unwanted
access by clients.
✓ A bank app forbids a client to
change an Account's balance.
o Allows you to change the class
implementation.
o Allows you to constrain objects' state
Abstraction vs Encapsulation
Encapsulation:
For the mathematical equation, shown in the figure
assume that complex functions are required→ But In
the end we obtain result for it.
Calculator shows the result of equation but hides the
implementation (calculating the result) involved
Abstraction:
The calculator shown in the figure has to be powered
by a battery source. How the battery module works
for the calculator is not necessary to know for the
user who uses the calculator.
Using battery module along with other modules we use calculator -> Thus
using Abstraction encapsulation is performed
Difference Between Abstraction And
Encapsulation
The basic difference between
“abstraction” and “encapsulation”
is that abstraction focuses on
“identifying the necessary
components for building a system”
whereas, encapsulation focuses on
“hiding the internal complexities of
a system”.
Inheritance
• Inheritance is a special type of relationship where a class acquires the inherent
properties of its parent class along with this it also contains its own exclusive
properties.
• Inheritance is the OOP ability that allows Java classes to be derived from other
classes.
• The parent class is called a superclass and the derivatives are called
subclasses.
• Subclasses inherit fields and methods from their superclasses.
Inheritance
Class: Phone (Parent Class)
• Make call
• Receive call
• redial
Telephone: (Child Class) Mobilephone: (Child Class) Smartphone: (Child Class)
• Make call • Make call • Make call
• Receive call • Receive call • Receive call
• redial • Redial • Redial
• Send messages • Send message
• Receive messages • Receive message
• Browse Internet
• Install and use applications
Polymorphism
• Polymorphism is a Greek word that means "many-shaped".
• fGoernmesr.ally, polymorphism refers to the ability to appear in many
• hvPaoorllydiaminbolger.ptohiscmhaningeJabveahparvoigorraamccmoirndginigs tthoewahbailtitoybojefcat rinesfetarenncceeit is
• Fccaoolmrl,emaxuatenmxicptalemti,oeynsosmuagoheda,evaeypoaiucstmcuhraoerotmpsheeoscnsoaeuglfdeo,rbmceoaamiln,myetuthcni.nicga.tIiot nc.anThbee a
• Sdiof,fetrheentg.oal is common that is communication, but their approach is
• Polymorphism enables us to "program in the general" rather than
"program in the specific”.
Real Life Example Of Polymorphism
Suppose if you are in (i). class room that time you behave like a student, (ii).
when you are in market at that time you behave like a customer, (iii). when
you at your home at that time you behave like a son or daughter, Here one
person present in different-different behaviors.
Real Life Example Of Polymorphism
To speak something; for example, a
cat speaks meow, dog barks woof,
etc.
Get the idea????
Summary
• Key concept in Java : class and object
• 4 characteristics of Java language
▪ Abstraction
▪ Encapsulation
▪ Inheritance
▪ polymorphism