• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

OOPS In Java

OOPS:
'OOPS' means object oriented programming structure.It can be achieved by
1.Security
2.Reusability
3.Memory Management

OOPS Principles:
1.Encapsulation
2.Data abstraction
3.Inheritance
4.Polymorphism

By using these principles we can achieve above 3 properties is called OOPS.
Based on the OOPS principles programming languages are classified into 2 types

1.Object oriented programming language(OOPL)
2.Object based programming language(OBPL)

Object oriented programming language(OOPL):
If any language supports all the oops principles is known as Object oriented programming language(OOPL).
Example: C++ , Java , .NET

Object based programming language(OBPL):
If any language supports all the oops principles except Inheritance and dynamic Polymorphism is known as Object based programming language(OBPL).
Example: JavaScript , VBScript

Object: Object is as realtime entity, which is existing physically with some specific properties.
1.Identity
2.Attributes
3.Behavior

Examples of object is Pen , Book, TV, Remote, Water Bottle etc.
In Java:
class Addition
{
int x,y,z;
void add()
{
..
..
}
}
In the above example
'Addition' is as Identity.
int x,y,z are a Attributes.
void add() is a Behavior.



Encapsulation

Binding of attributes and behaviors in a single container .In java language it can be achieved by 'class' keyword.
class:
'class' is container which is collection of variable and methods (or) it is a blueprint of the object.
Syntax:

class ClassName
{
list of variables;
..
..
list of methods()
{
..
..
}
..
..
}
in the above syntax class is keyword.
ClassName any userdefined name.




Variable:
Variable is an identifer whose value can can be changed.
Example:
int x=10;
x=20;

In java language these variable are classified into following types

1.Local variable
2.Global variable
3.Formal variable
4.Block variable

Local variable:
If any variable declare with in method or constructor.
Global variable
If any variable declare with in class or outside of the method or constructor.
Formal variable
If any variable declare with in method signature or constructor signature.
Block variable
If any variable declare with in block(if block ,for block ete..)

class Demo
{
int x; --> Global Variable
void show(int y) --> formal Varaible
{
int z; -->Local Variable
if()
{
int p; --> Block Variable
}
}
}

Global and Local Variable Program
Image

class LocalGlobal
{
int x; //Global Varaible
void show()
{
int y=10;//Local Variable
x=100;
System.out.println("x value is "+x);
System.out.println("y value is "+y);
}

void disp()
{
int z=20;//Local Variable
x=200;
System.out.println("x value is "+x);
//System.out.println("y value is "+y);
System.out.println("z value is "+z);
}
}

class Main
{
public static void main(String args[])
{
LocalGlobal d=new LocalGlobal();
d.show();
d.disp();
}
}

Output:
x value is 100
y value is 10
x value is 200
z value is 20
Method:

Method is a collection of statements used to perform specific operation.

Syntax:

accessmodifiers returntype methodname(list of parameters/no parameters)
{
..
..
..
}

In the above syntax access modifiers are used to change the current properties of method or constructor.In java language it is optional.

returntype:
These are classified into two types.
1.Noreturn type (means void)
2.Return type(means any primitive data type)
Method name any userdefined name.
In java language these methods are classisfied into following types
1.Noparametarized method
2.Parametarized Method

Noparametarized method: If any method signature does not contain parameters is known as Noparametarized method
Syntax:

accessmodifiers returntype methodname()
{
..
..
..
}

Parametarized Method
If any method signature contains List of parameters is known as parametarized method
Syntax:

accessmodifiers returntype methodname(datatype variable1,datatype variable2,....)
{
..
..
..
}


Based on the return type these methods are classified into following types
1.No return type with no Parametarized
2.No return type with Parametarized
3.return type with no Parametarized
4.return type with Parametarized

1. MaxNumber Program Using No return type with No Parametarized

class Maxnum
{
void max()
{
int x=10,y=20;
if(x>y)
{
System.out.println("x is Max");
}
else
{
System.out.println("y is Max");
}
}
}
class Main
{
public static void main(String args[])
{
Maxnum m=new Maxnum();
m.max();
}
}

Output:
y is Max

import java.util.*;
class Maxnum
{
void max()
{
int x,y;
Scanner s=new Scanner(System.in);
System.out.println("Enter x ,y values");
x=s.nextInt();
y=s.nextInt();
if(x>y)
{
System.out.println("x is Max");
}
else
{
System.out.println("y is Max");
}
}
}
class Main
{
public static void main(String args[])
{
Maxnum m=new Maxnum();
m.max();
}
}

Output:
Enter x ,y values
23 45
y is Max
2. MaxNumber Program Using No return type with Parametarized

class Maxnum
{
void max(int x,int y)
{
if(x>y)
{
System.out.println("x is Max");
}
else
{
System.out.println("y is Max");
}
}
}
class Main
{
public static void main(String args[])
{
Maxnum m=new Maxnum();
m.max(10,20);
}
}

Output:
y is Max

import java.util.*;
class Maxnum
{
void max(int x,int y)
{
if(x>y)
{
System.out.println("x is Max");
}
else
{
System.out.println("y is Max");
}
}
}
class Main
{
public static void main(String args[])
{
Maxnum m=new Maxnum();
int a,b;
Scanner s=new Scanner(System.in);
System.out.println("Enter a ,b values");
a=s.nextInt();
b=s.nextInt();
m.max(a,b);
}
}

Output:
Enter x ,y values
23 45
y is Max
3. MaxNumber Program Using return type with No Parametarized

class Maxnum
{
int max()
{
int x=10,y=13;
if(x>y)
{
return x;
}
else
{
return y;
}
}
}
class Main
{
public static void main(String args[])
{
Maxnum m=new Maxnum();
int p=m.max();
System.out.println(p);
}
}

Output:
y is Max

import java.util.*;
class Maxnum
{
int max()
{
int x,y;
Scanner s=new Scanner(System.in);
System.out.println("Enter x ,y values");
x=s.nextInt();
y=s.nextInt();
if(x>y)
{
return x;
}
else
{
return y;
}
}
}
class Main
{
public static void main(String args[])
{
Maxnum m=new Maxnum();
int p=m.max();
System.out.println(p);
}
}

Output:
Enter x ,y values
23 45
y is Max
4. MaxNumber Program Using return type with Parametarized

class Maxnum
{
int max(int x,int y)
{
if(x>y)
{
return x;
}
else
{
return y;
}
}
}
class Main
{
public static void main(String args[])
{
Maxnum m=new Maxnum();
int p=m.max(10,20);
System.out.println(p);
}
}

Output:
20

import java.util.*;
class Maxnum
{
int max(int x,int y)
{
if(x>y)
{
return x;
}
else
{
return y;
}
}
}
class Main
{
public static void main(String args[])
{
Maxnum m=new Maxnum();
int a,b;
Scanner s=new Scanner(System.in);
System.out.println("Enter a ,b values");
a=s.nextInt();
b=s.nextInt();
int p=m.max(a,b);
System.out.println(p);
}
}

Output:
Enter x ,y values
23 45
45
3. MaxNumber Program Using return type with No Parametarized (String return)

class Maxnum
{
String max()
{
int x=10,y=13;
if(x>y)
{
return "x is max";
}
else
{
return "y is max";
}
}
}
class Main
{
public static void main(String args[])
{
Maxnum m=new Maxnum();
String p=m.max();
System.out.println(p);
}
}

Output:
y is Max

import java.util.*;
class Maxnum
{
String max()
{
int x,y;
Scanner s=new Scanner(System.in);
System.out.println("Enter x ,y values");
x=s.nextInt();
y=s.nextInt();
if(x>y)
{
return "x is max";
}
else
{
return "y is max";
}
}
}
class Main
{
public static void main(String args[])
{
Maxnum m=new Maxnum();
String p=m.max();
System.out.println(p);
}
}

Output:
Enter x ,y values
23 45
y is Max
4. MaxNumber Program Using return type with Parametarized(String Return)

class Maxnum
{
String max(int x,int y)
{
if(x>y)
{
return "x is max";
}
else
{
return "y is max";
}
}
}
class Main
{
public static void main(String args[])
{
Maxnum m=new Maxnum();
String p=m.max(10,20);
System.out.println(p);
}
}

Output:
y is max

import java.util.*;
class Maxnum
{
String max(int x,int y)
{
if(x>y)
{
return "x is max";
}
else
{
return "y is max";
}
}
}
class Main
{
public static void main(String args[])
{
Maxnum m=new Maxnum();
int a,b;
Scanner s=new Scanner(System.in);
System.out.println("Enter a ,b values");
a=s.nextInt();
b=s.nextInt();
String p=m.max(a,b);
System.out.println(p);
}
}

Output:
Enter x ,y values
23 45
y is max
Factorial Progrram Using Method 4 Ways

Program-1:
1.No return type with no Parametarized
Program-2:
2.No return type with Parametarized
Program-3:
3.return type with no Parametarized
Program-4:
4.return type with Parametarized

Factorial Program Using No return type with No Parametarized

import java.util.*;
class Factorial
{
void fact()
{
int x,f=1,n;
Scanner s=new Scanner(System.in);
System.out.println("Enter n Value");
n=s.nextInt();
for(x=1;x<=n;x++)
{
f=f*x;
}
System.out.println(f);
}
}
public class Main {
public static void main(String[] args) {
Factorial f=new Factorial();
f.fact();
}
}

Output:
Enter n Value
5
120

Factorial Program Using No return type with Parametarized

import java.util.*;
class Factorial
{
void fact(int n)
{
int x,f=1;
for(x=1;x<=n;x++)
{
f=f*x;
}
System.out.println(f);
}
}
public class Main {
public static void main(String[] args) {
Factorial f=new Factorial();
int n;
Scanner s=new Scanner(System.in);
System.out.println("Enter n Value");
n=s.nextInt();
f.fact(n);
} }

Output:
Enter n Value
5
120

Factorial Program Using return type with No Parametarized

import java.util.*;
class Factorial
{
int fact()
{
int x,f=1,n;
Scanner s=new Scanner(System.in);
System.out.println("Enter n Value");
n=s.nextInt();
for(x=1;x<=n;x++)
{
f=f*x;
}
return f;
}
}
public class Main {
public static void main(String[] args) {
Factorial f=new Factorial();
int a=f.fact();
System.out.print(a);
}
}

Output:
Enter n Value
5
120

Factorial Program Using return type with Parametarized

import java.util.*;
class Factorial
{
int fact(int n)
{
int x,f=1;
for(x=1;x<=n;x++)
{
f=f*x;
}
return f;
}
}
public class Main {
public static void main(String[] args) {
Factorial f=new Factorial();
int n;
Scanner s=new Scanner(System.in);
System.out.println("Enter n Value");
n=s.nextInt();
int a=f.fact(n);
System.out.print(a);
} }

Output:
Enter n Value
5
120