You are on page 1of 48

Object Oriented Programming

19CSE (2 Semester)
nd

Chapter#01: Introduction to Object


Oriented Programming & JAVA

1
Part-01
Introduction to Object Oriented
Programming

2
Outline
Introduction to subject &overview of syllabus
What is a program?
Types of programming languages
Procedural programming
Object Oriented Programming
Procedural v/s Object Oriented Programming
OOP fundamental building blocks/
Components/concepts/features
Concepts of object & classes

3
Introduction to Course
At end of this course, you will be able to:
 Identify the importance of JAVA
 Identify the additional features of JAVA as compared to C++
 Identify the difference between Applet and Application
 Apply object oriented principles including inheritance,
encapsulation, polymorphism and abstraction in your applications
 Create java applications/applets programs for:
 Exception handling
 Multithreading
 Filing
 GUI programming
What is a program?
 Computer programs, known as software, are instructions to
the computer.
 You tell a computer what to do through programs.
 Without programs, a computer is an empty machine.
 Computers do not understand human languages, so you
need to use computer languages to communicate with them.
 Programmers write instructions in various programming
languages, some directly understandable by computers and
others requiring intermediate translation steps.
These may be divided into three general types:
– Machine languages
– Assembly languages
– High-level languages
Machine Languages, Assembly
Languages and High-Level Languages
Machine Languages
• Any computer can directly understand only its own machine
language, defined by its hardware design.
– Generally consist of strings of numbers (ultimately reduced
to 1s and 0s) that instruct computers to perform their most
elementary operations one at a time.
– Machine dependent—a particular machine language can
be used on only one type of computer.
Assembly Languages and Assemblers
• English-like abbreviations that represent elementary
operations formed the basis of assembly languages.
• Translator programs called assemblers convert early
assembly-language programs to machine language.
Machine Languages, Assembly Languages and
High-Level Languages
High-Level Languages and Compilers
 High-level languages
 Single statements accomplish substantial tasks.
 Compilers convert high-level language programs into machine
language.
 Allow you to write instructions that look almost like everyday English
and contain commonly used mathematical notations.
 A payroll program written in a high-level language might contain a
single statement such as
 grossPay = basePay + overTimePay
Interpreters
 Compiling a high-level language program into machine
language can take considerable computer time.
 Interpreter programs, developer to execute high-level language
programs directly, avoid the delay or compilation, although
they run slower than compiled programs.
Popular High-Level Languages
 COBOL (COmmon Business Oriented Language)
 FORTRAN (FORmula TRANslation)
 BASIC (Beginner All-purpose Symbolic Instructional Code)
 Pascal (named for Blaise Pascal)
 Ada (named for Ada Lovelace)
 C (whose developer designed B first)
 Visual Basic (Basic-like visual language developed by Microsoft)
 Delphi (Pascal-like visual language developed by Borland)
 C++ (an object-oriented language, based on C)
 Java (We use in this subject)

8
Procedural Programming
 Procedural Programming  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.
 During a program’s execution, any given procedure might be
called at any point, including by other procedures or itself.
 A procedural program is divided into functions ( ideally, atleast)
each function has a clearly defined purpose & clearly defined
interface to the other functions in the program.
 Examples: FORTRAN, ALGOL, COBOL, BASIC, Pascal and C etc.
Problems with procedural programming/structural programming

 Un restricted Access
Function have unrestricted access to global data
 Real world Modeling
Un related functions and data, the basics of
procedural paradigm, provide a poor model of real
world.
 Difficult of creating new data types
Traditional languages are not extensible because they
will not let you create new data types.

10
What is Object Oriented Programming?
 As the name suggests, Object-Oriented Programming or OOPs
refers to languages that uses objects in programming.
 An object is a basic unit of Object Oriented Programming and
represents the real life entities.
 An object represents an entity in real world that can be distinctly
identified.
 For example: student, a desk, mobile, circle, button etc. can be
viewed as objects.
 Object-oriented programming aims to implement real-world
entities like inheritance, hiding, polymorphism etc. in
programming.
 The main aim of OOP is to bind together the data and the
functions that operate on them so that no other part of the code
can access this data except that function.
What is Object Oriented Programming?
 Two key features:
 Data hiding
 Encapsulation
 Data is hidden inside the object, no one can access data,
only particular method or member function can access
data.
 the data in a class is hidden from other classes, so it is
also known as data-hiding.
 The process of bundling both data and functions
together in a single entity is called encapsulation &
entity is called object.
Procedural v/s OOP
 The unit in procedural  The unit of object oriented
programming is function. programming is class.
 In procedural  In object oriented
programming, program is programming, program is
divided into small parts divided into small parts
called functions. called objects.
 Procedural programming  OO programming
follows top down follows bottom up
approach. approach.
 Adding new data and function is  Adding new data and function is
not easy. easy.

13
Procedural v/s OOP
 Procedural programming  Object oriented
concentrates on creating programming starts
functions. isolating the classes, and
 Procedural programming the look for the methods
separates the data of the inside the class.
program from the  OOP focus both of them
operations that
manipulate the data.
OOP fundamental building blocks/Components/concepts/features

 Four fundamental building blocks/components/concepts of OOP/features


 Inheritance
 Polymorphism
 Encapsulation
 Abstraction
OOP fundamental building blocks/Components/concepts

Inheritance
 Inheritance is the processing of creating new classes from existing class(es).
 The new created classes are called derived classes or child classes or sub
classes and the existing classes are called base classes or super classes
 The main advantages:
 existing classes remain same or un changed but we can add additional features in the derived
classes
 The code in base class need not be rewritten in the child class.
 The variables and methods of the base class can be used in the child
class as well.
 It provides the idea of reusability
OOP fundamental building blocks/Components/concepts

Encapsulation
 Encapsulation simply means binding object state(fields) and behaviors
(methods) together in to single unit (called class). If you are creating class, you
are doing encapsulation.
 For example: capsule, it is wrapped with different medicines
 A java class is example of encapsulation.
 Java bean class is fully encapsulated class because all the data members are
private here.

Abstraction
 Hiding internal details and showing functionality is known as abstraction
 Refers to the set of representing essential features without including the
background details or explanations
 Example: phone call, we don’t know the internal processing
 In java we use abstract class and interface to achieve abstraction
OOP fundamental building blocks/Components/concepts

Polymorphism
 The word polymorphism means many forms
 It comes from Greek word “Poly” which means many and “morphism” which
means form
 It is the ability to take more than one form
 For example: to draw shape, it can draw circle, rectangle, oval etc. depend
upon the type of object
 In java we use method overloading and method overriding to achieve
polymorphism
Other OOP concepts
Object
 Object means a real entity such as pen, chair, table, keyboard , bike etc.
 Any entity that has state and behavior is called object.
 It is a basic unit of Object Oriented Programming and represents the real life
entities. 
 It can be physical or logical
 An object is instance of class
 An object consists of:
 State : It is represented by attributes of an object. It also reflects the properties of an object.
 Behavior : It is represented by methods of an object. It also reflects the
response of an object with other objects.
 Identity : It gives a unique name to an object and enables one object to
interact with other objects.
Example 2: Object: Car
 Example1: house object
State: Color, Brand, Weight, Model
 State: color, address, area
 Behavior: open the house, close the house
Behavior: Break, Accelerate, Slow Down,
Gear change.
Other OOP concepts
Class
 A class is essential element of Object Oriented Programming
 A class is the template or prototype that defines the states and the behaviors
common to all objects of a certain kind.
 It is the collection of objects of similar type
 Classes are user define data types and behave like built in types of
programming language
 In general, a simple class declarations can include these components, in order:
 Modifiers: A class can be public or has default access
 Class name: The name should begin with a initial letter (capitalized by convention).
 Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword
extends. A class can only extend (subclass) one parent.
 Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any,
preceded by the keyword implements. A class can implement more than one interface.
 Body: The class body surrounded by braces, { }.
 In the body of class we define states and behaviors which are common to all objects of that
class.
Other OOP concepts
Message
 A single object by itself may not be very useful. An application contains many
objects. One object interacts with another object by invoking methods on that
object. It is also referred to as Method Invocation.
 Objects interact and communicate with each other by sending messages to
each other
 A message of an object is a request of execution of a procedure, and there fore
will invoke a method (procedure) in the receiving object that generates the
desired result.
 OOP involves three basic steps:
 Creating classes that defines the objects
and their behavior
 Creating objects from class definition
 Establishing communication between objects
through message passing
Part-02
Introduction to JAVA

22
Outlines
What and why Java?
History of Java
How Java is different from C?
How Java is different from C++?
Characteristics/Features of Java
Java Environment
Java IDE tools

23
What & Why Java is important?
What is JAVA?
 Java is a general purpose OO programming language.
 Write Once Run Anywhere (WORA)
 Designed for easy web/internet applications
 Java enables users to develop and deploy applications on the Internet for
servers, desktop computers, and small hand-held devices.
 Wide spread acceptance
Why JAVA? Two big reasons:
 Trouble with C/C++ language is that they are not portable and not
plate-form independent languages
 Emergence of world wide web, which demanded portable programs
 Portability and security necessitate the need of invention of Java

24
Java, Web, and Beyond
Java can be used to develop:
Stand alone applications
Web applications
 Java Applets
Java Servlets and Java Server Pages
Java can also be used to develop applications
for hand-held devices such as Palm and cell
phones

25
Examples of Java’s Versatility
 Standalone Application: TicTacToe

 Applet: TicTacToe

 Servlets: SelfTest Web site

 Mobile Computing: Cell phones


26
History of JAVA
 James Gosling initiated Java language project
in June 1991 for use in one of his many set-
top box projects. 
 The language, initially called ‘Oak’
 Sun released the first public implementation
as Java 1.0 in 1995. It promised to Write
Once, Run Anywhere (WORA), providing no-
cost run-times on popular platforms.
 On 13 November 2006, Sun released much of
Java as free and open source software under
the terms of the GNU General Public License
(GPL).
27
How is Java different from C?
C Language:
 Major difference is that C is a structure oriented language
and Java is an object oriented language and has
mechanism to define classes and objects.
 Java does not support an explicit pointer type
 Java does not have preprocessor, so we cant use #define,
 #include and #ifdef statements.
 Java does not include structures, unions and enum data
types.
 Java does not include keywords like goto, sizeof and
typedef.
 Java adds labeled break and continue statements.
 Java adds many features required for object
oriented programming.
How is Java different from C++?
C++ language
Features removed in java:
Java doesn’t support pointers to avoid
unauthorized access of memory locations.
Java does not include structures, unions and enum
data types.
Java does not support operator over loading.
Preprocessor plays less important role
in C++ and so eliminated entirely in
java.
Java does not perform automatic
typeconversions that
result in loss of precision.
How is Java different from C++?
 Java does not support global variables.
Every method and variable is declared within
a class and forms part of that class.
 Java does not allow default arguments.
 Java does not support inheritance of multiple
super classes by a sub class (i.e., multiple
inheritance). This is accomplished by using
‘interface’ concept.
 It is not possible to declare unsigned integers
in java.
 In java objects are passed by reference only. In
C++ objects may be passed by value or
reference.
How is Java different from C++?
New features added in Java:
 Multithreading, that allows two or more
pieces of the same program to execute
concurrently.
 C++ has a set of library functions that use a
common header file. But java replaces it with
its own set of API classes.
 It adds packages and interfaces.
 Java supports automatic garbage collection.
 break and continue statements have been
enhanced in java to accept labels as targets.
 The use of unicode characters ensures
portability.
How is Java different from C++?
Features that differ:
Though C++ and java supports Boolean data type,
C++ takes any nonzero value as true and zero as
false. True and false in java are predefined literals
that are values for a boolean expression.
Java has replaced the destructor function with a
finalize() function.
C++ supports exception handling that is similar to
java's. However, in C++ there is no requirement
that a thrown exception be caught.
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

33
Characteristics of Java
• Java Is Simple Java is partially modeled on C++, but greatly
simplified and improved. Some people refer to
• Java Is Object-Oriented Java as "C++--" because it is like C++ but
• Java Is Distributed with more functionality and fewer negative
• Java Is Interpreted aspects.

• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

34
Characteristics of Java
• Java Is Simple Java is inherently object-oriented.
Although many object-oriented languages
• Java Is Object-Oriented began strictly as procedural languages,
• Java Is Distributed Java was designed from the start to be
• Java Is Interpreted object-oriented. Object-oriented
programming (OOP) is a popular
• Java Is Robust programming approach that is replacing
• Java Is Secure traditional procedural programming
techniques.
• Java Is Architecture-Neutral
• Java Is Portable One of the central issues in software
• Java's Performance development is how to reuse code. Object-
oriented programming provides great
• Java Is Multithreaded flexibility, modularity, clarity, and
• Java Is Dynamic reusability through encapsulation,
inheritance, and polymorphism.

35
Characteristics of Java
• Java Is Simple Distributed computing involves several
computers working together on a network.
• Java Is Object-Oriented Java is designed to make distributed
• Java Is Distributed computing easy. Since networking
• Java Is Interpreted capability is inherently integrated into
Java, writing network programs is like
• Java Is Robust sending and receiving data to and from a
• Java Is Secure file.
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

36
Characteristics of Java
• Java Is Simple You need an interpreter to run Java
programs. The programs are compiled into
• Java Is Object-Oriented the Java Virtual Machine code called
• Java Is Distributed bytecode. The bytecode is machine-
• Java Is Interpreted independent and can run on any machine
that has a Java interpreter, which is part of
• Java Is Robust the Java Virtual Machine (JVM).
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

37
Characteristics of Java
• Java Is Simple Java compilers can detect many problems
that would first show up at execution time
• Java Is Object-Oriented in other languages.
• Java Is Distributed
• Java Is Interpreted Java has eliminated certain types of error-
prone programming constructs found in
• Java Is Robust other languages.
• Java Is Secure
Java has a runtime exception-handling
• Java Is Architecture-Neutral feature to provide programming support
• Java Is Portable for robustness.
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

38
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
Java implements several security
• Java Is Robust mechanisms to protect your system against
• Java Is Secure harm caused by stray programs.
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

39
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral Write once, run anywhere
• Java Is Portable
With a Java Virtual Machine (JVM),
• Java's Performance you can write one program that will
• Java Is Multithreaded run on any platform.
• Java Is Dynamic

40
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable Because Java is architecture neutral,
• Java's Performance Java programs are portable. They can
be run on any platform without being
• Java Is Multithreaded recompiled.
• Java Is Dynamic

41
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable Java’s performance Because Java is
• Java's Performance architecture neutral, Java programs
are portable. They can be run on any
• Java Is Multithreaded platform without being recompiled.
• Java Is Dynamic

42
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
Multithread programming is smoothly
• Java Is Multithreaded integrated in Java, whereas in other
• Java Is Dynamic languages you have to call procedures
specific to the operating system to enable
multithreading.
43
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance Java was designed to adapt to an evolving
environment. New code can be loaded on the
• Java Is Multithreaded fly without recompilation. There is no need for
• Java Is Dynamic developers to create, and for users to install,
major new software versions. New features can
be incorporated transparently as needed.
44
Java Environment
• Java includes many development tools, classes
and methods
– Development tools are part of Java
Development Kit (JDK) and
– The classes and methods are part of Java Standard
Library (JSL), also known as Application
Programming Interface (API).
• JDK constitutes of tools like java compiler,
java interpreter and many.
• API includes hundreds of classes and methods
grouped into several packages according to their
functionality.
JDK Versions
JDK 1.02 (1995)
JDK 1.1 (1996)
JDK 1.2 (1998)
JDK 1.3 (2000)
JDK 1.4 (2002)
JDK 1.5 (2004) a. k. a. JDK 5 or Java 5
J2SE 1.2 (December 8, 1998)
J2SE 1.3 (May 8, 2000)
J2SE 1.4 (February 6, 2002)
J2SE 5.0 (September 30, 2004)
Java SE 6 (December 11, 2006)
Java SE 7 (July 28, 2011)
46
JDK Editions
• Java Standard Edition (J2SE)
– J2SE can be used to develop client-side standalone
applications or applets.
• Java Enterprise Edition (J2EE)
– J2EE can be used to develop server-side applications
such as Java servlets and Java ServerPages.
• Java Micro Edition (J2ME).
– J2ME can be used to develop applications for mobile
devices such as cell phones.
This book uses J2SE to introduce Java
programming.
47
Java IDE Tools
Borland JBuilder
NetBeans Open Source by Sun
Sun ONE Studio by Sun MicroSystems
Eclipse Open Source by IBM

48

You might also like