JAVA OOPs Concepts
OOP:- Object Oriented Programming is the technique to create programs based on real world
Class:- A class defines the Properties and behavior ( Variables and methods) that is shared by all its objects. It is a blue print for the creation of objects
Object:- Object is the basic entity of object oriented programming language. Object is a n instance of class. It takes the properties(Variables) and uses the behavior (Methods) defined in the class.
Encapsulation, Inheritance and Polymorphism are main pillars of OOPs.
Encapsulation:- Encapsulation is the process of binding together the methods and data variables as a single entity. This keeps both the data and functionality code safe from outside world. It hides the data within the class as makes it available only through the methods.
Java provides different accessibility scopes (Public, Protected, Private, default) to hide the data from outside.
EXAMPLE:-
class check {
private int amount = 0;
public int getAmount()
{
return amount;
}
public void setAmount(int amt)
{
amount = amt;
}
}
public class Mainclass
{
public static void main(String[] arg)
{
int amt=0;
check obj = new check();
obj.setAmount(200);
amt = getAmount();
System.out.println(amt);
}
}
Here the data variable "amount" and methods setAmount() and getAmount() are enclosed together with a single entity called the "check" class.
INHERITANCE:-
Inheritance allow a class (subclass) to acquire the properties and behavior of another class (Super Class). In Java a class can inherit only one class (super class) at a time but a class can have any number of sub classes.
It help to reuse, customize and enhance the existing code. Java uses extends keyword to extend a class.
EXAMPLE:-
class A
{
public void fun1(int x)
{
System.out.println("int in A");
}
}
class B extends A
{
public void fun2(int x, int y)
fun1(6);
System.out.println("int in B");
}
}
public class C
{
public static void main(String[] arg)
{
B obj = new B();
obj.fun2(2,3);
}
}
In above example class B extends class A and so acquire properties and behavior of class A. So we can call method of A in class B.
Class:- A class defines the Properties and behavior ( Variables and methods) that is shared by all its objects. It is a blue print for the creation of objects
Object:- Object is the basic entity of object oriented programming language. Object is a n instance of class. It takes the properties(Variables) and uses the behavior (Methods) defined in the class.
Encapsulation, Inheritance and Polymorphism are main pillars of OOPs.
Encapsulation:- Encapsulation is the process of binding together the methods and data variables as a single entity. This keeps both the data and functionality code safe from outside world. It hides the data within the class as makes it available only through the methods.
Java provides different accessibility scopes (Public, Protected, Private, default) to hide the data from outside.
EXAMPLE:-
class check {
private int amount = 0;
public int getAmount()
{
return amount;
}
public void setAmount(int amt)
{
amount = amt;
}
}
public class Mainclass
{
public static void main(String[] arg)
{
int amt=0;
check obj = new check();
obj.setAmount(200);
amt = getAmount();
System.out.println(amt);
}
}
Here the data variable "amount" and methods setAmount() and getAmount() are enclosed together with a single entity called the "check" class.
INHERITANCE:-
Inheritance allow a class (subclass) to acquire the properties and behavior of another class (Super Class). In Java a class can inherit only one class (super class) at a time but a class can have any number of sub classes.
It help to reuse, customize and enhance the existing code. Java uses extends keyword to extend a class.
EXAMPLE:-
class A
{
public void fun1(int x)
{
System.out.println("int in A");
}
}
class B extends A
{
public void fun2(int x, int y)
fun1(6);
System.out.println("int in B");
}
}
public class C
{
public static void main(String[] arg)
{
B obj = new B();
obj.fun2(2,3);
}
}
In above example class B extends class A and so acquire properties and behavior of class A. So we can call method of A in class B.
Comments