You are on page 1of 44

1

INTRODUCTION
TO
OBJECT-ORIENTED PROGRAMMING
(OOP)

11/18/2021 OOP- CH-1- Introduction to OOP


Contents
1. Overview of OOP
2. Java, Byte Code and JVM
3. Basic Concepts of OOP
 Classes
 Objects
 Members
 Class member visibility

11/18/2021
Contents
1. Overview of OOP
2. Java, JVM and Byte Code
3. Basic Concepts of OOP
 Classes
 Objects
 Members
 Class member visibility

11/18/2021
Overview of OOP
 Object-oriented programming (OOP) is a
programming paradigm that uses “Objects “and
their interactions to design applications.
 OOP focuses on the objects that developers want to
manipulate rather than the logic required to
manipulate them.
 This approach to programming is well-suited for
programs that are large, complex and actively
updated or maintained.
 The first step in OOP is to collect all of the objects
a programmer wants to manipulate and identify
how they relate to each other (Data Modeling).
 Examples of an object can range from physical entities,
such as a human being who is described by properties
like name and address, down to small computer
programs, such as Widgets.
 Once an object is known, it is labeled with
a class of objects that defines the kind of data it
contains and any logic sequences that can
manipulate it.
 Each distinct logic sequence is known as a method.
 Objects can communicate with well-defined
interfaces called messages.
Basic principles of OOP
Basic principles of OOP
 OOP contains the following fundamentals principles
 Abstraction
 Allows us to consider complex ideas while ignoring
irrelevant detail that would confuse us.
 It is the process of selecting data from a larger pool to show
only the relevant details to the object. 
 This concept helps developers more easily make changes
and additions over time.
 Example
 Suppose you want to create a dating application and you are
asked to collect all the information about your users. 
Example: Abstraction
Dating application

Full name, address, phone number, favorite


food, favorite movie, hobbies, tax information,
social security number, credit score

Full name, address,


phone number,
favorite food, favorite
movie, hobbies,

The process of fetching/removing/selecting the user


information from a larger pool is referred to as Abstraction.
 Encapsulation
 The implementation and state of each object are privately
held inside a defined boundary, or class. Other objects do
not have access to this class or the authority to make
changes but are only able to call a list of public functions,
or methods.
 This characteristic of data hiding provides greater program
security and avoids unintended data corruption.
 Inheritance
 Allows us to define general characteristics and operation of
an object and allow us to create more specialized versions of
this object.
 Inheritance is the ability of one object to acquire some/all
properties of another object.
 For example, a child inherits the traits of his/her parents.
 With inheritance, reusability is a major advantage. You can
reuse the fields and methods of the existing class.
  For example, Apple is a fruit so assume that we have a class
Fruit and a subclass of it named Apple. Our Apple acquires
the properties of the Fruit class.
 Polymorphism
 Allows us to interact with an object as its generalized category
regardless of its more specialized category.
 Polymorphism gives us a way to use a class exactly like its parent
so there is no confusion with mixing types.
 This being said, each child sub-class keeps its own
functions/methods as they are.
 Example: If we had a superclass called Mammal that has a
method called mammalSound().
 The sub-classes of Mammals could be Dogs, whales, elephants, and
horses.
 Each of these would have their own iteration of a mammal sound
(dog-barks, whale-clicks).
13 11/18/2021
What Exactly is OOP?
14

 Identifying an Object?
 You can also think of other non physical things as objects:-
such as a bank account
 A bank account is not something that can be physically touched
but intellectually we can consider a bank account to be an object.
 OOP is a method of programming that involves the
creation of intellectuals objects that model a business
problem we are trying to solve.
 In creating an OO program we define the properties of a
class of objects and then create individual objects from the
class
11/18/2021
Benefits of OOP Approach
15

 Better abstraction
 Modeling information and behavior together
 Better maintainability
 More comprehensible, less fragile software
 Better usability
 Classes as encapsulated components that can be used
in other systems

11/18/2021
Contents
1. Overview of OOP
2. Java, Byte Code and JVM
3. Basic Concepts of OOP
 Classes
 Objects
 Members
 Class member visibility

11/18/2021
Java, JVM and Byte code
17

 Java
 Java is a programming language and a platform.
Java is a high level, robust, object-oriented and secure
programming language.
 Java was developed by Sun Microsystems (which is
now the subsidiary of Oracle) in the year 1995. 
 James Gosling is known as the father of Java. 

11/18/2021
18

 According to Sun, 3 billion devices run Java.


 There are many devices where Java is currently used. Some of
them are as follows:
 Desktop Applications such as acrobat reader, media player, antivirus,
etc.
 Web Applications such as irctc.co.in, javatpoint.com, etc.
 Enterprise Applications such as banking applications.
 Mobile
 Embedded System
 Smart Card
 Robotics
 Games, etc.

11/18/2021
19

 Types of Java Applications


 There are mainly 4 types of applications that can be
created using Java programming:
1. Standalone Application: Desktop applications or
window-based applications.
2. Web Application: An application that runs on the server
side and creates a dynamic page.
3. Enterprise Application: An application that is distributed
in nature, such as banking applications.
4. Mobile Application: An application which is created for
mobile devices. 
11/18/2021
20 11/18/2021
Features of Java
21

 Object Oriented
 In Java, everything is an Object. Java can be easily
extended since it is based on the Object model.
 Simple
 Java is designed to be easy to learn. If you understand the
basic concept of OOP Java, it would be easy to master.
 Secure
 With Java's secure feature it enables to develop virus-
free, tamper-free systems. Authentication techniques are
based on public-key encryption.
11/18/2021
22

 Platform Independent
 Unlike many other programming languages including C and
C++, when Java is compiled, it is not compiled into
platform specific machine, rather into platform-independent
byte code.
 This byte code is distributed over the web and interpreted by the
Virtual Machine (JVM) on whichever platform it is being run on.
 Robust
 Java makes an effort to eliminate error-prone situations by
emphasizing mainly on compile time error checking and
runtime checking.

11/18/2021
23

 Portable
 Being architecture-neutral and having no implementation
dependent aspects of the specification makes Java
portable.
 The compiler in Java is written in ANSI C with a clean
portability boundary, which is a POSIX subset.
 Architecture-neutral
 Java compiler generates an architecture-neutral object file
format, which makes the compiled code executable on
many processors, with the presence of Java runtime
system.
11/18/2021
24

 Dynamic
 Java is considered to be more dynamic than C or C++
since it is designed to adapt to an evolving environment.
 Java programs can carry an extensive amount of run-time
information that can be used to verify and resolve accesses to
objects at run-time.
 Interpreted
 Java byte code is translated on the fly to native machine
instructions and is not stored anywhere.
 The development process is more rapid and analytical since the
linking is an incremental and light-weight process.

11/18/2021
25

 High Performance
 With the use of Just-In-Time compilers, Java enables high
performance.
 Multithreaded
 With Java's multithreaded feature it is possible to write
programs that can perform many tasks simultaneously.
 This design feature allows the developers to construct interactive
applications that can run smoothly.
 Distributed
 Java is designed for the distributed environment of the
internet.
11/18/2021
Java bytcode
26

 What is Java Bytecode?


 Java bytecode is the instruction set for the Java Virtual
Machine.
 Itacts similar to an assembler which is an alias
representation of a C++ code.
 As soon as a java program is compiled, java bytecode
is generated. In more apt terms, java bytecode is the
machine code in the form of a .class file.
 With the help of java bytecode we achieve platform
independence in java.

11/18/2021
27 11/18/2021
Java Virtual Machine(JVM)
28

 JVM (Java Virtual Machine) is an abstract


machine.
 It is a specification that provides runtime environment
in which java byte-code can be executed.
 JVMs are available for many hardware and
software platforms (i.e. JVM is platform
dependent).

11/18/2021
29

 What is JVM?
1. A specification where working of Java Virtual Machine
is specified.
 But implementation provider is independent to choose the
algorithm. Its implementation has been provided by Oracle
and other companies.
2. An implementation Its implementation is known as
JRE (Java Runtime Environment).
3. Runtime Instance Whenever you write java command
on the command prompt to run the java class, an
instance of JVM is created.
11/18/2021
30

 What it does?
 The JVM performs following operation:
 Loads code
 Verifies code
 Executes code
 Provides runtime environment
 JVM provides definitions for the:
 Memory area
 Class file format
 Register set
 Garbage-collected heap
 Fatal error reporting etc

11/18/2021
31 11/18/2021
Contents
1. Overview of OOP
2. Java, JVM and Byte Code
3. Basic Concepts of OOP
 Objects
 Classes
 Members
 Class member visibility

11/18/2021
Basic Concepts of OOP
33

 What is Object?
 Object: A single software unit that combines
attributes and methods. It represents an entity in
the real world.
 Attribute: A "characteristic" of an object; like a
variable associated with a kind of object.
 Method: A "behavior" of an object; like a function
associated with a kind of object.

11/18/2021
34

 Example
 Dog
 Attributes:breed, color, hungry, tired, etc.
 Behaviors: eating, sleeping, etc.
 Bank Account
 Attributes:account number, owner, balance
 Behaviors: withdraw, deposit

11/18/2021
What is Classes?
35

 The definitions of the attributes and methods of an


object are organized into a class.
 Thus, a class is the generic definition for a set of

similar objects (i.e. Person as a generic definition


for different persons)
 A class is an abstract description of a set of

objects.
 A class can be thought of as a template used to

create a set of objects. (A blue print to create


(instantiate) an object)
11/18/2021
36

 We actually write code for a class, not object


 The objects are called instances of the class.
 Every instance of the same class will have the same set of
attributes;
 Every object has the same attributes but,
 Each instance will have its own distinct values for
those attributes.
11/18/2021
Bank Example
class: Account
 The "account" class describes
number:
the attributes and behaviors of
balance:
bank accounts.
deposit()
 The “account” class defines two
withdraw()
state variables (account number
and balance) and two methods
(deposit and withdraw).
Bank Example – three objects
Instance #1

number: 054
 When the program runs there
balance: $19
will be many instances of the
Instance #2
account class. number: 712

 Each instance will have its own balance: $240

account number and balance Instance #3

(object state) number: 036

balance: $941
 Methods can only be invoked .
Members of the Class
39

11/18/2021
Members of the class
40

 A class can contain any of the following variable types.


 Local variables − Variables defined inside methods,
constructors or blocks are called local variables.
 The variable will be declared and initialized within the method and
the variable will be destroyed when the method has completed.
 Instance variables − Instance variables are variables within a
class but outside any method.
 These variables are initialized when the class is instantiated. Instance
variables can be accessed from inside any method, constructor or
blocks of that particular class.
 Class variables − Class variables are variables declared within
a class, outside any method, with the static keyword.

11/18/2021
41

 A class can have any number of methods to access


the value of various kinds of methods. 
 A method is a program module that contains a series
of statements that carry out a task.
 To execute a method, you invoke or call it from
another method; the calling method makes a method
call, which invokes the called method. 

11/18/2021
Class Members Visibility
42

 A class member is declared with an visibility labels, that specifies


how it will be accessed outside its class.
 Each object has members (members can be variable and methods) which
can be declared to have specific access.
 Possible access privileges are:
 public : You can access it from anywhere.
 protected : You can access it from any other class in the same directory
(folder), or from any subclass.
 package (default) : You can access it from any other class in the same
directory.
 private : You cannot access it from outside the class. Surprisingly, private
variables and methods can be accessed by other objects in the same class.

11/18/2021
43

Questions?
11/18/2021
44

Thank You
11/18/2021

You might also like