Fundamentals of Object Oriented Programming
Fundamentals of Object Oriented Programming
KHK
OOP allows us to decompose the problem into number of entities called Objects.
Data of object can be accessed only by the methods associated with that object.
It follows Bottom-Up approach in program development.
User-interface design:
Another popular application of OOP has been in the area of designing graphical user
interfaces such as Windows. C++ is mainly used for developing user-interfaces.
Developing computer games:
OOP is also used for developing computer games . These games offer virtual reality
environments in which a number of objects interact with each other in complex ways
to give the desired result.
Scripting:
In recent years, OOP has also been used for developing HTML, XHTML and XML
documents for the Internet. Python, Ruby and Java are the scripting languages based
on object-oriented principles which are used for scripting.
Object Databases:
These days OOP concepts have also been introduced in database systems to develop a
new DBMS named object databases. These databases store the data directly in the
form of objects. However, these databases are not as popular as the traditional
RDBMS.
Office Automation Systems
These include formal as well as informal electronic systems primarily concerned with
information sharing and communication to and from people inside as well as outside
the organization. Some examples are:
Email
Word processing
Web calendars
Desktop publishing
CIM/CAD/CAM Systems
OOP can also be used in manufacturing and design applications as it allows people to
reduce the effort involved.OOP makes it possible for the designers and engineers to
produce flowcharts and blueprints accurately.
AI Expert Systems
These are computer applications which are developed to solve complex problems
pertaining to a specific domain, which is at a level far beyond the reach of a human
brain.
Class:
A class is a template definition of the methods and variables for a particular kind of
object. In other words , class is the blue print ( logical representation) from which an
individual objects is created.
Object:
An Object is a real time entity. It is also known as instance of a class.
An object will have some properties and it can perform some actions. Object contains
variables and methods.
The objects which exhibit similar properties and actions are grouped under one class.
Encapsulation:
Wrapping up of data (variables) and methods into single unit is called Encapsulation.
Encapsulation can be described as a protective mechanism that prevents the code and
data being accessed by other code defined outside the class.
Abstraction:
Providing the essential features without its inner details is called abstraction (or)
hiding internal implementation is called Abstraction.
We can enhance the internal implementation without effecting outside world.
Abstraction provides security.
Inheritance:
Acquiring the properties from one class to another class is called inheritance (or)
producing new class from already existing class is called inheritance.
Polymorphism:
The word polymorphism came from two Greek words „poly‟ means „many‟ and
„morphos‟ means „forms‟. Polymorphism represents the ability to assume several
different forms. The ability to define more than one function with the same name is
called Polymorphism.
Dynamic Binding:
It is also known as dynamic dispatch, it is the process of linking function call to a
specific function definition at run-time.
Dynamic binding is also known as late binding.
Message Passing:
OOPS includes objects which communicate with each other.
Objects communicate with one another by sending and receiving information much
the same way as people pass messages to one another.
Data
Data
Function Function
Delegation:
When objects of one class is used as data member in another class, such composition
of objects is known as delegation.
Such a relation between two classes is known as “ has a” relation.
1. Main program is divided into small parts 1. Main program is divided into small object
depending on the functions depending on the problem
2. Functions get more importance than data 2. Data gets more importance than functions
in program. in Program
3. Most of the functions use global data. 3. Each object controls its own data
4.Same data may be transfer from one 4.Data does not possible transfer from one
function to another object to another
5. There is no perfect way for data hiding 5. Data hiding possible in OOP which
prevent illegal access of function from
outside of it. This is one of the best
advantages of OOP also.
6. More data or functions can not be added 6. More data or functions can be added with
easily Program very easily
.
7. Top down process is followed for 7. Bottom up process is followed for
program design. program design.
Portable:
Java ensures portability in two ways. First java compiler generates bytecode
instructions that can be implemented on any machine. Secondly the size of the
primitive data types are machine – independent.
Distributed
Java is designed for the distributed environment of the Internet, because it handles
TCP/IP protocols
Java included features for intraaddress-space messaging. This allowed objects on two
different computers to execute procedures remotely.
Dynamic
Java programs carry with them substantial amounts of run-time type information that
is used to verify and resolve accesses to objects at run time. This makes it possible to
dynamically link code in a safe and expedient manner.
Security
Java systems not only verify all memory access but also ensure that no viruses are
communicated with an applet . the absence of pointers in java ensures that programs
can not gain access to memory locations.
The java runtime environment (JRE) facilitates the execution of programs developed
in java. It basically consists of the following:
Documentation Section:
The documentation comprises a set of comment lines giving the name of the program,
the author and other details, which the programmer would like to refer at a latter
stage.
Java supports three types of comments single line comments ( // ), multi line
comments(/*… */) , documentation comments(/** ... */)
Package Statements:
The first statement allowed in java file is a package statement, this statements declares
a package name and informs the compiler that the classes defined here belong to this
package.
package pack_name;
The package statement is optional, that is our classes do not have to be a part of a
package.
Import statement:
The next statement after a package statement may be a number of import statements.
This is similar to the #include statement in C.
import java . lang. * ;
import java . awt . * ;
This statement instructs the interpreter to load the classes contained in the packages
mentioned.
Interface statement:
An interface is like a class but includes a group of method declarations. This is also an
optional statement and is used only when we wish to implement the multiple
inheritance feature in the program. Interface is new concept in the java.
Class Definition:
A java program may contain multiple class definitions. Classes are the primary
essential elements of a java program. The no of classes used depends on the
complexity of the problem.
Main method class:
Since every java stand alone program requires a main method as its starting point.
This class is defined the essential part of a java program.
A simple java program may contain only this part. The main method creates objects
of various classes and establishes communications between them.
Bytecode is the result of compiling source code written in a language that supports
this approach.
Java Java
Byte Code
Program Compiler
The best-known language today that uses the bytecode is java, LISP , ICON, Prolog.
First of all, the .java program is converted into a .class file consisting of byte code
Keywords:
It is a special type of reserved word for a specific purpose in the program
development.
There are 50 keywords currently defined in the Java language. All the keywords must
be written in lower case
Ex: abstract, byte, class extends, private , protected , case , switch, if , else
Identifiers:
Identifiers are the names given by the programmer to variables, methods, and classes to
identify them to the compiler
The can have letters, numbers, or the underscore( _ ) and dollar-sign($) characters
Must not begin with a number.
Upper case and Lower case letters are distinct.
They can be of any length.
Valid Invalid
HelloWorld Hello World (uses a space)
Hi_JAVA Hi JAVA! (uses a space and punctuation mark)
value3 3value(begins with a number)
Tall short (this is a Java keyword)
$age #age (does not begin with any other symbol except _ $ )
Literals:
Literals in java are sequence of characters that represents constant values to be stored
in variables. Java language specifies five major types of literals:
Integer Literals. (10,20)
Floating point literals.(10.234)
Character literals.(„J‟)
String literals. (“java”)
Boolean literals. (true, false)
Operators:
Operator is a special symbol that takes one or more arguments and operates on them
to produce a result. Java provides a rich set of operators to manipulate variables. We
can divide all the Java operators into the following groups:
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Misc Operators
Separators:
Separators are used to inform the Java compiler of how things are grouped in the
code. For example, items in a list are separated by commas much like lists of items in
a sentence. The most commonly used separator in Java is the semicolon.
Symbol Name
; Semicolon
, Comma
{} Braces
() Parentheses
[] Brackets
. Period
Compiling a Program
The Java source programs have to compile it into the Java bytecode, the
intermediate code understood by the Java Virtual Machine (JVM).
The Java SDK compiler is named javac. At the Windows command prompt
HelloWorld.java file would be compiled by typing the following command :
javac HelloWorld.java
Running a Java Application Program
In order to run a java program (or execute) a program on any computer, the
program's .class file must be loaded into the computer's memory, where it is then
interpreted by the Java Virtual Machine(JVM). To run a Java program on Windows
command prompt, type
java HelloWorld
on the command line. This command loads the JVM, which will then load and
interpret the application's bytecode (HelloWorld.class).
Boolean Literals
Constants which stores either true or false.
The values of true and false do not convert into any numerical representation. The
true literal in Java does not equal 1, nor does the false literal equal 0.
Java defines eight primitive types of data: byte, short, int, long, char, float, double, and
boolean. The simple types are defined to have an explicit range and mathematical behavior.
These can be put in four groups:
Integers This group includes byte, short, int, and long, which are for whole valued
signed numbers.
Floating-point numbers This group includes float and double, which represent
numbers with fractional precision.
Characters This group includes char, which represents symbols in a character set,
like letters and numbers.
Boolean This group includes boolean, which is a special type for representing
true/false values.
Integer types:
Integer types can hold whole numbers such as 123, -90, 5678. The size of the values
that can be stored depends on the integer type we choose. Java supports four types of
integers , they are byte , short, int, long.
We can make integers long by appending the letter L or l at the end of the number
Ex:
123L or 123 l
Floating point types:
This type holds numbers containing fractional parts such as 27.68 and -1.342. java
supports two types of floating point storage float (single precision), double (double
precision)
By default all floating point numbers are double precision numbers. To make them
single precision numbers append f or F
Ex:
1.23 F
Type Size (bytes) Range
float 4 3.4 e -38 To 1.7 e+38
double 8 3.4e-38 To 1.7 e+308
Character type:
In order to store character constants in memory , java provides a character data type
called “char” . Java supports Unicode format of characters i.e. it can support 65536 no
of characters. It occupies 2 bytes of memory.
initializer. In addition, all variables have a scope, which defines their visibility, and a
lifetime.
Declaring a Variable
In Java, all variables must be declared before they can be used. The basic form of a
variable declaration is shown here:
type identifier [ = value][, identifier [= value] ...] ;
To declare more than one variable of the specified type, use a comma-separated list
Ex:
int m1,m2,m3;
Initializing Variables
You can initialize the variable by specifying an equal sign(assignment operator) and a
value. The initialization expression must result in a value of the same (or compatible)
type as that specified for the variable.
byte z = 22; // initializes z.
double pi = 3.14159; // declares an approximation of pi.
char x = 'x'; // the variable x has the value 'x'.
Dynamic Initialization
Java allows variables to be initialized dynamically, using any expression valid at the
time the variable is declared.
double a = 3.0, b = 4.0;
double c = Math.sqrt(a * a + b * b); // c is dynamically initialized
A boolean value cannot be assigned to any other data type. Except boolean, all the
remaining 7 data types can be assigned to one another either implicitly or explicitly;
but boolean cannot.
class cast_ex{
public static void main (String args[ ] ){
float x =254.01f;
byte b;
b = (byte) x ; /*type conversion*/
System.out.println( " 'value of b ' "+b);
}}
Relational operators
Relational operators generate a boolean result and binary operators. They evaluate the
relationship between the values of the operands.. The relational operators are
< Less than,
> Greater than,
<= Less than or equal to
>= Greater than or equal to
== Equivalent
!= Not equivalent
A simple relational expression contains only one relational operator and is of the
following form:
Op1 Relational Operator Op2
Ex:
a > b
Logical Operators:
The logical operators are used to form compound conditions by combining two or
more relations. The logical expressions also give a value of true or false according to
the truth table.java supports three types of logical operators:
&& Logical AND
|| Logical OR
! Logical NOT
Truth Table:
A B A || B A && B !A
false false false false true
true false true false false
false true true false true
true true true true false
Bitwise operators
The bitwise operators allow you to manipulate individual bits in an integral primitive
data type. Java the following bit wise operators :
bitwise AND operator ( & )
bitwise OR operator ( | )
var = expression;
Conditional Operator:
Java includes a special ternary (three-way) operator that can replace certain types of if-else
statements. The ? : has this general form:
Here, exp1 can be any expression that evaluates to a boolean value. If exp1 is true, then exp2
is evaluated; otherwise, exp3 is evaluated.
Shift operators
The shift operators also manipulate bits. They can be used solely with primitive,
integral types.
left-shift operator (<<)
right-shift operator (>>)
SPECIAL OPERATORS
Java supports some special operations of interest such as instanceof operator and
member selection operator( . ) and comma operator.