Three principle of OOP
1. Encapsulation
2. Inheritance
3. Polymorphism
Encapsulation:
Encapsulation in JAVA is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation the variables of a class will be hidden from other classes and can be accessed only through the methods of their current class, therefore it is also known as data hiding.
Example:
public class EncapTest{
private String name;
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
}
public class Main{
public static void main(String args[]){
EncapTest ob = new EncapTest();
ob.setName("IMTIYAZ");
System.out.println(ob.getName()):
}
}
Inheritance:
Inheritance is the process by which one object acquires the properties of another object by use of inheritance, an object would need only to define those qualities that make it unique within its class. It can inherit its general attributes from its parent. Thus it is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case.
Example:
public class calculation{
int l;
public void add(int h, int r)
{
l = h+r;
System.out.println(l);
}
}
public class my_calculation extends calculation{
public void sub(int x,int y){
l = x-y;
System.out.println(l);
}
}
public class InheritTest{
public static void main(String args[]){
int a=20,b=10;
my_calculation ob = new my_calculation();
ob.add(a,b);
ob.sub(a,b);
}
}
Polymorphism:
Polymorphism is a feature that allows one interface to be used for a general class of actions. the specific action is determined by the exact nature of the situation.
Example:
public class bike{
void run(){
System.out.println("Running");
}
}
public class splender extends bike{
void run(){
System.out.println("Running safely with 60km");
}
}
public class Test{
public static void main(String args[]){
bike b = new splender(); //upcasting
b.run():
}
}
Output : : Running safely with 60km
}
Inheritance:
Inheritance is the process by which one object acquires the properties of another object by use of inheritance, an object would need only to define those qualities that make it unique within its class. It can inherit its general attributes from its parent. Thus it is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case.
Example:
public class calculation{
int l;
public void add(int h, int r)
{
l = h+r;
System.out.println(l);
}
}
public class my_calculation extends calculation{
public void sub(int x,int y){
l = x-y;
System.out.println(l);
}
}
public class InheritTest{
public static void main(String args[]){
int a=20,b=10;
my_calculation ob = new my_calculation();
ob.add(a,b);
ob.sub(a,b);
}
}
Polymorphism:
Polymorphism is a feature that allows one interface to be used for a general class of actions. the specific action is determined by the exact nature of the situation.
Example:
public class bike{
void run(){
System.out.println("Running");
}
}
public class splender extends bike{
void run(){
System.out.println("Running safely with 60km");
}
}
public class Test{
public static void main(String args[]){
bike b = new splender(); //upcasting
b.run():
}
}
Output : : Running safely with 60km
Well done!
ReplyDeleteMy pleasure
ReplyDeleteShortly a good description of OOP.
ReplyDelete