Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DBMS Lecture Notes Itt206

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

MODULE – 1

PRINCIPLES OF OBJECT
ORIENTED
TECHNIQUES
Ms Vrinda A
Assistant Professor,
Dept. of IT, MESCE,
Kuttippuram
Text Book

1. Herbert Schildt, Java: The Complete Reference, 8/e,


Tata McGraw Hill, 2011.
2. .Bahrami A., Object Oriented Systems Development
using the Unified Modeling
Object Oriented Vs Procedure
Oriented Programming
 Procedural Programming:
 It can be defined as a programming model which is derived
from structured programming, based upon the concept of
calling procedure.
 Procedures, also known as routines, subroutines or
functions, simply consist of a series of computational steps
to be carried out.
 Languages used in Procedural Programming: FORTRAN,
ALGOL, COBOL, BASIC, Pascal and C.
Object Oriented Vs Procedure Oriented
Programming
 Procedural Programming:
Object Oriented Vs Procedure Oriented
Programming

 Object Oriented Programming:


 It can be defined as a programming model which is based
upon the concept of objects.
 Objects contain data in the form of attributes and code in
the form of methods.
 Object oriented programming languages are class-
based, meaning that objects are instances of classes.
 Languages used in Object Oriented Programming:
Java, C++, C#, Python, PHP, JavaScript, Ruby, Perl.
Object Oriented Vs Procedure Oriented
Programming
 Object Oriented Programming:
Object Oriented Vs Procedure Oriented
Programming
Object Oriented Procedural Oriented
Key
Programming (OOP) Programming (POP)
Procedural Oriented Programming
Object-oriented Programming
is a programming language that
is a programming language
follows a step-by-step approach to
Definition that uses classes and objects break down a task into a collection
to create models based on the
of variables and routines through a
real world environment.
sequence of instructions.

In OOPs concept of objects


and classes is introduced and The main program is divided into
hence the program is divided small parts based on the functions
Approach into small chunks called and is treated as separate program
objects which are instances of for individual smaller program.
classes.
Object Oriented Vs Procedure Oriented
Programming
Object Oriented Procedural
Key
Programming Oriented
(OOP) Programming
(POP)

In OOPs access
Access modifiers are introduced On other hand no such
modifiers namely as Private, modifiers are introduced in
Public, and Protected POP.

Due to abstraction in OOPs


data hiding is possible and On other hand POP is less
Security hence it is more secure secure as compare to OOPs.
than POP.
The Object Oriented Approach

 Objects can be used effectively to represent


real- world entities.
 For instance, an object might represent a
particular employee in a company or an account in a
bank.
 Each employee object handles the processing
and data management related to that employee.
Objects
• An object has:
• state - descriptive characteristics
• behaviors - what it can do

• The state of a bank account includes its


account number and its current balance
• The behaviors associated with a bank account include
the ability to make deposits and withdrawals
• Note that the behavior of an object might change
its state
Objects
• In the context of programming, an object has methods,
attributes and can react to events.
• Method – Things which an object can do; the “verbs”
of objects.
• Attribute – Things which describe an object;
the “adjectives” of objects.
• Events – External forces to an object to which
that object can react.
Classes
• Class provides a way to create new objects based on a
definition of an object (Example: The animal class)

• An object is defined by a class


• A class is the blueprint of an object
• Multiple objects can be created from the same class
Classes and
Objects
• A class is a user defined data type that allows
programmers to create objects. A class provides a
definition for an object, describing an object‟s
attributes (data) and methods (operations).

• An object is an instance of a class. With one class,


you can have as many objects as required.
• This is analogous to a variable and a data type, the
class is the data type and the object is the variable.
Classes and
Objects
A class An object
(the concept) (the realization)

Bank John‟s Bank


Account Account Balance:
$5,257
Bill‟s Bank
Account Balance:
Multiple objects $1,245,069
from the same class Mary‟s Bank
Account Balance:
$16,833
Example: A “Rabbit”
object
• You could (in a game, for example) create an object
representing a rabbit
• It would have data:
• How hungry it is
• How frightened it is
• Where it is
• And methods:
• eat, hide, run, dig
Attributes

Contain current state of an object.


• Attributes can be classified as simple or complex.
• Simple attribute can be a primitive type
such as integer, string, etc., which takes on
literal values.
• Complex attribute can contain collections .
• Complex object: contains one or more
complex attributes
Methods and
Messages
• Method: Defines behavior of an object, as a set
of encapsulated functions.
• Message: Request from one object to another
asking second object to execute one of its methods.

(b)

(a) Object showing attributes and methods


(a) (b) Example of a method
Characteristics of Object-Oriented
Languages
Encapsulation
 In normal terms, Encapsulation is defined as
wrapping up of data and information under a single
unit.

 In Object-Oriented Programming, Encapsulation is


defined as binding together the data and the functions
that manipulate them.
Encapsulation
 Encapsulation leads to data abstraction or hiding.
 The different objects inside of each program will try
to communicate with each other automatically.
 If a programmer wants to stop objects
interacting with each from
other,
encapsulated in individual classes. they need to be
 Through the process of encapsulation, classes cannot
change or interact with the specific variables and
functions of an object.
Abstraction
 Abstraction means only essential
displaying information and hiding
the details.
 Data abstraction refers to providing only essential
information about the data to the outside world,
hiding the background details or implementation.
Abstraction
 Consider a real-life example of a man driving a car.
The man only knows that pressing the accelerators
will increase the speed of the car or applying brakes
will stop the car but he does not know about how on
pressing accelerator the speed is actually increasing,
he does not know about the inner mechanism of the
car or the implementation of accelerator, brakes etc in
the car.
Abstraction
 Abstraction can be achieved in two ways:
 Abstraction using Classes: The class helps us to group
data members and member functions using available
access specifiers. A Class can decide which data
member will be visible to the outside world and
which is not.
 Abstraction in Methods: Whenever we need to call a
built-in method, we simply call that method and pass
the required arguments without knowing the
underlying algorithm.
Polymorphism
 The word polymorphism means having many forms.
 It is defined as the ability of a message to be
displayed in more than one form.
 A person at the same time can have different
characteristic. Like a man at the same time is a father,
a husband, an employee.
 So the same person posses different behavior in
different situations. This is called polymorphism.
 We can perform polymorphism in java by method
overloading and method overriding.
Method Overloading

• When there are multiple functions with the same name


but different parameters then these functions are said
to be overloaded.

• Functions can be overloaded by change in the number


of arguments or/and a change in the type of
arguments.
Method overriding

• Occurs when a derived class has a definition for one


of the member functions of the base class. That
base function is said to be overridden.
Inheritance
 Inheritance can be defined as the process where one
class acquires the properties (methods and fields) of
another.
 With the use of inheritance the information is made
manageable in a hierarchical order.
 The class which inherits the properties of other is
known as subclass (derived class, child class) and the
class whose properties are inherited is known as
superclass (base class, parent class).
Inheritance
 Important terminology:
 Super Class: The class whose features are inherited is
known as super class (or a base class or a parent class).
 Sub Class: The class that inherits the other class is known
as a subclass (or a derived class, extended class, or child
class). The subclass can add its own fields and methods in
addition to the super class fields and methods.
 Reusability: Inheritance supports the concept of
“reusability”, i.e. when we want to create a new class and
there is already a class that includes some of the code that
we want, we can derive our new class from the existing
class. By doing this, we are reusing the fields and methods
of the existing class.
Java
Overview
What is Java
 Java is a popular programming language,
created in 1995.
 It is owned by Oracle, and more than 3 billion devices
run Java.
 It is used for:
 Mobile applications (specially Android apps)
 Desktop applications
 Web applications
 Web servers and application servers
 Games
 Database connection, etc.
Why Use Java?
 Platform Independent: Java works on different
platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
 Simple: It is easy to learn and simple to use
 It is open-source and free
 It is secure, fast and powerful
 Robust: Java makes an effort to eliminate error
prone situations
 Multithreaded: It is possible to write programs that
can perform many tasks simultaneously.
 Distributed: Java is designed for the
distributed environment of the internet.
Java Virtual Machine (JVM)
 JVM is an abstract machine that enables your computer to
run a Java program.
 When you run the Java program, Java compiler first
compiles your Java code to bytecode. Then, the JVM
translates bytecode into native machine code.
 Java is a platform-independent language. It's because when
you write Java code, it's ultimately written for JVM but not
your physical machine (computer).
 Since JVM executes the Java bytecode which is platform-
independent, Java is platform-independent.
Java Virtual Machine (JVM)

 What is JRE (Java Runtime Environment)?


 It is a software package that provides Java class
libraries, Java Virtual Machine (JVM), and other
components that are required to run Java
applications.
 JRE is a superset of JVM
Java Virtual Machine (JVM)
 What is JDK (Java Development Kit)?
 It is a software development kit required to develop
applications in Java.
 When you download JDK, JRE is also downloaded
with it.
 In addition to JRE, JDK also contains a number of
development tools (compilers, JavaDoc, Java
Debugger, etc).
Relationship between JVM, JDK, and JRE
Data Types

 Data types are divided into two groups:


 Primitive data types - includes byte,
short, int, long, float, double, boolean and char.
 Non-primitive data types - such as
String, Arrays and Classes.

 Primitive data type has no additional methods.


Primitive Data Types
 There are eight primitive data types in
Java:
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
Stores whole numbers from -2,147,483,648 to
int 4 bytes
2,147,483,647
Stores whole numbers from -9,223,372,036,854,775,808 to
long 8 bytes
9,223,372,036,854,775,807
Stores fractional numbers. Sufficient for storing 6 to 7
float 4 bytes
decimal digits
Stores fractional numbers. Sufficient for storing 15 decimal
double 8 bytes
digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values
Non-primitive Data Types
 Otherwise called reference types because they refer to objects.
 The main difference between primitive and non-primitive data
types are:
 Primitive types are predefined (already defined) in Java.
Non-primitive types are created by the programmer and is
not defined by Java (except for String).
 Non-primitive types can be used to call methods to
perform certain operations, while primitive types cannot.
 A primitive type has always a value, while non-primitive
types can be null.
 The size of a primitive type depends on the data type,
while non-primitive types have all the same size.
Operators

 Java divides the operators into the following groups:


 Arithmetic operators (+, -, *, /, %, ++, --)

 Assignment operators ( =, +=, -=, etc )

 Comparison operators (==, !=, <, >, <=, >=)

 Logical operators (&&, ||, !)

 Bitwise operators (&, |, ^, ~, >>, <<, )


Control Statements
 Normally, statements in a execute
program sequentially.
 Some instructions enable you to the next
select statement to be executed.

 All programs could be written in three


control structures:
 sequence structure
 selection structure - if, if-else, switch, conditional operator
 repetition structure - while, do…while, for and for…in
Control Statements

Sequence Structure
if Selection
Statement

if (grade >= 60 )
System.out.println( “Passed" );
if -else Selection
Statement

if ( grade >= 60 )
System.out.println(“Passed");
else
System.out.println(“Failed");
Question
 Write a program to check a given number is negative or
not.
 Write a program to check whether the given number is
odd number or even number.
 Write a program to find the largest of two numbers.
 Write a program to find the largest of three numbers.
Nested if…
if ( grade else
>=
90 System.out.println(
) "A" );
else if ( grade >= 80 )
System.out.println( "B" );
else if ( grade >= 70 )
System.out.println( "C" );
else if ( grade >= 60 )
System.out.println( "D" );
else
System.out.println( "F" );
Questions
 Write a menu-driven program to perform the different
mathematical operations.

 Write a program to print the grade of a student based on


the total marks obtained from 3 subjects. Consider 10
marks for each subject (26 - 30: Grade A, 21 – 25:
Grade B, 16 – 20: Grade C, <=15: F).
Repetition Statement while

int count = 1;
while ( count <= 100 )
{
System.out.println(count);
}
Repetition Statement while
int i = 1, sum =
0; while ( i <=
100 )
{
sum = sum +
i; i = i +
1;
}
Repetition Statement for

for (int i = 1; i < 10; i++)


{
System.out.println(“Welcome");
}
do…while Repetition Statement

do
{
.....
.....
}while(condition);
Questions
 Write a program to find the sum of squares of first 10
numbers.
 Write a program to print all the even numbers between
two limits.
 Write a program to find the factorial of a number.
break and continue Statement
 The break causes immediate exit from the loop statements.
 Execution continues with the first statement
after the structure.

class Test {
public static void main(String[] args) {
// for loop
for (int i = 1; i <= 10; i++) {
// if the value of i is 5 the loop terminates
if (i == 5) {
break;

}
System.out.println(i);
}
}}
break and continue Statement
 The continue statement skips the remaining statements
in the body of the statement and proceeds with the next
iteration of the loop.

for ( var count = 1; count <= 10; ++count )


{
if ( count ==
5 ) continue;
System.out.println(
count + " " );
} //end for
switch Multiple-Selection
Statement
switch(choice)
{
case 1:
...
break;
case 2:
..
.
break;
default:
..
.
Question
 Write a menu-driven program to perform all arithmetic
oprations.
Class and Objects
class Employee
{
private String name;
private double salary;
Employee(String n, double s)
{
name = n; salary = s;
}

void pay ()
{
System.out.println("Pay
to the Employee " +
name + " $" +
salary);
}
}
How to declare and create objects?

Employee secretary; // declares secretary


secretary = new Employee (); // allocates space
Employee secretary = new Employee(); // does
both

• But the secretary is still "blank" (null)

secretary.name = “Liya”; // dot


notation secretary.pay(); // sends a
message
Constructor
• It is a special type of method used to initialize
the object.

• It is invoked at the time of object creation.

• It provides data for the object that is why it is called


constructor.

• Constructor name must be same as its class name


Constructor
• A constructor that have no parameter is known
as default constructor.
• If thereis no
For constructor,
Example compiler
automatically creates
class Bike{ a default constructor.
Bike()
{ • It provides
System.o values
default to the object
ut.println like 0, null etc.
("Bike is depending on the
created") type.
} ;
}
Constructor

• A constructor that have parameters is known


as parameterized constructor.

• Parameterized constructor is used to provide different


values to the distinct objects.
Constructor
For example:
public static void main(String
class Student{
args[ ]){
int id;
Student s1
String name; = new
Student(int i, String Student(111, “Arun"); =
Student
n) new
{ s2
id = i; Student(222, “James");
name = n; s1.display();
} s2.display();
void display() { }
System.out.println(id+
}
" "+name);
}
this
Keyword
• „this‟is a type of object.
• Inside a class, no dots are necessary, because
• you are working on „this‟object
• If you wish, you can make it explicit:
class Person {
...
this.age = this.age + 1;
}

• You usually don't need to use this


Garbage

Collection
Java garbage collection is the process by which Java
programs perform automatic memory management.
• When Java programs run on the JVM, objects
are created on the heap,
• a portion of memory dedicated to the program.

• Eventually, some objects will no longer be needed.


• The garbage collector finds these unused objects and
deletes them to free up memory.
Garbage

Collection
Java garbage collection is the process by which Java
programs perform automatic memory management.
• When Java programs run on the JVM, objects
are created on the heap,
• a portion of memory dedicated to the program.

• Eventually, some objects will no longer be needed or


urreachable.
• The garbage collector finds these unused objects and
deletes them to free up memory.
er i = new Integer(4); // the new Integer object is reachable via the reference in 'i' i = null; // the Integer object is no longer reachable.

Benefits of Garbage
Collection
• It automatically handles the deletion of unused or
unreachable objects to free up memory resources.
• Programmers working in languages without garbage
collection (like C and C++) must implement manual
memory management in their code.

Integer i = new Integer(4);


// the new Integer object is reachable via the reference in „i‟

i = null; // the Integer object is no longer reachable.


Garbage Collection: Example
public class Test Runtime.getRuntime().gc();
{
// finalize method is called on
public static void main(String[] args) object once before garbage
throws InterruptedException collecting it
{
protected void finalize()
Test t1 = new throws Throwable
Test(); Test t2 =
{
new Test(); t1 =
System.out.println("Garbag
null;
e collector called");
// requesting JVM for
}
running Garbage Collector
System.gc(); }
t2 = null; }
// requesting JVM for
running Garbage Collector

You might also like