0% found this document useful (0 votes)
2 views

5 oop

The document provides an overview of Java as an object-oriented programming language, detailing its principles such as encapsulation, abstraction, inheritance, and polymorphism. It explains key concepts like classes, objects, and variable types (instance, class, and local variables), along with their memory allocation and access methods. Additionally, it emphasizes the importance of understanding variable scope and usage in Java programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

5 oop

The document provides an overview of Java as an object-oriented programming language, detailing its principles such as encapsulation, abstraction, inheritance, and polymorphism. It explains key concepts like classes, objects, and variable types (instance, class, and local variables), along with their memory allocation and access methods. Additionally, it emphasizes the importance of understanding variable scope and usage in Java programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Java By Venkatesh Mansani Naresh i Technologies

Object Oriented Programming (OOP)


Java is an object oriented programming language (OOPL).

OOPL:
A language that supports all the principles of an object oriented programming is
known as an object oriented programming language.

Object Oriented Principles(OOPs):


1) Encapsulation
2) Abstraction
3) Inheritance
4) Polymorphism
Object Oriented = Object Based + Inheritance + Runtime Polymorphism

Object Oriented Languages:


C++, Java, VC++, C#.Net, Visual Basic.Net, Simula, Ada, Small Talk, Python, .. etc.,

Object Based Languages:


Java Script, VB Script, Visual Basic, .. etc.,
In order to use the above principles in a Java programming we need the following
language constructs:
1) Class
2) Object

Class:
A class is a collection of variables & methods.

The syntax of a class:


class ClassName
{
DataType Variable1;

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

DataType Variable2;
================
ReturnType MethodName1(arg1, arg2, … )
{
==================
}
}
Class will not occupy memory where as file occupies memory.

Object:
Object is an instance of a class.

The syntax to create an object:


ClassName ObjectReference=new Constructor();

new:
It is called as dynamic memory allocation operator and it allocates the memory to
instance variables at run time.
Object occupies memory and object reference also occupies memory.
Object contains data whereas an object reference contains hash code.

Anonymous Object:
Syntax: new Constructor();
Example: new Demo();
It is also called as unreferenced object.
Object reference can be created without object also. That object reference
contains null.
null is an object literal.

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

Variables:
A variable is a container that contains data.
There are 3 types of variables in Java:
1) Instance Variables
2) Class Variables
3) Local Variables

1) Instance Variables:
A variable that is defined as a member of a class is known as an instance variable.
Memory allocated to instance variables whenever an object is created.
Instance variables are stored in heap area.

2) Class Variables:
A variable that is defined as a static member of a class is known as class variable.
Memory allocated to class variables whenever class is loaded.
Class variables are stored in method area.

3) Local Variables:
A variable that is defined inside a method is known as local variable.
Memory allocated to local variables whenever method is called.
Local variables are stored in stack area.
Note1: Local variables cannot be static in Java.
Note2: No global variables in Java (Outside the class).

Execution Priority:
1) Class Variables
2) Static Blocks
3) Main Method

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

Class can also be called as reference data type & Object reference can also be
called as reference variable.
Primitive type variable contains data whereas reference type variable contains
hash code.
Separate copy of instance variable exists for every object whereas only one copy
of class variable exists for all objects.

Example:
class Demo
{
int x=10;
static int y=20;
public static void main(String args[])
{
int z=30;
Demo d1=new Demo();
Demo d2=new Demo();
}
}

There are two ways to access an instance variable:


1) By using object
2) By using object reference
Use object to access an instance variable if it is required only one time.
Use object reference to access an instance variable if it is required more than one
time.

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

There are four ways to access class variable:


1) Directly
2) By using class name
3) By using object
4) By using object reference
3rd & 4th ways are not recommended to use.
Access class variable directly if it is present in the same class.
Use class name to access class variable if it is present in another class.

There is a only one way to access local variable: i.e. directly.


Instance Variable Example:
class Demo
{
int x=10;
public static void main(String args[])
{
System.out.println(new Demo().x);
Demo d=new Demo();
System.out.println(d.x);
}
}

Class Variable Example:


class Demo
{
static int x=10;
public static void main(String args[])

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

{
System.out.println(x);
System.out.println(Demo.x);
System.out.println(new Demo().x);
Demo d=new Demo();
System.out.println(d.x);
}
}

Local Variable Example:


class Demo
{
public static void main(String args[])
{
int x=10;
System.out.println(x);
}
}
Note: Local variables must be initialized before access otherwise compile time
error occurs because they do not get default values.
class Demo
{
int x=10;
int y=20;
public static void main(String args[])
{

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

int z=10;
for(int i=1;i<=10;i++)
{
=============
}
}
}
In the above example x is an instance variable, y is a class variable, z is a local
variable, args is a method parameter & i is a block variable
Instance variables & Class variables scope is based on access modifiers.
Method parameters & local variables scope is limited to method only.
Block variables scope is limited to block only.
Note: Method parameters & block variables are also called as local variables.
Use instance variable if the value is different for objects.
Use class variable if the value is same for all objects.
Use local variable to perform the task.

By

Mr. Venatesh Mansani


Naresh i Technologies

[email protected] Naresh i Technologies

You might also like