Вы находитесь на странице: 1из 7

Chapter 1

Object Oriented Programming

1.1 Introduction
Object-oriented programming (OOP) is a programming paradigm based on the
concept of "objects", which are data structures that contain data, in the form of fields,
often known as attributes; and code, in the form of procedures, often known as
methods. A distinguishing feature of objects is that an object's procedures can access
and often modify the data fields of the object with which they are associated (objects
have a notion of "this"). In object-oriented programming, computer programs are
designed by making them out of objects that interact with one another.There is
significant diversity in object-oriented programming, but most popular languages are
class-based, meaning that objects are instances of classes, which typically also
determines their type.
Many of the most widely used programming languages are multi-paradigm
programming languages that support object-oriented programming to a greater or
lesser degree, typically in combination with imperative, procedural programming.
Significant object-oriented languages include C++, Objective-C, Smalltalk, Delphi,
Java, C#, Perl, Python, Ruby and PHP.

1.2 Fundamental features and concepts


A survey by Deborah J. Armstrong of nearly 40 years of computing literature
identified a number of fundamental concepts found in the large majority of definitions
of OOP.
Not all of these concepts appear in all object-oriented programming languages. For
example, object-oriented programming that uses classes is sometimes called classbased programming, while prototype-based programming does not typically use
classes. As a result, a significantly different yet analogous terminology is used to
define the concepts of object and instance.
Benjamin C. Pierce and some other researchers view any attempt to distill OOP to a
minimal set of features as futile. He nonetheless identifies fundamental features that
support the OOP programming style in most object-oriented languages.
- Encapsulation
- Polymorphism

- Object inheritance

Similarly, in his 2003 book, Concepts in programming languages, John C. Mitchell


identifies four main features: dynamic dispatch, abstraction, subtype polymorphism,
and inheritance. Michael Lee Scott in Programming Language Pragmatics considers
only encapsulation, inheritance, and dynamic dispatch.
Additional concepts used in object-oriented programming include:
- Classes of objects (object constructors)
- Instances of classes (objects, which have been constructed via a class)
- Methods which act on the attached objects.
- Message passing
- Abstraction

1.3 Additional features


- Encapsulation enforces modularity
Encapsulation refers to the creation of self-contained modules that bind processing

functions to the data. These user-defined data types are called "classes", and one
instance of a class is an "object". For example, in a payroll system, a class could be
Manager, and Pat and Jan could be two instances (two objects) of the Manager class.
Encapsulation ensures good code modularity, which keeps routines separate and less
prone to conflict with each other.
-Inheritance

passes "knowledge" down

Classes are created in hierarchies, and inheritance lets the structure and methods in
one class pass down the hierarchy. That means less programming is required when
adding functions to complex systems. If a step is added at the bottom of a hierarchy,
only the processing and data associated with that unique step must be added.
Everything else above that step is inherited. The ability to reuse existing objects is
considered a major advantage of object technology.

-Polymorphism takes any shape


Object-oriented programming lets programmers create procedures for objects whose
exact type is not known until runtime. For example, a screen cursor may change its
shape from an arrow to a line depending on the program mode. The routine to move
the cursor on screen in response to mouse movement can be written for "cursor", and
polymorphism lets that cursor take simulating system behaviour.

Chapter 2
Database Connection using JDBC and Exception
Handling

Steps needed to connect the database using JDBC:

1. Import the required package


- Java.sql
- Javax.sql

2. Load and register the driver


The prototype for the class which is used is public static class forName(String) throws Class Not found Exception
Class Jdbc Demo1
{ public static void main (String []args)
{
try {
class.forName("Sun.jdbc.odbc.Jdbc:OdbcDriver")
system.outprintln("driver class successfully loaded")
}
catch(Class Not found Exception)
{}
}

3. Connect to the database


Our next job is to connect with the database. To do this java provides a method called
getConnection() which belongs to the Class DriverManager.

Prototype is public static Connection gertConnection(String conn.string, String user, String


Password)

Class Test
{ public static void main()
{ try {
class.forName("Sun.jdbc.odbc.Jdbc:OdbcDriver")
Connection conn = DriverManager.getConnection
("jdbc:odbc:user","username","pass");
}
catch(Class Not found Exception)
{}
}

4. Create Statement object


Our next step is to create an object which will be used to send SQL command to
database

Prototype isPublic statement createStatement() throws SQL Exception

5. Execute the query


According to JDBC, there are 2 types of SQL commands- public ResultSet executeQuery(String) throws SQL Exception
- public int executeUpdate(String) throws SQL Exception

6. Fetch the data


Our next aim is to retrieve the data. Now to do this, we assume we have the data
stored inside the resultSet.

We need to take 2 steps- Move the required pointer using resultSet.next()


- Retrieve the data stored in current row using public string getString(String), getInt(),
getBoolean(), getData(), etc.

7. Disconnect with the database


The final step in JDBC programming is to close the database connection which is
done by the method of close of connection() method.

Its prototype isPublic void close() throws SQL Exception

Complete Program
import java.sql.*;
Class MyDemoCode
{
{ public static void main (String []args)
{
try {
class.forName("Sun.jdbc.odbc.Jdbc:OdbcDriver")
system.outprintln("driver class successfully loaded")
}
try {
Connection conn = DriverManager.getConnection
("jdbc:odbc:user","username","pass");
Statement st = conn.createStatement();
ResultSet.rs = st.executeQuery(Select ename, salary from employee);

while(rs.next())
{ String str = rrs.getString(1);
int amt = rs.getInt(2);
system.out.println(str + \+ + amt);
}
conn.close();
}
catch(SQL Exception ex)
{}
}
}

Вам также может понравиться