Chapter-One (Introduction To OOP)
Chapter-One (Introduction To OOP)
BITS College
Undergraduate Program
Course Code: SE132
Course Title: Object Oriented
Programming
Contents
Overview of OOP Encapsulation
Java, JVM and Byte Code Inheritance
Basic Concepts of OOP Polymorphism
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
It is proportional to the faithfulness with which the structures and
interactions in the program mirror in the real world
Cont.…
Example
Design from the world of library
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
How do we organize it ?
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.
Object
Contents
1. Overview of OOP
Loading
Loads a .class file when a given class is referenced for the first time
Reads .class file and produces the binary representation of the type
Linking Initializing
Verification
Interpreter
Garbage Collector
o Reads byte code and interpret into machine code
Cleans up unused class
o Execute the machine code line by line objects, memory areas
o It is slow
JIT compiler
Basic concept
(principles) of OOP
Class
modifier type methodName1(parameter list) { Attributes: the state of an object which is known as
// body of method
} variable
modifier type methodName2(parameter list) { Methods: behavior of an object which is known as
// body of method
} function in many programming language
}
Methods
Compiled By: Asamnew Gizaw 07/29/24
22
Objects
Instances of classes that represent specific entities, with their own state
(fields) and behavior (Method).
Encapsulate data and methods, allowing reusability
They interact with each other to achieve desired functionality.
Example: public class Student {
int x=10;
public static void main (String args []) {
Student s = new Student();
System.out.println(s.x);
}
}
Compiled By: Asamnew Gizaw 07/29/24
23
Abstraction
Example: Suppose you want to create a dating application and you are asked to collect all the information about your
users.
A process of performing a single action in different ways via super and sub-class
It is implemented by method overriding or overloading
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).
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
Java
Is a programming language and a platform (compiler, execution
engine, set of libraries, etc.…)
Is a high level, robust, object-oriented and secure programming
language.
Developed by Sun Microsystems (which is now the subsidiary of
Oracle) in the year 1995.
James Gosling is known as the father of Java
Web Application: An application that runs on the server side and creates a
dynamic page.
Enterprise Application: An application that is distributed in nature, such as
banking applications.
Mobile Application: An application which is created for mobile devices.
Secure
Object Oriented o With Java's security feature it enables
Robust
Platform Independent o Eliminate error-prone situations by
o Java is compiled into platform- emphasizing mainly on compile time
independent byte code. error checking and runtime checking.
o This byte code is distributed over the o Problems will be handled by the
web and interpreted by the Virtual JVM and will not be propagated to
Machine (JVM) on whichever platform the underlying system
it is being run on. o It supports garbage collection
Portable Architecture-neutral
High Performance
o With the use of Just-In-Time compilers, Java enables high performance.
Multithreaded
o With Java's multithreaded feature it is possible to write programs that can
perform many tasks simultaneously.
o This design feature allows the developers to construct interactive
applications that can run smoothly.
Distributed
o Java is designed for the distributed environment of the internet. 07/29/24
40
Contents
1. Overview of OOP
2. Java, JVM and Byte Code
3. Basic Concepts of OOP
Objects
Classes
Members
Class member visibility
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.
Cont.…
Example
Dog
Attributes: breed, color, hungry, tired, etc.
Behaviors: eating, sleeping, etc.
Bank Account
Attributes: account number, owner, balance
Behaviors: withdraw, deposit
Class
Cont.…
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 two
deposit()
state variables(account number and
balance) and two methods (deposit withdraw()
and withdraw).
Instance #1
Instance #2
Each instance will have its own account
number number: 712
balance: $240
and balance (object state)
Instance #3
number: 036
Method can only be invoked
balance: $941
Compiled By: Asamnew Gizaw 07/29/24
48
Cont.….
Local variables – variables defined inside methods, constructors or blocks.
The variable will be declared and initialized within the method and the variable
will be destroyed when the method complete its execution
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. 07/29/24
50
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.