0% found this document useful (0 votes)
11 views52 pages

Introduction To OOP

The document outlines an undergraduate course on Object Oriented Programming (OOP) at BITS College, covering key concepts such as classes, objects, encapsulation, inheritance, and polymorphism. It explains the principles of OOP, the Java programming language, and the Java Virtual Machine (JVM), emphasizing the importance of modeling real-world entities in programming. Additionally, it discusses the structure of classes, members, and their visibility within the context of OOP.

Uploaded by

midasasfaw
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views52 pages

Introduction To OOP

The document outlines an undergraduate course on Object Oriented Programming (OOP) at BITS College, covering key concepts such as classes, objects, encapsulation, inheritance, and polymorphism. It explains the principles of OOP, the Java programming language, and the Java Virtual Machine (JVM), emphasizing the importance of modeling real-world entities in programming. Additionally, it discusses the structure of classes, members, and their visibility within the context of OOP.

Uploaded by

midasasfaw
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BITS College

Undergraduate Program
Course Code: SE132
Course Title: Object Oriented
Programming
Introduction to Object Oriented
Programming
Contents
1. Overview of OOP
2. Java, JVM and Byte Code
3. Basic Concepts of OOP
 Classes

 Objects

 Members

 Class member visibility


4. .
Encapsulation
5. Inheritance
6. Polymorphism
Contents
1. Overview of OOP
2. Java, JVM and Byte Code
3. Basic Concepts of OOP
 Classes

 Objects

 Members

 Class member visibility


.
Overview of OOP
 Object-oriented programming (OOP) languages had their roots in
simulation
 The object oriented philosophy
 Every program is really a simulation

 The quality of a program’s design is proportional to the faithfulness

with which the structures and interactions in the program mirror in the
real world
 According to this philosophy, a program to manage book handling in a
library is really a simulation of what the librarians would do manually.
Cont…
 The second principle suggests that the things manipulated by the program
should correspond to things in the real world
 E.g first name not FN

 Why ?
 We can talk to domain experts

 We can talk to each other

 We can understand our code easily when revising it after a while


Cont…
 Example
 Design from the world of library
 When you walk to a library what do you see?

 Books, Librarians, Customers, Shelves,…

 Card catalog in older libraries

 The library itself


Cont…
 Relationships
 Books have title, authors, ISBN, …
 Librarian and customers have names
 Customers have library cards with unique identifiers
 Customers return books
 Librarians check in returned books and also checkout books for customers
 Each book belongs to a specific branch of a library
Cont…
 How do we organize it ?
Object Oriented Programming (OOP)
 Object Oriented Programming (OOP) is a programming paradigm that
relies on the concepts of classes and 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.
Cont…
 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 by sending/ receiving
messages.
Basic principles of OOP
Basic principles of OOP
 OOP contains the following fundamental 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
of the object.
 It is also used to hide implementation details (e.g. JAVA libraries)
 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.
Cont…
 Encapsulation
 Isa complete specification of the attributes and behaviors of objects. This make
objects to be independent, completely-specified and self-sufficient.
 bundling data and methods that work on that data within one unit, like a class in
Java.
 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.
Cont…
 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.
Cont…
 Polymorphism
 Is a concept by which we can perform a single action in different ways.
 Allows us to interact with an object as its generalized category regardless of its more
specialized category.
 It is implemented by method overriding or overloading
 This being said, each child sub-class keeps its own functions/methods as they are.
 Example: If we have 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).
Cont…
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
Contents
1. Overview of OOP
2. Java, JVM and Byte Code
3. Basic Concepts of OOP
 Classes

 Objects

 Members

 Class member visibility


Java, JVM and Byte code
 Java
 Java is a programming language and a platform (compiler, execution
engine, set of libraries, etc…). 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
 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 [Link], [Link], etc.
 Enterprise Applications such as banking applications.
 Mobile
 Embedded System
 Smart Card
 Robotics
 Games, etc.
 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.
Features of Java
 Object Oriented
 InJava, everything is an Object. Java can be easily extended since it is based
on the Object model.
 Supports every OOP principles

 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 security feature it enables to develop virus-free, tamper-free
systems. Authentication techniques are based on public-key encryption.
 Verifies the byte code
 Platform Independent
 Unlike many other programming languages including C and C++, when Java is compiled, it
is not compiled into platform specific machine code, rather into platform-independent byte
code.
 Write once and run anywhere
 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.
 Problems will be handled by the JVM and will not be propagated to the underlying system
 It supports garbage collection
 Portable
 Being architecture-neutral and having no implementation dependent aspects
of the specification makes Java portable.
 Thecompiler 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.
 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.
 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.
Java Virtual Machine(JVM)
Java Virtual Machine Architecture
Class Loader Subsystem (delegation
hierarchy algorithm)
 Class Loader Subsystem
 Loads a .class file when a given class is referenced for the first time

 Loading

 Reads .class file and stores it in the method area.

 Creates instance of class [Link] that represents the type in


the heap area
 Linking
 Verification
 Ensures binary representation is structurally correct
 .class file is generated by valid compiler and formatted properly
 Preparation
 Memory is allocated for class variables
 Default values are assigned
 Resolution
 Replaces symbolic references with actual references
 Initialization
 Class (static) variable are assigned with their original values
Runtime Data Areas
 Method Area
 Created on JVM startup
 Used to store metadata of a class (fully qualified name, whether the type is class or interface,
access modifier, constant pool, field information, method information, etc…)
 Heap Area
 Created on JVM startup
 Memory for objects is allocated on heap
 Stack Area
 Created when a new thread is created
 Used by the thread to track method calls
 PC register
 Created when a new tread is created
 Holds the address of current executing instruction
 Native method stack
 Created when a native method is invoked
 It is used by methods written in languages other than java
Execution Engine
 Reads byte code assigned to run time data areas and execute it piece
by piece
 Interpreter
 Reads byte code and interpret into machine code
 Execute the machine code line by line
 It is slow
 JIT compiler
 Maintains a separate count for every method
 Compiles a hotspot code and stores it to code cash
 Profile is part of the JIT that identifies hotspot
 Garbage Collector
Contents
1. Overview of OOP
2. Java, JVM and Byte Code
3. Basic Concepts of OOP
 Objects

 Classes

 Members

 Class member visibility


What is Class?
 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)
Basic Concepts of OOP
 What is Object?
 Object: is an instance of a class 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.
 Example
 Dog
 Attributes:breed, color, hungry, tired, etc.
 Behaviors: eating, sleeping, etc.

 Bank Account
 Attributes:account number, owner, balance
 Behaviors: withdraw, deposit
 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.
Bank Example
 The "account" class describes the
class: Account
attributes and behaviors of
number:
bank accounts.
balance:
 The “account” class defines
deposit()
two state variables
withdraw()
(account number and balance) and

two methods (deposit and withdraw).


Bank Example – three objects
Instance #1
 When the program runs there will be
number: 054
many instances of the account class.
balance: $19
 Each instance will have its own account number
Instance #2
and balance (object state)
number: 712

balance: $240
 Method can only be invoked
Instance #3

number: 036

balance: $941
Members of the Class
Members of the Class
 A class can contain any of the following variable
 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 complete its
execution
 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.
Cont...
 A class can have any number 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.
Class Members Visibility
 A class member is declared with a 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.
Cont...

You might also like