Professional Documents
Culture Documents
Fundamentals of Object Oriented Programming
Fundamentals of Object Oriented Programming
UNIT - I It ties the data more closely to the functions that operate on it.
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.
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.
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: Message Passing:
OOPS includes objects which communicate with each other.
Acquiring the properties from one class to another class is called inheritance (or) producing new class
Objects communicate with one another by sending and receiving information much the same ay as
from already existing class is called inheritance.
people pass messages to one another.
Reusability of code is main advantage of inheritance.
The relation between the two class is known as “is a” relation.
Delegation:
Function
Function
When objects of
3 4
JAVA UNIT-1 JAVA UNIT-1
one class is used as data member in another class, such composition of objects is known as delegation. Java designed as pure object-oriented language. We require minimum one class to develop a program.
Such a relation between two classes is known as “ has a” relation. The object model in Java is simple and easy to extend.
Robust:
Explain Differences between POP and OOP: The ability to create robust programs was given a high priority in the design of Java.
Procedure Oriented Programming Object Oriented Programming Java is a strictly typed language; it checks your code at compile time also at run time.
1.Main program is divided into small object Java eliminates manually disallocati0n of dynamically allocated memory i.e. deallocation is completely
1.Main program is divided into small parts
depending on the problem automatic.
depending on the functions Java provides object-oriented exception handling. In a well-written Java program, all run-time errors can
and should be managed by your program.
2. 2. Functions get more importance than 2. Data gets more importance than functions
Multithreaded:
data in Program Java supports multithreaded programming, which allows you to write programs that do many things
in program. simultaneously.
3. Most of the functions use global data. 3. Each object controls its own data This feature greatly improves the interactive performance of graphical and game applications.
4.Same data may be transfer from one 4.Data does not possible transfer from one
function to another object to another Platform independent and Architecture-Neutral:
5. There is no perfect way for data hiding 5. Data hiding possible in OOP which prevent illegal Java programs can be easily moved from one computer to another , anywhere and anytime.
access of function from outside of it. This is one of The Java Virtual Machine (JVM) made the java language platform independent and architecture neutral.
the best advantages of OOP also. Interpreted and High Performance:
6. More data or functions can not be added 6. More data or functions can be added with Program Java enables the creation of cross-platform programs by compiling into an intermediate representation
. The Java bytecode was carefully designed so that it would be easy to translate directly into native
7. Top down process is followed for 7. Bottom up process is followed for program design. machine code for very high performance by using a just-in-time compiler.
Simple: Dynamic:
Java inherits the C/C++ syntax and many of the object-oriented features of C++. Also, some of the more Java programs carry with them substantial amounts of run-time type information that is used to verify
confusing concepts from C++ are eliminated from Java and resolve accesses to objects at run time. This makes it possible to dynamically link code in a safe and
7 8
JAVA UNIT-1 JAVA UNIT-1
other details, which the programmer would like to refer at a later stage.
Java Java
Java supports three types of comments single line comments ( // ), multi-line comments (/*… */), Byte Code
Program Compiler
documentation comments (/** ... */).
Package Statements: The best-known language today that uses the bytecode is java, LISP , ICON, Prolog.
The first statement allowed in java file is a package statement, this statement declares a package name
and informs the compiler that the classes defined here belong to this package. What is Java Virtual Machine:
package pack name;
Java Virtual Machine (JVM) is the heart of entire Java program execution process. First of all, the .java
The package statement is optional, that is our classes do not have to be a part of a package.
program is converted into a .class file consisting of byte code instructions by the java compiler at the
Import statement: time of compilation.
The next statement after a package statement may be a number of import statements. This is similar to The JVM exists only inside the computer memory, it is a simulated computer with in the computer
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
Explain Java Tokens?
the essential part of a java program.
A simple java program may contain only this part. The main method creates objects of various classes A token is the smallest element of a program that is meaningful to the compiler. These tokens define the
and establishes communications between them. structure of the Java language. The compiler recognizes them for building up expressions and statements.
Java tokens can be broken into five categories:
What is byte code?
Identifiers
Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system,
keywords
which is called the Java Virtual Machine (JVM). That is, in its standard form, the JVM is an interpreter
literals
for bytecode.
operators
Bytecode is the result of compiling source code written in a language that supports this approach.
separators
Keywords:
It is a special type of reserved word for a specific purpose in the program development.
9 10
JAVA UNIT-1 JAVA UNIT-1
There are 50 keywords currently defined in the Java language. All the keywords must be written in lower Logical Operators
case Assignment Operators
Ex: abstract, byte, class extends, private , protected , case , switch, if , else Misc Operators
Identifiers: Separators:
Identifiers are the names given by the programmer to variables, methods, and classes to identify them to Separators are used to inform the Java compiler of how things are grouped in the code. For example,
the compiler items in a list are separated by commas much like lists of items in a sentence. The most commonly used
The can have letters, numbers, or the underscore (_) and dollar-sign ($) characters separator in Java is the semicolon.
Must not begin with a number.
Upper case and Lower-case letters are distinct. Symbol Name
They can be of any length. ; Semicolon
, Comma
Valid Invalid {} Braces
HelloWorld Hello World (uses a space) () Parentheses
Hi_JAVA Hi JAVA! (uses a space and punctuation mark) [] Brackets
11 12
JAVA UNIT-1 JAVA UNIT-1
A statement is an executable combination of tokens ending with a semicolon( : ). Mark. Statements are String literal
usually executed in sequence in the order in which they apper.. java implements several types of Boolean literals
Declaration statement: An integer constant refers to a sequence of digits. Java supports three types of integer literals.
The statement that declares a variable of a particular type. In Java, a variable must be declared anywhere Decimal (Base 10)
before it can be used in a program. Floating-point numbers represent decimal values with a fractional component. They can be expressed in
The statement that stores (assigns) a value in a variable. An assignment statement uses the equal sign (=) double d2 = 1.234e2; // same value as d1, but in scientific notation float f1 = 123.4f;
as an assignment operator. Like other statements, an assignment statement ends with a semicolon: Character constants:
variable Name = Value; greeting = "Hello World"; A single character constant contains a single character enclosed with in a pair of single quote marks. Java
num1 = 50; // (a) Assign 50 to num1 num2 = supports Unicode character set.
num1 = num2; // (c) Copy num2's value (25) into num1 A string constant is a sequence of characters enclosed between double quotes. The characters may be
Control flow statements: alphabets, digits, special characters and blank spaces.
Regulate the order in which statements get executed. We can write statements using if, switch, loops. “Hello World” “two\nines”
Jump Statements: Java string is that they must begin and end on the same line.
Jump statements pass control to the beginning or end of the current block, or to a labelled statement. The Boolean Literals:
labels must be in the same block. java supports four types of jump statements: Constants which stores either true or false.
Blocks The values of true and false do not convert into any numerical representation. The
break true literal in Java does not equal 1, nor does the false literal equal 0.
We can make integers long by appending the letter L or l at the end of the number Ex:
Explain java Data Types? 123L or 123 l
Data types specify the size and type of values that can be stored. java language is rich in its data types. Floating point types:
Java supports two types of data types This type holds numbers containing fractional parts such as 27.68 and -1.342. java supports two types of
Primitive data types floating-point storage float (single precision), double (double precision)
Reference data types
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 By default, all floating-point numbers are double precision numbers. To make them single precision
Type Size (bits) Range Data Type Default Value (for fields)
boolean 1 true or false byte 0
short 0
A variable is an identifier that denotes a storage location used to store a data value. A variable is defined long 0L
by the combination of an identifier, a type, and an optional initializer. In addition, all variables have a float 0.0f
In Java, all variables must be declared before they can be used. The basic form of a variable declaration String (or any object) null boolean false
is shown here: The compiler never assigns a default value to an uninitialized local variable.
A scope determines what objects are visible to other parts of your program. It also determines the System.out.println(y); // prints 10.0
lifetime of those objects. In the above code 4 bytes integer value is assigned to 8 bytes double value.
Variables are created when their scope is entered, and destroyed when their scope is left. Explicit casting (narrowing conversion)
When you declare a variable within a scope, you are localizing that variable and protecting it from A data type of higher size (occupying more memory) cannot be assigned to a data type of lower size.
unauthorized access and/or modification. The scope rules provide the foundation for encapsulation. This is not done implicitly by the JVM and requires explicit casting; a casting operation to be performed
by the programmer. The higher size is narrowed to lower size.
What is a default value of a variable?
Syn: type variable1 = (type) variable2
It's not always necessary to assign a value when a field is declared. Fields that are declared but not
double x = 10.5;
initialized will be set to a reasonable default by the compiler.
17 18
JAVA UNIT-1 JAVA UNIT-1
var = expression;
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 manipulates 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 instance of operator and member selection
operator( . ) and comma operator.
What is an Operator Precedence and Associativity?
Each operator in java has a precedence associated with it. This precedence is used to determine how an
expression involving more than one operator is evaluated. The operators of the same precedence are
21 22