Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 43

Introduction to Object

Oriented Programming
Chapter 1
Content
• Overview of Object Oriented Language(OOP)
• Brief history of Java
Programming Paradigms
• PROGRAMS MUST BE DESIGNED.
• No one can just sit down at the computer and compose a program of any
complexity.
• Software engineering is a discipline that deals with the construction of
correct, working, well-written programs.
• Software engineers tend to use accepted and proven methods for
analyzing the problem to be solved and designing the solution. This is
what is called a Programming paradigm
Recap: Structured programming paradigm
• In Structured Programming paradigm, to solve a large problem, break
the problem into several pieces and work on each piece separately; to
solve each piece, treat it as a new problem which can itself be broken
down into smaller problems; eventually, you will work your way down to
problems that can be solved directly, without further decomposition.
• This approach is called top-down programming.
• What do you think would happen as the complexity of the program
increases?
• It doesn’t give adequate consideration to the data that it manipulates
• It makes it difficult to reuse work done in another program
Object Oriented Programming paradigm
• Object-­oriented programming took the best ideas of structured
programming and combined them with several new concepts.
• 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.
Cont.
• 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.
• The data is in the form of fields(attributes or properties)
• Each distinct logic sequence is known as a method.
Brief history of Java
• Java
• Java is a programming language and a platform. Java is a high
level, robust, object-oriented and secure programming language.
• Java is as a platform is the environment for developing and
managing java applets and applications.
• The java platform consists of the java language, the java packages
and the java virtual machine.
• It was initiated in 1991 by James Gosling in SunMicrosystems. The
language was initially called Oak, then Green and ultimately Java,n
• Java was released to the public in 1995.
04/09/2024 8
Cont.
• There are variations of the java platform.
• Standard Edition(Java SE): which is the mainstream version of the
language ad the associated tools.
• E.g. applications for PCs and workstation
• Enterprise Edition(Java EE): which includes extra libraries to
support large scale system development.
• E.g. Enterprise Resource Planning(ERP)
• Micro Edition(Java ME): which is associated for developing
software for portable devices such as cell phones and embedded
systems in the Internet of Things.
• E.g. sensors, gateways, TV sets, printers and more
Cont.
• 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 e.g Android applications in java
• Embedded System: e.g digital watches
• Smart Card e.g Java Card
• Robotics e.g Java speech API for synthesizing speech
• Games: e.g Minecraft3.

04/09/2024 11
• 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.

04/09/2024 12
04/09/2024 13
Features of Java
• 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.

04/09/2024 14
• 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.

04/09/2024 15
• Portable
• Being architecture-neutral and having no implementation dependent aspects
of the specification makes Java portable.
• 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.

04/09/2024 16
• 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.

04/09/2024 17
Java Virtual Machine(JVM)
• 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).

04/09/2024 18
• Java Virtual Machine, or JVM, loads, verifies and executes Java
bytecode. It is known as the interpreter or the core of Java
programming language because it executes Java programming.
• JVM is specifically responsible for converting bytecode to machine-
specific code and is necessary in both JDK(Java Development Kit) and
JRE(Java Runtime Environment).

04/09/2024 19
1. Runtime Instance Whenever you write java command on
the command prompt to run the java class, an instance
of JVM is created.

04/09/2024 20
Java bytcode
• What is Java Bytecode?
• Java bytecode is an intermediate code generated by the java compiler after
the compilation of the source code(java program).
• It acts 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.

04/09/2024 21
Steps of compiling and running java program
• Write the source code
• Compile the code with java compiler or javac
• The javac will create a .class file which is the bytecode(not executable)
• Java Virtual Machine interprets the bytecode and converts it to
machine code
• The JVM executes the bytecode
• Output is displayed

04/09/2024 22
04/09/2024 23
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).
What Exactly is OOP?
• 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 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

04/09/2024 30
Benefits of OOP Approach
• 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

04/09/2024 31
Basic Concepts of OOP
• 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.

04/09/2024 32
• Example
• Dog
• Attributes: breed, color, hungry, tired, etc.
• Behaviors: eating, sleeping, etc.
• Bank Account
• Attributes: account number, owner, balance
• Behaviors: withdraw, deposit

04/09/2024 33
What is Classes?
• 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)

04/09/2024 34
 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.

04/09/2024 35
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

number: 036
(object state)
balance: $941
• Methods can only be invoked .
Members of the Class

04/09/2024 38
Members of the class
• 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.

04/09/2024 39
• 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.

04/09/2024 40
Class Members Visibility
• 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.

04/09/2024 41
Questions?
04/09/2024 42
Thank You
04/09/2024 43

You might also like