Professional Documents
Culture Documents
They are also known as pillars of the object oriented programming paradigm.
1. Object:
Any entity that has state and behaviour is known as an object. For example: chair, pen, table,
keyboard, bike etc. It can be physical and logical.
2. Class:
Collection of objects is called class. It is a logical entity.
3. Abstraction
•Abstraction in OOPs is very easy to understand when you relate it to the real time example. For example,
when you drive your car you do not have to be concerned with the exact internal working of your car.
What you are concerned with is interacting with your car via its interfaces like steering wheel, brake
pedal, accelerator pedal etc. Here the knowledge you have of your car is abstract.
•In computer science, abstraction is the process by which data and programs are defined with a
representation similar in form to its meaning (semantics) while hiding away the implementation details.
•In more simple terms, abstraction is to hide information that is not relevant to context or rather show
only relevant information and to simplify it by comparing it to something similar in the real world.
•Abstraction captures only those details about an object that is relevant to the current perspective.
3.1 Data abstraction
Data abstraction is the way to create complex data types from multiple smaller data types
– which is more close to real life entity. e.g. An Employee class can be a complex object of
having various small associations.
public class Employee
{
private Department department;
private Address address;
private Education education;
//So on...
}
So, if you want to fetch information of a employee, you ask it from Employee object – as
you do in real life, ask the person itself.
3.2 Control abstraction
Control abstraction is achieved by hiding the sequence of actions for a complex task – inside a simple
method call, so logic to perform the task can be hidden from the client and could be changed in future
without impacting the client code.
public class EmployeeManager
{
public Address getPrefferedAddress(Employee e)
{
//Get all addresses from database
//Apply logic to determine which address is preferred
//Return address
}
}
In above example, tomorrow if you want to change the logic so that everytime domestic address is
always the preferred address, you will change the logic inside getPrefferedAddress() method, and client
will be unaffected.
4.Encapsulation
Wrapping data and methods within classes in combination with implementation
hiding (through access control) is often called encapsulation in OOPs. The result is
a data type with characteristics and behaviors. Encapsulation essentially has both
i.e. information hiding and implementation hiding.
Information hiding is done through using access control modifiers (public, private,
protected) and implementation hiding is achieved through creation of interface
for a class.
Implementation hiding gives the designer the freedom to modify how the
responsibility is fulfilled by an object. This is especially valuable at points where
the design (or even the requirements) are likely to change.
4.1. Information hiding
class InformationHiding
{
//Restrict direct access to inward data
private ArrayList items = new ArrayList();
//Provide a way to access data - internal logic can safely be changed in future
public ArrayList getItems(){
return items;
}
}
4.2 Implementation hiding
interface ImplemenatationHiding {
Integer sumAllItems(ArrayList items);
}
class InformationHiding implements ImplemenatationHiding
{
//Restrict direct access to inward data
private ArrayList items = new ArrayList();
//Provide a way to access data - internal logic can safely be changed in future
public ArrayList getItems(){
return items;
}
public Integer sumAllItems(ArrayList items) {
//Here you may do N number of things in any sequence
//Which you do not want your clients to know
//You can change the sequence or even whole logic
//without affecting the client
}
}
5. Inheritance
Inheritance is another important concept in object oriented programming. Inheritance in Java is
a mechanism by which one object acquires the properties and behaviors of the parent object.
It’s essentially creating a parent-child relationship between classes. In Java, you will use
inheritance mainly for code re-usability and maintainability.
Keyword “extends” is used to inherit a class in java. The “extends” keyword indicates that you
are making a new class that derives from an existing class. In the terminology of Java, a class
that is inherited is called a super class. The new class is called a subclass.
A subclass inherits all the non-private members (fields, methods, and nested classes) from its
superclass. Constructors are not members, so they are not inherited by subclasses, but the
constructor of the superclass can be invoked from the subclass.
class Employee
{
private Department department;
private Address address;
private Education education;
//So on...
}
class Manager extends Employee {
private List<Employee> reportees;
}
In above example, Manager is specialized version of Employee and reuses department, address
and education from Employee class as well as define it’s own reportees list.
6. Polymorphism
Polymorphism is the ability by which, we can create functions or reference
variables which behaves differently in different programmatic context.
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
Operator For casting a data type to another, a casting operator '()' is No operator required.
required.
Size of Data Types Destination type can be smaller than source type. Here the destination type must be larger
than source type.
Implemented It is done during program designing. It is done explicitly while compiling.
Conversion type Narrowing conversion. Widening conversion.
System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
}
}
Narrowing Casting:
Narrowing casting must be done manually by placing the type in parentheses in
front of the value:
Example
public class MyClass {
public static void main(String[] args) {
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
Syntax:
if(condition){
//code if condition is true
}else{
//code if condition is false
}
Example:
public class IfElseExample {
public static void main(String[] args) {
int number=13;
if(number%2==0){
System.out.println("even number");
}
else{
System.out.println("odd number");
}
}
}
Output:
odd number
Java IF-else-if ladder Statement
The if-else-if ladder statement executes one condition from multiple
statements.
Syntax:
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Example:
else if(marks>=80 && marks<90){
public class IfElseIfExample {
System.out.println("A grade");
public static void main(String[] args) {
}
int marks=65;
else if(marks>=90 && marks<100)
if(marks<50){ {
System.out.println("fail"); System.out.println("A+ grade");
} }else{
else if(marks>=50 && marks<60){ System.out.println("Invalid!");
System.out.println("D grade"); }
} }
else if(marks>=60 && marks<70){ }
System.out.println("C grade"); Output: C grade
}
else if(marks>=70 && marks<80){
System.out.println("B grade");
}
6.2 Selection Statement
Java Switch Statement
The Java switch statement is executes one statement from multiple
conditions. It is like if-else-if ladder statement.
Syntax:
switch(expression){
case value1:
//code to be executed; break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
Example:
public class SwitchExample {
public static void main(String[] args) {
int number=20;
switch(number){
case 10: System.out.println("10");break;
case 20: System.out.println("20");break;
case 30: System.out.println("30");break;
default:System.out.println("Not in 10, 20 or 30");
}
}
}
Output:20
Java Switch Statement if fall-through
The java switch statement is fall-through. It means it executes all statement after
first match if break statement is not used with switch cases.
Example:
public class SwitchExample2 {
public static void main(String[] args) {
int number=20;
switch(number){
case 10: System.out.println("10");
case 20: System.out.println("20");
case 30: System.out.println("30");
default:System.out.println("Not in 10, 20 or 30");
} }}
Output:
20
30
Not in 10, 20 or 30
6.3 Iteration Statements
Java For Loop
The Java for loop is used to iterate a part of the program several times. If the
number of iteration is fixed, it is recommended to use for loop.
There are three types of for loop in java.
•Simple For Loop
•For-each or Enhanced For Loop
•Labeled For Loop
Java Simple For Loop
The simple for loop is same as C/C++. We can initialize variable,check condition and
increment/decrement value.
Syntax:
for(initialization;condition;incr/decr){
//code to be executed
} Output:
1
Example: 2
public class ForExample { 3
public static void main(String[] args) { 4
5
for(int i=1;i<=10;i++){ 6
System.out.println(i); 7
} 8
9
} 10
}
Java For-each Loop
The for-each loop is used to traverse array or collection in java. It is easier to use than simple for
loop because we don't need to increment value and use subscript notation.
It works on elements basis not index. It returns element one by one in the defined variable.
Syntax:
for(Type var:array){
//code to be executed
}
Example:
Output:
public class ForEachExample {
12
public static void main(String[] args) {
23
int arr[]={12,23,44,56,78};
44
for(int i:arr){
56
System.out.println(i);
78
}
}}
Java Labeled For Loop
We can have name of each for loop. To do so, we use label before the for loop.
It is useful if we have nested for loop so that we can break/continue specific
for loop.
Normally, break and continue keywords breaks/continues the inner most for
loop only.
Syntax:
labelname:
for(initialization;condition;incr/decr){
//code to be executed
}
Example:
public class LabeledForExample { Output:
11
public static void main(String[] args)
12
{ aa:
13
for(int i=1;i<=3;i++){ bb: 21
for(int j=1;j<=3;j++){ If you use break bb;, it will break
if(i==2&&j==2){ break inner loop only which is the default
aa; behavior of any loop.
}
System.out.println(i+" "+j);
}
}
}
}
Example:
public class LabeledForExample {
Output:
public static void main(String[] args) {
aa: 11
for(int i=1;i<=3;i++){ 12
bb:
13
for(int j=1;j<=3;j++){
if(i==2&&j==2){ break bb; 21
} 31
System.out.println(i+" "+j);
32
}
} 33
}
}
Java Infinitive For Loop
If you use two semicolons ;; in the for loop, it will be infinitive for loop.
Syntax: for(;;){
//code to be executed
}
Example:
public class ForExample {
public static void main(String[] args) {
for(;;){
System.out.println("infinitive loop");
}
}
}
Output:
infinitive loop
infinitive loop CTRL+C
Java While Loop
The Java while loop is used to iterate a part of the program several times. If
the number of iteration is not fixed, it is recommended to use while loop.
Syntax:
while(condition){
//code to be executed
}
Output:
Example: 1
public class WhileExample { 2
public static void main(String[] args) { 3
int i=1;
4
while(i<=10){
5
System.out.println(i);
i++;
6
} 7
} 8
} 9
10
Java Infinitive While Loop
If you pass true in the while loop, it will be infinitive while loop.
Syntax:
while(true){ Output:
//code to be executed infinitive while loop
} infinitive while loop
Example: infinitive while loop
public class WhileExample2 { infinitive while loop
public static void main(String[] args) { infinitive while loop
while(true){ ctrl+c
System.out.println("infinitive while loop");
}
}
}
Java do-while Loop
The Java do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and
you must have to execute the loop at least once, it is recommended to use do-while loop.
It is executed at least once because condition is checked after loop body.
Syntax:
do{
//code to be executed
}while(condition); Output:
Example: 1
public class DoWhileExample { 2
public static void main(String[] args) { 3
int i=1; 4
do{ 5
System.out.println(i); i++; 6
}while(i<=10); 7
}
8
}
9
10
Java Infinitive do-while Loop
If you pass true in the do-while loop, it will be infinitive do-while loop.
Syntax:
while(true){
//code to be executed
}
Example:
public class DoWhileExample2 {
public static void main(String[] args) {
do{
System.out.println("infinitive do while loop");
}while(true); Output:
} infinitive do while loop
infinitive do while loop
}
infinitive do while loop....
6.4 Jump statements
1.Java Break Statement
The Java break is used to break loop or switch statement. It breaks the current flow of the
program at specified condition. In
case of inner loop, it breaks only inner loop.
Syntax:
break;
Java Break Statement with Loop
Example:
public class BreakExample { Output:
public static void main(String[] args) { 1
for(int i=1;i<=10;i++){ 2
if(i==5){
3
break;
} 4
System.out.println(i);
}
}
}
Java Break Statement with Inner Loop
It breaks inner loop only if you use break statement inside the inner loop.
Example:
public class BreakExample2 {
public static void main(String[] args) {
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
Output:
if(i==2&&j==2){ break;
11
}
12
System.out.println(i+" "+j);
13
}
21
}
31
}
32
}
33
2.Java Continue Statement
The Java continue statement is used to continue loop. It continues the current flow
of the program and skips the remaining code at specified condition. In case of inner
loop, it continues only inner loop.
Syntax:
continue;
Example:
Output:
public class ContinueExample { 1
public static void main(String[] args) { 2
for(int i=1;i<=10;i++){
3
if(i==5){
continue; 4
} 6
System.out.println(i); 7
}
8
}
} 9
10
Java Continue Statement with Inner Loop
It continues inner loop only if you use continue statement inside the inner
loop.
Example:
public class ContinueExample2 {
public static void main(String[] args) {
Output:
for(int i=1;i<=3;i++){ 11
for(int j=1;j<=3;j++){ 12
if(i==2&&j==2){ continue; 13
} 21
System.out.println(i+" "+j); 23
} 31
} 32
}
33
}
Difference bettween C++ and Java
Comparison Index C++ Java
Platform-independent C++ is platform-dependent. Java is platform-independent.
Mainly used for C++ is mainly used for system Java is mainly used for application
programming. programming. It i s w i de l y u s e d in
window, web-based, enterprise and
mobile applications.
Goto C++ supports goto statement. Java doesn't support goto statement.
Multiple inheritance C++ supports multiple inheritance. Java doesn't support multiple inheritance
through class. It can be achieved by
interfaces in java.
Operator Overloading C++ supports operator overloading. Java doesn't support operator overloading.
Pointers C++ supports pointers. You can writ Java supports pointer internally. But you
pointer program in C++. e can't write the pointer program in java. It
means java has restricted pointer support in
java.
Compiler and Interpreter C++ uses compiler only. Java uses compiler and interpreter both.
Call by Value and Call by C++ supports both call by value and call Java supports call by value only. There is no call by
reference by reference. reference in java.
Structure and Union C++ supports structures and unions. Java doesn't support structures and unions.
Thread Support C++ doesn't have built-in support for Java has built-in thread support.
threads. It
relies on third-party libraries for thread
support.
Virtual Keyword C++ supports virtual keyword so that we Java has no virtual keyword. We can override all
can non-static methods by default. In other words, non-
decide whether or not override a function. static methods are virtual by default.
unsigned right shift >>> C++ doesn't support >>> operator. Java supports unsigned right shift >>> operator
that fills zero at the top for the negative numbers.
For p o s i t i v e n u m b e r s , i t w o r k s s a m e l i k e > >
operator.
Inheritance Tree C++ creates a new inheritance tree Java uses single inheritance tree always because all
always. classes are the child of Object class in java. Object
class is the root of inheritance tree in java.