Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: JAVA OOPS COMPLETE CONCEPT
Description: This notes will be useul for all the students who want to learn java in easy manner its unti_1 form jnuth university if u are interested other topics i can share u... but this copy is very rare u cannot more simpler than this notes..

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


UNIT-1
Object-Oriented Thinking - A way of viewing the World
Agents and Communities:
➢ An object-oriented program is structured as a community of interacting agents, called
objects
➢ Consider the scenario where I (stays at Hyderabad) wants to send a flower bouquet to my
friend, who is at Chennai
...


OOPS Concepts
Object
➢ It is the basic unit of Object Oriented Programming and it represents the real life entities
...
etc
...

An Object Consists of
➢ State: It is represented by attributes of an object
...

➢ Identity: It gives a unique name to an object
...

➢ It represents the set of properties or methods that are common to all objects of one type
...


Data Hiding & Encapsulation
Using private access modifiers(access specifiers), we can hide data from other classes
...

A java class is the example of encapsulation
...

For example phone call, we don't know the internal processing
...

Inheritance
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors
of a parent object
...

When you inherit from an existing class, you can reuse methods and fields of the parent class
...

Inheritance represents the IS-A relationship which is also known as a parent-child relationship
...
etc
...

For Code Reusability
...

Generally it occurs when we have many classes that are related to each other by inheritance
...


Differences between OOP and Procedure oriented programming
Basis For
comparison

POP

OOP

Basic

Procedure/Structure oriented
...


Approach

Top-down
...


Basis

Main focus is on "how to get the
task done" i
...
on the procedure or
structure of a program
...

Hence, only objects are permitted
to access the entities of a class
...


Entire program is divided into
objects
...


Access specifier are "private,
default, protected & public”
...


It overloads functions,
constructors
...


Supports Inheritance
...
hence data
security increases
...


Data is shared among the objects
through the member functions
...

Java inherits the C/C++ syntax and many of the object-oriented features of C++
...

Object Oriented
In java everything is an Object which has some data and behavior
...

We can’t have any method outside of the class
...

Platform Independent:
C/CPP compiler produces “
...
exe” file are specific to OS(ie platform dependent)
...
class” file (byte code), which is interpreted on any platform with
corresponding JVM
...
class” file (byte code) is platform independent
...

JVM is the java interpreter, which converts java byte code into machine language
...

It is part of JRE(Java Runtime Environment)
...

Java Compiler generates ‘
...

Java Interpreter interprets this byte code, converts into an executable file (exe file)
...


Java byte code was carefully designed so that it would be easy to translate directly into native
machine code for very high performance by using a just-in-time compiler
...

Operating system upgrades, processor upgrades, and changes in any core system resources does
not make a program to malfunction
...

There is no pointers, avoids security problems
...
)
Java makes an effort to eliminate error-prone situations by emphasizing mainly on compile time
error checking and runtime checking(exception handling & type checking)
...

Multithreading is a special type of Multitasking
...

Each task is a separate program
...

The aim of multi tasking is to reduce this idle time of the processor
...

Multitasking- Advantages:
Throughput(no
...

Quick response time for jobs(i
...

Thread:
Part of a program
...


Multithreading:
Running multiple threads of a program simultaneously
...

RMI (Remote Method Invocation) and EJB (Enterprise Java Bean) are used for creating
distributed applications
...

Secure
No explicit pointer
Java Programs run inside a virtual machine sandbox
...


Primitive Data types:
Keyword

Type

Example

boolean

true or false

true

byte

8-bit integral value

123

short

16-bit integral value

22456

int

32-bit integral value

123

long

64-bit integral value

3123456789L

float

32-bit floating-point value

123
...
456

char

16-bit Unicode value

'a'

Why char uses 2 bytes ?
java uses Unicode system not ASCII code system
...

In Unicode, character holds 2 byte, so java also uses 2 byte for characters
...

The value depends on the data type of the variable
...

There are three types of variables in java: local, instance and static
...

Example-1: 10+15
Example-2: (x*y)/z;

Type Casting:
Convert a value from one data type to another data type is known as type casting
...

It is also known as implicit conversion or casting down
...

It is safe because there is no chance to lose data
...

Example:
int x = 7;
//automatically converts the integer type into long type
long y = x;
Narrowing Type Casting: Converting a higher data type into a lower one
...

It is done manually by the programmer
...

Example:
double d = 166
...
out
...
out
...
out
...
out
...
out
...
out
...

It returns bit by bit OR of input values, i
...

For example, a = 5 = 0101 (In Binary), b = 7 = 0111 (In Binary)
Bitwise OR Operation of 5 and 7

0101
| 0111
________
0111 = 7 (In decimal)
Bitwise AND (&):
This operator is a binary operator, denoted by ‘&’
...
e, if both bits are 1, it gives 1, else it gives 0
...

It returns bit by bit XOR of input values, i
...

For example,
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
Bitwise XOR Operation of 5 and 7
0101
^ 0111
________
0010 = 2 (In decimal)
Bitwise Complement (~) –
This operator is a unary operator, denoted by ‘~’
...
e, with all bits inverted,
which means it makes every 0 to 1, and every 1 to 0
...

The leftmost bit and a depends on the sign of initial number
...

Example 1:
a = 10
a>>1 = 5
Example 2:
a = -10
a>>1 = -5
Left shift operator (<<):
Shifts the bits of the number to the left and fills 0 on voids left as a result
...

Examples:
a = 5 = 0000 0101
b = -10 = 1111 0110

a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20

b << 1 = 1110 1100 = -20
b << 2 = 1101 1000 = -40

6) Logical Operators(Logical Short Circuit Operators): &&, ||
Example:
if ( x != null

&&

x
...

7) Ternary : ? :
Example:
int a=2;
int b=5;
int min=(aSystem
...
println(min);
8) Assignment: = += -= *= /= %= &= ^= |= <<= >>= >>>=
Example:
int a=10;
int b=20;
a+=4;//a=a+4 (a=10+4)
b-=4;//b=b-4 (b=20-4)
System
...
println(a);
System
...
println(b);

Access Modifiers / Access Specifiers
Access modifiers tells us the scope of the variable(ie where a variable can be accessed)
...

default: these memebers can be accessed in any class with in the package
...
Also if the class to
which these members belong to has any derived classes in other packages, those classes also can
access
...


instance variables Vs static variables
Instance variables:
Each object has a separate copy of value for instance variables
...

Static variables:
For a static variable, there will be only one copy of value in the memory for a given class,
irrespective of how many objects the class has
...

We can access static variables with class name itself
...


Static method Vs Non-Static method
Non Static (instance) Method:
These methods operate on instance variables of the class
...

Static methods:
These behavior of these methods is common to all objects
...


Wrapper Classes:
Byte, Short, Integer, Long, Float, Double, Character & Boolean are the wrapper classes in java
...


Command Line Arguments - Example
class Test
{
public static void main(String args[])
{
int x,y,z;
x=Integer
...
parseInt(args[1]);

z=x+y;
System
...
println("The sum="+z);
}
}

Taking input from user at Runtime (Scanner Class) - Example
import java
...
Scanner;
class Test
{
public static void main(String args[])
{
int x,y,z;
Scanner sc = new Scanner(System
...
out
...
nextInt();
System
...
println("Enter the second no:");
y = sc
...
out
...
out
...
while Loop
Syntax:
do
{
Statement(s) to be executed;
}
while (expression);
for Loop:
for (initialization; test condition; iteration statement)
{
Statement(s) to be executed if test condition is true
}

Arrays
Array is an object which contains elements of a similar data type
...

It is a data structure where we store similar elements
...

Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element
is stored on 1st index and so on
...

Syntax to Declare Multidimensional Array in Java
dataType[][]

arrayRefVar; (or)

dataType

[][]arrayRefVar; (or)

dataType

arrayRefVar[][]; (or)

dataType []arrayRefVar[];

Example to instantiate Multidimensional Array in Java
int[][] arr = new int[3][3];
Example-1:
class Test
{
public static void main(String args[])
{
int

arr[][] = { {1,2,3}, {2,4,5}, {4,4,5} };

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System
...
print(arr[i][j]+" ");
}
System
...
println();
}
}
}

Jagged Array in Java
If we are creating odd number of columns in a 2D array, it is known as a jagged array
...

Example:
class Test
{
public static void main(String[] args)
{
int

arr[][] = new

int[3][];

arr[0] = new int[3];
arr[1] = new int[4];
arr[2] = new int[2];
int count = 0;
for (int i=0; i ...
length; j++)
arr[i][j] = count++;
for (int i=0; i ...
length; j++)
{
System
...
print(arr[i][j]+" ");
}
System
...
println();//new line
}
}
}

Matrix Addition – Example
class AddMatrix {
public static void main(String args[])
{
int row, col,i,j;
Scanner in = new Scanner(System
...
out
...
nextInt();
System
...
println("Enter the number columns");
col = in
...
out
...
nextInt();
}
System
...
println("Enter the elements of matrix2");
for ( i= 0 ; i < row ; i++ )
{
for ( j= 0 ; j < col ;j++ )
mat2[i][j] = in
...
out
...
out
...
out
...
out
...
out
...
init(1,"akbar");
Student s2 = new Student();
s2
...
display();
s2
...
out
...
out
...
init(1,"xxx");
s[0]
...
init(2,"yyy");
s[1]
...
init(3,"zzz");
s[2]
...

Method Overloading: Defining multiple methods with the same name is called method
overloading
...
By varying no
...

2
...

3
...

Note: we can’t overload two methods by varying only return types
...
out
...
out
...
out
...
out
...
sum(10,20);
Test
...
sum(10
...
5);
Test
...
5f,20
...

Example:
class Student
{
private int sno;
private String sname;
public void

init(int

sno, String sname)

{
this
...
sname = sname;
}
public void display()
{
System
...
println(sno);
System
...
println(sname);
}
}
class Test
{
public static void main(String args[])
{
Student s1 = new Student();
s1
...
display();
}
}

Constructors
Constructor is a special method(member function) which is automatically called every time when
a new object of the class is created
...

Constructor should not have return type
...

Constructors can be overloaded
...
The default no-argument constructor
...


Default Constructor / Parameterized Constructor / Constructor overloading
class Sample
{
int x,y,z;
Sample()
{
x=0;
y=0;
z=0;
}
Sample(int x, int y, int z)
{
this
...
y=y;
this
...
out
...
out
...
out
...
display();
Sample s2 = new Sample();
s2
...
display();
}
}

this():
used to call one constructor from the other of the same class
...
out
...
out
...
In other
words, it is a way to destroy the unused objects
...

It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra
efforts
...

finalize() belongs to Object class
...

Example:
class Test
{
public static void main(String[] args)
{
Test t1 = new Test();
obj = null;
System
...
out
...
out
...


Strings
String basically represents sequence of char values
...
The java
...
String class is
used to create a string object
...
Whenever we change any
string, a new instance is created
...
String s1=”Shiva”; // string literal created on the string pool
2
...
charAt(int index):
Returns char value for the given index
...
out
...
charAt(0)); // a
2
...
out
...
length()); // 7
3
...

Example:
System
...
println("abc"
...
substring():
looks for characters in a string
...

Example:
String string = "animals";
System
...
println(string
...
indexOf( ):
looks at the characters in the string and finds the first index that matches the desired value
...

It can also start from a requested position
...
out
...
indexOf("al")); // 4

System
...
println(string
...
contains():
returns true or false after matching the sequence of char value
...
out
...
contains("b")); // true
7
...
out
...
startsWith("a")); // true
System
...
println("abc"
...
out
...
endsWith("c")); // true
System
...
println("abc"
...
replace():
The replace() method does a simple search and replace on the string
Example:
System
...
println("abcabc"
...
out
...
replace("a", "A")); // AbcAbc
9
...

Example: System
...
println("Abc123"
...
toUpperCase(): returns a string in uppercase
...
out
...
toUpperCase()); // ANIMALS
11
...

Example:
System
...
println("abc"
...
out
...
trim()); // a b c

Inheritance
➢ Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object
...

➢ The idea behind inheritance in Java is that you can create new classes that are built upon
existing classes
...
Moreover, you can add new methods and fields in your current class also
...


Types of Inheritance:
1
...

2
...

3
...

Multiple and hybrid inheritance is supported through interface only
...
Hierarchical: One base class is used to create many sub classes
...
Hybrid: uses more than on type of inheritance
...
sno=sno;
this
...
out
...
out
...


this
...
java=java;
this
...
out
...
out
...
out
...
display();
}
}

super keyword / Method Overriding:
Method overriding: Redefining the method of the base class in sub class is called as method
overriding
...
r
...

super:
1
...

2
...

3
...

Example:
class Student
{
int sno;
String sname;
Student(int sno,String sname)
{
this
...
sname=sname;
}
void display()
{
System
...
println(sno);
System
...
println(sname);
}
}

class BtechStudent extends Student
{
int ds,co,java;
BtechStudent(int sno,String sname,int ds,int co, int java)
{
super(sno,sname);

// calls the base class constructor

this
...
co=co;
this
...

void

display()

{
super
...


System
...
println(ds);
System
...
println(java);
System
...
println(co);
}
}
class Test
{
public static void main(String args[])
{
BtechStudent b1 = new BtechStudent(1,"shiva",77,88,99);
b1
...
out
...
");
}
}
class Dog extends Animal
{
void bark()
{
System
...
println("barking
...
out
...
");
}
}
class Test
{
public static void main(String args[])
{
BabyDog
d
...
bark();
d
...

In java Multiple and Hybrid inheritance is supported through interfaces only
...

Diamond Problem:
class A
{
int

a = 10;

};
class B extends A
{
B()
{
a = 20;
}
};
class C extends A
{
C()
{
a = 30;
}
};

Let “ D “ be the class extends B and C classes
...

So now when we try to access the variable ‘a’ in class D, we get the ambiguity because it could
not figure out which of the two copies of ‘a’ to be used is not known
...
out
...
getRateOfInterest() );
b = new ICICI();
System
...
println( b
...
out
...
getRateOfInterest() );
}
}

abstract Class / abstract Method
abstract Method: The method which do not have implementation is called abstract method
...

abstract class: The class which has at least one abstract method is called abstract class
...
out
...
");
return dim1*dim2;
}
}
class Triangle extends Figure
{
Triangle(int x, int y)
{
super(x,y);
}
double area()
{
System
...
println("from triangle class
...
area();
System
...
println("AREA=" + a);
ref1 = new Triangle(10,4);
a = ref1
...
out
...

Example:
final int x=10; // x value is 10 fixed, cant change
final methods:
Class Test
{
final

float sum(int x, int y)

{
System
...
println(x+y);

}
}
The method sum() cant be overridden in sub classes if any for the class ‘Test’
...


Forms of Inheritance:
The substitutability means that when a child class acquires properties from its parent class, the
object of the parent class may be substituted with the child class object
...

The substitutability can achieve using inheritance, whether using extends or implements
keywords
...

Child class is a specialized form of parent class
...

Example:
Java AWT Components
TextComponent (Parent class)
TextArea (Derived class)
TextField (Derived class)
Specification
Two different mechanisms are provided by Java, interface and abstract, to make use of
subclassification for specification
...
Child class
implements the behavior
...

Mostly, not used for refinement of its parent class, but instead is used for definitions of the
properties provided by its parent
...
1 Event Listeners: ActionListener, MouseListener
Construction
Child class inherits most of its functionality from parent, but may change the name or parameters
of methods inherited from parent class to form its interface
...
It simplifies the
construction of newly formed abstraction
...

Example: Stack class defined in Java libraries
Extension(Generalization)
The child class generalizes or extends the parent class by providing more functionality
The child doesn't change anything inherited from the parent, it simply adds new features
Example, ColoredWindow inheriting from Window
Limitation
The child class limits some of the behavior of the parent class
...
Is not a subtype, and substitutability is not proper
...

Although the Java does not permit a subclass to be formed be inheritance from more than one
parent class, several approximations to the concept are possible
...
The child class may use the code defined in the parent
class without re-writing it
...

➢ Inheritance provides a clear model structure which is easy to understand
...

➢ With inheritance, we will be able to override the methods of the base class so that the
meaningful implementation of the base class method can be designed in the derived class
...


Costs of Inheritance
➢ Inheritance decreases the execution speed due to the increased time and effort it takes, the
program to jump through all the levels of overloaded classes
...
This
means one cannot be used independently of each other
...

➢ The overuse of inheritance makes the program more complex
...

Methods of Object Class:
hashCode(): Returns the hashcode number for this object
...

clone(): creates and returns the exact copy (clone) of this object
...

notify(): wakes up single thread, waiting on this object's monitor
...

finalize(): is invoked by the garbage collector before object is being garbage collected
Title: JAVA OOPS COMPLETE CONCEPT
Description: This notes will be useul for all the students who want to learn java in easy manner its unti_1 form jnuth university if u are interested other topics i can share u... but this copy is very rare u cannot more simpler than this notes..