0% found this document useful (0 votes)
17 views

Chapter-One (Introduction To OOP)

Uploaded by

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

Chapter-One (Introduction To OOP)

Uploaded by

Nabek Deresa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 52

1

BITS College
Undergraduate Program
Course Code: SE132
Course Title: Object Oriented
Programming

Compiled By: Asamnew Gizaw 07/29/24


2

Introduction to Object Oriented


Programming

Compiled By: Asamnew Gizaw 07/29/24


3

Contents
 Overview of OOP  Encapsulation
 Java, JVM and Byte Code  Inheritance
 Basic Concepts of OOP  Polymorphism
 Classes
 Objects
 Members
.
 Class member visibility

Compiled By: Asamnew Gizaw 07/29/24


4

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

Compiled By: Asamnew Gizaw 07/29/24


5

Cont.…

Example
 Design from the world of library

 When you walk to a library what do you see?

Structure  Books, Librarians, Customers, Shelves,…

 Card catalog in older libraries

 The library itself


Compiled By: Asamnew Gizaw 07/29/24
Cont.… 6

 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

Compiled By: Asamnew Gizaw 07/29/24


Cont.… 7

 How do we organize it ?

Compiled By: Asamnew Gizaw 07/29/24


8

Object Oriented Programming (OOP)

 Relies on the concepts of classes and objects and their interactions

 Focuses on the objects that developers want to manipulate rather than


the logic required to manipulate them.

Compiled By: Asamnew Gizaw 07/29/24


Cont.… 9

 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.

Compiled By: Asamnew Gizaw 07/29/24


10
Cont.…

 Object

 Is labeled with a class of objects that defines the kind


of data it contains and any logic sequences (method)
that can manipulate it.
 Can communicate with well-defined interfaces by
sending/receiving messages.

Compiled By: Asamnew Gizaw 07/29/24


11

Contents

1. Overview of OOP

2. Java, JVM and Byte Code

3. Basic Concepts of OOP


 Classes
 Objects
 Members
 Class member visibility
Compiled By: Asamnew Gizaw 07/29/24
12

Java virtual Machine

 Is a specification: bytecode, class metadata

 Is a implementation: GRAALVM (jdk), oracle hotspot (high


performance JVM)
 Is a runtime instance: java my_app.class

Compiled By: Asamnew Gizaw 07/29/24


13
JVM Architecture
Java API .class
file
Your application .class file Class Loader

Run time data area

Method Java PC Native


Heap
Area Stack Register Method Stack

Native Method Native Method library


Execution Engine interface (JNI) (.dll, .so etc)

Operating System 07/29/24


14

Class Loader Subsystem

Loading Linking Initialization


Bootstrap class loader
(rt.class)
Verify
Initialize static vars,
Extension class Loader blocks…
(jre/lib/ext)
Prepare

Application class Loader


(class path, -cp)
Resolve

Compiled By: Asamnew Gizaw 07/29/24


15

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

 The representation is stored in the method area

 Creates instance of class java.lang.Class that represents the type in


the heap area

Compiled By: Asamnew Gizaw 07/29/24


16

Linking Initializing
 Verification

o Ensures binary representation is structurally correct  Class (static) variable


o .class file is generated by valid compiler and are assigned with their

formatted properly original values


 Preparation

o Memory is allocated for class variables

o Default values are assigned


 Resolution

o Replaces symbolic references with actual references


Compiled By: Asamnew Gizaw 07/29/24
17

Runtime Data Areas


 Method Area  PC register

o Created on JVM startup o Created when a new tread is created

o Used to store metadata of a class o Holds the address of current instruction


 Stack Area
(modifier, field, method)
o Created when a new thread is created
 Heap Area
o Used by the thread to track method calls
o Created on JVM startup
 Native method stack
o Memory for objects is allocated
o Used by methods written in languages
on heap
other than java
Compiled By: Asamnew Gizaw 07/29/24
18
Execution Engine

 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

o Maintains a separate count for every method

o Compiles a hotspot code and stores it to code cash

o Used for recursive operation


Compiled By: Asamnew Gizaw 07/29/24
19

Basic concept
(principles) of OOP

Compiled By: Asamnew Gizaw 07/29/24


20

Class

 Blueprints or templates that define the structure and behavior of objects.


 A class declaration consists of:
 Modifiers: These can be public or default access.
 Class name: Initial letter.
 Superclass: A class can only extend (subclass) one parent.
 Interfaces: A class can implement more than one interface.
 Body: Body surrounded by braces { }.

Compiled By: Asamnew Gizaw 07/29/24


Class 21

 Example public class Student extends superClass implements I1, I2{


private float grade;
modifier keyWods class ClassName keyWords {
modifier type fieldName1; public void setGrade(float grade){
modifier type fieldName2; this.grade = grade;
Attributes }
modifier type fieldName3;

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

 Consider complex ideas while ignoring irrelevant detail that would


confuse us.
 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)

Compiled By: Asamnew Gizaw 07/29/24


Cont.…. 24

 Example: Suppose you want to create a dating application and you are asked to collect all the information about your
users.

Full name, address, phone


Full name, address, phone
number, favorite food,
number, favorite food,
favorite movie, hobbies, tax
favorite movie, hobbies,
information, social security
number, credit score

The process of fetching/removing/selecting the user


information from a larger pool is referred to as Abstraction.
Compiled By: Asamnew Gizaw 07/29/24
25
Encapsulation

 Is a complete specification of the attributes and behaviors of objects.


 Bundling data and related methods together within a class
 Allows for better organization of code, maintains data integrity, and
exposes only necessary information through a public interface,
enhancing security and modularity.

Compiled By: Asamnew Gizaw 07/29/24


26
Cont.…

Compiled By: Asamnew Gizaw 07/29/24


27
Inheritance

 Defining general characteristics and operation of an object, to create more


specialized versions of an object.
 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
Compiled By: Asamnew Gizaw 07/29/24
28
Polymorphism

 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).

Compiled By: Asamnew Gizaw 07/29/24


Summary 29

Compiled By: Asamnew Gizaw 07/29/24


30
Benefit 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

Compiled By: Asamnew Gizaw 07/29/24


31

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

Compiled By: Asamnew Gizaw 07/29/24


32
Cont.…

Java is different from other


programming language because,
it both compiled and interpreted.
A file with .class extension is
contain a byte code. Byte code is
machine language of the java
virtual machine ( java VM).

Compiled By: Asamnew Gizaw 07/29/24


33
Cont.…

 According to Sun, 3 billion devices run Java.

 There are many devices where Java is currently used. Example:

 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 Robotics

Embedded System Games, etc.


Smart Card

Compiled By: Asamnew Gizaw 07/29/24


34

Types of Java Applications

 Standalone Application: Desktop applications or window-based applications.

 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.

Compiled By: Asamnew Gizaw 07/29/24


35

Compiled By: Asamnew Gizaw 07/29/24


Cont.… 36

 Secure
 Object Oriented o With Java's security feature it enables

o In Java, everything is an Object to develop virus-free, tamper-free

o Can be easily extended. systems.


o Authentication techniques are based
o Supports every OOP principles
on public-key encryption.
 Simple: Designed to be easy to learn.
o Verifies the byte code
If you understand the basic concept of
OOP Java, it would be easy to master.
Compiled By: Asamnew Gizaw 07/29/24
Cont.… 37

 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

Compiled By: Asamnew Gizaw 07/29/24


Cont.… 38

 Portable  Architecture-neutral

o Running on different platform o Java compiler generates an

(window, Linux, Mac, Solaris). architecture-neutral object file format,

o The compiler in Java is written in which makes the compiled code


ANSI C with a clean portability executable on many processors, with the
boundary, which is a POSIX subset. presence of Java runtime system.
 Interpreted
o Java byte code is translated on the fly to native
machine instructions and is not stored anywhere. 07/29/24
Cont.… 39

 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

Compiled By: Asamnew Gizaw 07/29/24


41

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.

Compiled By: Asamnew Gizaw 07/29/24


Cont.… 42

 An object can be: -


 a physical thing, such as a Car
 a mental thing, such as an Idea.
 a natural thing, such as an Animal
 an artificial thing, such as an ATM.

Compiled By: Asamnew Gizaw 07/29/24


43

Cont.…
 Example
 Dog
 Attributes: breed, color, hungry, tired, etc.
 Behaviors: eating, sleeping, etc.

 Bank Account
 Attributes: account number, owner, balance
 Behaviors: withdraw, deposit

Compiled By: Asamnew Gizaw 07/29/24


44

Class

 Is the generic definition for a set of similar objects (i.e. Person as a


generic definition for different persons)
 Is an abstract description of a set of objects.
 A template used to create a set of objects. (A blue print to create
(instantiate) an object)

Compiled By: Asamnew Gizaw 07/29/24


45

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.

Compiled By: Asamnew Gizaw 07/29/24


46

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).

Compiled By: Asamnew Gizaw 07/29/24


47

Instance #1

 When the program runs there will be number: 054

many instances of the account class. balance: $19

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

Members of the Class

Compiled By: Asamnew Gizaw 07/29/24


49

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.

Compiled By: Asamnew Gizaw 07/29/24


51

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.

Compiled By: Asamnew Gizaw 07/29/24


Cont... 52

Compiled By: Asamnew Gizaw 07/29/24

You might also like