Object Oriented Programming With JAVA
Object Oriented Programming With JAVA
• INTRODUCTION
• OOPS PRINCIPLES
• Arrays
• Operators
INTRODUCTION
Key features of Java
security
portability
Java is purely object oriented programming (OOP) language. Here, we will discuss the basics
of OOPs
concepts.
Two Paradigms :
Encapsulation
Inheritance
Polymorphism
Abstraction :
Abstraction can be thought of as hiding the implementation details from the end-user. A powerful way
to manage abstraction is through the use of hierarchical classifications. This allows us to layer the semantics
vehicle and can be thought of as a single object. But, from inside, car is a collection of several subsystems
of complex systems, breaking them into more manageable pieces. For example, we consider a car as a
viz. steering, brakes, sound system, engine etc. Again, each of these subsystems is a collection of individual
parts (Ex. Sound system is a combination of a radio and CD/tape player). As an owner of the car, we manage
it as an individual entity by achieving hierarchical abstractions.
Hierarchical abstractions of complex systems can also be applied to computer programs. The data from a
traditional process-oriented program can be transformed by abstraction into its component objects. A sequence of
process steps can become a collection of messages between these objects.
Java program compilation
A java program source code is a text file containing one or more class
definitions is called as compilation unit.
When one type of data is assigned to another type of variable, an automatic type conversion will
take Java’s Automatic Conversions place if the following two conditions are met:
Here, target-type specifies the desired type to convert the specified value to.
For example,
int a; byte b; b = (byte) a;
Illustration of type conversion
class Conversion }
{ System.out.println("d and b " + d + " " +
public static void main(String args[]) b);
}
{
byte b;
int i = 257;
double d = 323.142;
System.out.println("\nConversion of
int to byte.");
b = (byte) i;
System.out.println("i and b " + i + "
" + b);
System.out.println("\nConversion of
double to int.");
i = (int) d;
System.out.println("d and i " + d + "
" + i);
System.out.println("\nConversion of
double to byte."); b
= (byte) d;
ARRAYS
System.out.println();
}
}
}
OPERATORS
Arithmetic
Relational
Bit-wise
Logical
There are compound assignment operators for all of the arithmetic, binary operators. Thus,
any statement of the form :
var = var op expression;
can be rewritten as : var op= expression; (in java)
Here is a sample program that shows several op= assignments in
action:
// Demonstrate several assignment operators.
class OpEquals {
public static void main(String[] args) {
int a = 1;
int b = 2;
int c = 3; The output of this program is shown
a += 5; here:
b *= 4; a=6
c += a * b; b=8
c %= 6; c=3
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
Increment and Decrement
The increment operator increases its operand by one. The decrement operator decreases
its operand by one. For example, this statement:
x = x + 1;
can be rewritten like this by use of the increment operator:
x++;
Similarly, this statement:
x = x - 1;
is equivalent to
x--;
The following program demonstrates the increment operator .
// Demonstrate ++.
class IncDec {
public static void main(String[] args) {
int a = 1;
int b = 2; The output of this program
int c; follows:
int d; a=2
c = ++b; b=3
d = a++; c=4
c++; d=1
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
SHIFT OPERATORS: