![]() |
Java Oop|Data Abstraction|Encapsulation With Examples |
Java Oop|Data Abstraction|Encapsulation With Examples
Data Abstraction: A Abstraction means Data hiding in other words.
you can say that in this type of programming essential data is shown to the user or outside class and unessential data is hidden.
Members defined with a public access specifier are accessible throughout the program.
C File Handling
Members defined with a private access specifier are not accessible throughout the program like a private element of a class can be accessed only inside in its own class.
Real-Life Example: Let's assume that you are going to buy a car in a showroom, then you can know about the company name, model name, color, cost, and oil type.
but you don't know about piston and its functionality, the person who made that model of car.
How To Use Data Abstraction in Java?
class Showroom
{
public void company()
{
System.out.println("Car Company Name Renault");
}
public void model()
{
System.out.println("Car Model Duster");
}
public void color()
{
System.out.println("Car Color White/Gray/Silver");
}
public void oilType()
{
System.out.println("Car Oil Type Petrol");
}
public void price()
{
System.out.println("Car Price 800,000 to 1400,000");
}
private void piston()
{
System.out.println("4 piston");
}
private void person_who_made()
{
System.out.println("Alexo Remon");
}
}
class icoderweb
{
public static void main(String[] args)
{
obj=new Showroom();
obj.company();
obj.model();
obj.color();
obj.price();
obj.oilType();
}
}
*****OUTPUT*****
Car Company name Renault
Car Model Duster
Car Color White/Gray/Silver
Car Price 400,000 to 800,000
Car Oil Type Petrol
{
public void company()
{
System.out.println("Car Company Name Renault");
}
public void model()
{
System.out.println("Car Model Duster");
}
public void color()
{
System.out.println("Car Color White/Gray/Silver");
}
public void oilType()
{
System.out.println("Car Oil Type Petrol");
}
public void price()
{
System.out.println("Car Price 800,000 to 1400,000");
}
private void piston()
{
System.out.println("4 piston");
}
private void person_who_made()
{
System.out.println("Alexo Remon");
}
}
class icoderweb
{
public static void main(String[] args)
{
obj=new Showroom();
obj.company();
obj.model();
obj.color();
obj.price();
obj.oilType();
}
}
*****OUTPUT*****
Car Company name Renault
Car Model Duster
Car Color White/Gray/Silver
Car Price 400,000 to 800,000
Car Oil Type Petrol
Advantage of Data Abstraction
2. Data Abstraction avoids code duplication and increases code reusability.
3. You don't have to write the low-level code because a private element of a class can not be accessed outside that class.
Encapsulation: A Encapsulation process of combining many elements into a single entity is called Encapsulation.
or
In the field of the programming language, the process of combining data member and member function into a single entity-like class is called Data encapsulation.
It is an important feature of object-oriented programming.
It is used to prevent the direct accessibility of data member and member function and this is done by using access specifier public, private, and protected.
Access Specifier: It is a keyword that is used to provide accessibility of data member variable and member function of a class. It is also called an access modifier.
Types of Access Specifier
1.Public
2.Private
3.Protected
public: A public specifier allows the accessibility of data member and member function to the other classes. a public element of a class can be accessed anywhere in the program.
private: Private access specifier can be used to hide data member and member function from the other classes. a private element of a class can be accessed only inside its own class. The private element of a class can not be accessed out of that class.
protected: Protected access specifier is approximately the same as private but it allows the accessibility of data member and member function to the child class. protected is used in the case of inheritance.
How To Use Encapsulation in Java?import java.util.Scanner;
class SInterest
{
Scanner in=new Scanner(System.in);
float a;
float r;
float t;
float si;
void getPara()
{
System.out.print("Enter Initial Amount:");
a=in.nextFloat();
System.out.print("Enter Rate of Interest:");
r=in.nextFloat();
System.out.print("Enter Time Duration:");
t=in.nextFloat();
}
void findInterest()
{
si=(a*r*t)/100;
}
void show()
{
System.out.println("Simple Interest:"+si);
}
public static void main(String[] args)
{
SInterest obj=new SInterest();
obj.getPara();
obj.findInterest();
obj.show();
}
}
*****OUTPUT*****
Enter initial Amout:1500
Enter Rate of Interest:5.5
Enter Time Duration:8
Simple Interest=660.0
class SInterest
{
Scanner in=new Scanner(System.in);
float a;
float r;
float t;
float si;
void getPara()
{
System.out.print("Enter Initial Amount:");
a=in.nextFloat();
System.out.print("Enter Rate of Interest:");
r=in.nextFloat();
System.out.print("Enter Time Duration:");
t=in.nextFloat();
}
void findInterest()
{
si=(a*r*t)/100;
}
void show()
{
System.out.println("Simple Interest:"+si);
}
public static void main(String[] args)
{
SInterest obj=new SInterest();
obj.getPara();
obj.findInterest();
obj.show();
}
}
*****OUTPUT*****
Enter initial Amout:1500
Enter Rate of Interest:5.5
Enter Time Duration:8
Simple Interest=660.0
Inheritance: A process of getting the property of one class into another class is called Inheritance.
The other word we can say that the process of deriving a new class from an old class is called inheritance.
which the new class is called derived or child or subclass and old class are called Base or Parent or Superclass.
When a class inherits the property of a class it means it can access all the data member and member functions of that class except the private element.
Python File Handling
1.Parent/Super/Base class2.Child/Sub/Derived class
Parent/Super/Base class: The class which is inherited by another class is called Parent or Super or Base class.
Child/Sub/Derived class: The class which inherits the property of another class is called Child or Sub or Derived class.
Syntax(Derived class extends Base class)
class Subtraction extends Addition
Here Subtraction is a Derived class and Addition is a Base class and extension is a keyword that is used to inherit one class into another.
class Addition
{
void add()
{
int x=100;
int y=200;
int z=x+y;
System.out.println("Addition:"+z);
}
}
class Subtraction extends Addition
{
void sub()
{
int x=300;
int y=200;
int z=x-y;
System.out.println("Subtraction:"+z);
}
}
class icoderweb
{
public static void main(String[] args)
{
Subtraction obj=new Subtraction();
obj.add();
obj.sub();
}
}
*****OUTPUT*****
Addition:300
Subtraction:100
{
void add()
{
int x=100;
int y=200;
int z=x+y;
System.out.println("Addition:"+z);
}
}
class Subtraction extends Addition
{
void sub()
{
int x=300;
int y=200;
int z=x-y;
System.out.println("Subtraction:"+z);
}
}
class icoderweb
{
public static void main(String[] args)
{
Subtraction obj=new Subtraction();
obj.add();
obj.sub();
}
}
*****OUTPUT*****
Addition:300
Subtraction:100
1.Single Inheritance
2.Multiple Inheritance
3.Multilevel Inheritance
4.Hierarchical Inheritance
5.Hybrid Inheritance
Single Inheritance: In a single type of inheritance only two classes are used in which one is inherited by another class.
How To Use Single Inheritance in Java?class Addition
{
void add()
{
int x=100;
int y=200;
int z=x+y;
System.out.println("Addition:"+z);
}
}
class Subtraction extends Addition
{
void sub()
{
int x=300;
int y=200;
int z=x-y;
System.out.println("Subtraction:"+z);
}
}
class icoderweb
{
public static void main(String[] args)
{
Subtraction obj=new Subtraction();
obj.add();
obj.sub();
}
}
*****OUTPUT*****
Addition:300
Subtraction:100
{
void add()
{
int x=100;
int y=200;
int z=x+y;
System.out.println("Addition:"+z);
}
}
class Subtraction extends Addition
{
void sub()
{
int x=300;
int y=200;
int z=x-y;
System.out.println("Subtraction:"+z);
}
}
class icoderweb
{
public static void main(String[] args)
{
Subtraction obj=new Subtraction();
obj.add();
obj.sub();
}
}
*****OUTPUT*****
Addition:300
Subtraction:100
In the above example, you can see that there are only two classes are used in which Addition is inherited by Subtraction therefore using object of subtraction we can call function to add() and sub().
Multiple Inheritance: A multiple Inheritance two or more than two classes are inherited by a single class simultaneously called multiple inheritances.
In another word, we can say that in this type of inheritance Base class maybe two or more than two but the derived class should be one. In this type of inheritance, at least three classes are compulsory.
Java does not support multiple inheritances therefore interface are used to implement multiple inheritances.
the interface is declared with the interface keyword and it is implemented by a class while the class is extended by a class.
we can not define a function inside an interface only can be declared.
How To Use Multiple Inheritance in java?
interface Addition
{
void add();
}
class Subtraction
{
void sub()
{
int x=300;
int y=200;
int z=x-y;
System.out.println("Subtraction:"+z);
}
}
class Multiplication extends Subtraction implements Addition
{
public void add
{
int x=300;
int y=200;
int z=x+y;
System.out.println("Addition:"+z);
}
void multi()
{
int x=20;
int y=20;
int z=x*y;
System.out.println("Multiplication:"+z);
}
}
class icoderweb
{
public static void main(String[] args)
{
Multiplication obj=new Multiplication();
obj.sub();
obj.add();
obj.multi();
}
}
*****OUTPUT*****
Subtraction:100
Addition:500
Multiplication:400
void add();
}
class Subtraction
{
void sub()
{
int x=300;
int y=200;
int z=x-y;
System.out.println("Subtraction:"+z);
}
}
class Multiplication extends Subtraction implements Addition
{
public void add
{
int x=300;
int y=200;
int z=x+y;
System.out.println("Addition:"+z);
}
void multi()
{
int x=20;
int y=20;
int z=x*y;
System.out.println("Multiplication:"+z);
}
}
class icoderweb
{
public static void main(String[] args)
{
Multiplication obj=new Multiplication();
obj.sub();
obj.add();
obj.multi();
}
}
*****OUTPUT*****
Subtraction:100
Addition:500
Multiplication:400
In the above example, you can see that there are two classes(Subtraction and Multiplication) and one interface (Addition) are used.
which Subtraction is extended and Addition is implemented by Multiplication, therefore, using object of Multiplication we can call function to add(), sub(), and multiply().
Multilevel Inheritance: A multilevel Inheritance first class is inherited by second class is inherited by third class and so on called multilevel inheritance.
In this type of inheritance, each derived class is the base class for the next class.
It can be used at least three classes are compulsory.
Java Class or Objects
class Addition
{
public void add()
{
int x=300;
int y=200;
int z=x+y;
System.out.println("Addition:"+z);
}
}
class Subtraction extends Addition
{
void sub()
{
int x=300;
int y=200;
int z=x-y;
System.out.println("Sub="+x);
}
}
class Multiplication extends Subtraction
{
void multi()
{
int x=20;
int y=20;
int z=x*y;
System.out.println("Multiplication:"+z);
}
}
class icoderweb
{
public static void main(String[] args)
{
Multiplication obj=new Multiplication();
obj.add();
obj.sub();
obj.multi();
}
}
*****OUTPUT*****
Addition:500
Subtraction:100
Multiplication:400
{
public void add()
{
int x=300;
int y=200;
int z=x+y;
System.out.println("Addition:"+z);
}
}
class Subtraction extends Addition
{
void sub()
{
int x=300;
int y=200;
int z=x-y;
System.out.println("Sub="+x);
}
}
class Multiplication extends Subtraction
{
void multi()
{
int x=20;
int y=20;
int z=x*y;
System.out.println("Multiplication:"+z);
}
}
class icoderweb
{
public static void main(String[] args)
{
Multiplication obj=new Multiplication();
obj.add();
obj.sub();
obj.multi();
}
}
*****OUTPUT*****
Addition:500
Subtraction:100
Multiplication:400
Hierarchical Inheritance: A single class is inherited by two or more than two classes simultaneously called hierarchical inheritance.
In another word, we can say that in this type of inheritance derived class maybe two or more than two but the Base class should be one.
It can be used at least three classes are compulsory.
class Addition
{
public void add()
{
int x=300;
int y=200;
int z=x+y;
System.out.println("Addition:"+z);
}
}
class Subtraction extends Addition
{
void sub()
{
int x=300;
int y=200;
int z=x-y;
System.out.println("Subtraction:"+z);
}
}
class Multiplication extends Addition
{
void multi()
{
int x=20;
int y=20;
int z=x*y;
System.out.println("Multiplication:"+z);
}
}
class icoderweb
{
public static void main(String[] args)
{
Multiplication obj=new Multiplication();
obj.add();
obj.multi();
}
}
*****OUTPUT*****
Addition:500
Multiplication:400
{
public void add()
{
int x=300;
int y=200;
int z=x+y;
System.out.println("Addition:"+z);
}
}
class Subtraction extends Addition
{
void sub()
{
int x=300;
int y=200;
int z=x-y;
System.out.println("Subtraction:"+z);
}
}
class Multiplication extends Addition
{
void multi()
{
int x=20;
int y=20;
int z=x*y;
System.out.println("Multiplication:"+z);
}
}
class icoderweb
{
public static void main(String[] args)
{
Multiplication obj=new Multiplication();
obj.add();
obj.multi();
}
}
Addition:500
Multiplication:400
In the above example, you can see that there are three classes(Addition, Subtraction, and Multiplication) that are used.
which Addition is inherited by Subtraction and Multiplication therefore using the object of Multiplication we can call the function only add() and multiply().
Because there is no relation between Subtraction and Multiplication, therefore, function sub() can not be called by an object of Multiplication.
Similarly, by using the object of class Subtraction we can call the only function to add() and sub().
Hybrid Inheritance: A Hybrid Inheritance combination of two or more than two inheritance is called Hybrid inheritance.
It can be a combination of any two or more than two inheritance(single, multiple, multilevel, hierarchical).
It can be used at least three classes are compulsory.
How To Use Hybrid Inheritance in java?
interface Addition
{
void add();
}
class Subtraction
{
void sub()
{
int x=300;
int y=200;
int z=x-y;
System.out.println("Subtraction:"+z);
}
}
class Multiplication extends Subtraction implements Addition
{
public void add()
{
int x=300;
int y=200;
int z=x+y;
System.out.println("Addition:"+z);
}
void multi()
{
int x=20;
int y=20;
int z=x*y;
System.out.println("Multiplication:"+z);
}
}
class Division extends Multiplication
{
void div()
{
int x=100;
int y=20;
int z=x*y;
System.out.println("Division:"+z);
}
}
class icoderweb
{
public static void main(String[] args)
{
Division obj=new Division();
obj.add();
obj.sub();
obj.multi();
obj.div();
}
}
*****OUTPUT*****
Addition:500
Subtraction:100
Multiplication:400
Division:5.0
{
void add();
}
class Subtraction
{
void sub()
{
int x=300;
int y=200;
int z=x-y;
System.out.println("Subtraction:"+z);
}
}
class Multiplication extends Subtraction implements Addition
{
public void add()
{
int x=300;
int y=200;
int z=x+y;
System.out.println("Addition:"+z);
}
void multi()
{
int x=20;
int y=20;
int z=x*y;
System.out.println("Multiplication:"+z);
}
}
class Division extends Multiplication
{
void div()
{
int x=100;
int y=20;
int z=x*y;
System.out.println("Division:"+z);
}
}
class icoderweb
{
public static void main(String[] args)
{
Division obj=new Division();
obj.add();
obj.sub();
obj.multi();
obj.div();
}
}
*****OUTPUT*****
Addition:500
Subtraction:100
Multiplication:400
Division:5.0
1.Code Reusability: Code Reusability means function inside the base class is shared by all the derived classes.
2. Time-Saving: Because there is no need to define the same code of a class in another class.
Polymorphism: A Polymorphism means one name many forms so we can say that in this type of programming same function is used to perform different kinds of operations.
It is an important part of object-oriented programming language.
How To Use Polymorphism in java?
class Polymer
{
void p()
{
System.out.println("No parameters");
}
void p(int i)
{
System.out.println("Integer parameters");
}
void p(char)
{
System.out.println("Character parameters");
}
void p(float f)
{
System.out.println("Floating parameters");
}
void p(doubled)
{
System.out.println("Double parameters");
}
void p(String s)
{
System.out.println("String parameters");
}
}
class icoderweb
{
public static void main(String[] args)
{
Poly obj=new Polymor();
obj.p(12);
obj.p('e');
obj.p("Icoderweb");
}
}
*****OUTPUT*****
Integer Parameters
Character Parameters
String Parameters
{
void p()
{
System.out.println("No parameters");
}
void p(int i)
{
System.out.println("Integer parameters");
}
void p(char)
{
System.out.println("Character parameters");
}
void p(float f)
{
System.out.println("Floating parameters");
}
{
System.out.println("Double parameters");
}
void p(String s)
{
System.out.println("String parameters");
}
}
class icoderweb
{
public static void main(String[] args)
{
Poly obj=new Polymor();
obj.p(12);
obj.p('e');
obj.p("Icoderweb");
}
}
*****OUTPUT*****
Integer Parameters
Character Parameters
String Parameters
1.Compile time Polymorphism: Function overloading and Operator overloading are examples of Compile time Polymorphism.
2.Runtime Polymorphism: Function overriding is an example of Runtime Polymorphism.
Function overloading: A function overloading with the same name and the different parameters is called function overloading.
class FindArea
{
void area(int h, int w)
{
int a=h*w;
System.out.println("Area of Rectangle:"+a);
}
void area(int side)
{
int a=side*side;
System.out.println("Area of Square:"+a);
}
void area(float r)
{
float a=3.14f*r*r;
System.out.println("Area of Circle:"+a);
}
void area(float base,float height)
{
float a=0.5f*b*h;
System.out.println("Area of Triangle:"+a);
}
}
class icoderweb
{
public static void main(String[] args)
{
FindArea obj=new FindArea();
obj.area(8,5);
obj.area(20);
obj.area(2.2f);
obj.area(2.5f,6.3f);
}
}
*****OUTPUT*****
Area of Square:40
Area of Rectangle:400
Area of Circle:15.197601
Area of Triangle:7.875
{
void area(int h, int w)
{
int a=h*w;
System.out.println("Area of Rectangle:"+a);
}
void area(int side)
{
int a=side*side;
System.out.println("Area of Square:"+a);
}
void area(float r)
{
float a=3.14f*r*r;
System.out.println("Area of Circle:"+a);
}
void area(float base,float height)
{
float a=0.5f*b*h;
System.out.println("Area of Triangle:"+a);
}
}
class icoderweb
{
public static void main(String[] args)
{
FindArea obj=new FindArea();
obj.area(8,5);
obj.area(20);
obj.area(2.2f);
obj.area(2.5f,6.3f);
}
}
*****OUTPUT*****
Area of Square:40
Area of Rectangle:400
Area of Circle:15.197601
Area of Triangle:7.875
Function Overriding: A Function with the same name and same parameter is called function overriding.
It is not possible to make two functions with the same name and same parameter in a single class therefore to implement a function overriding derived class is used.
How To Use Function Overriding in Java?
class FindArea1
{
void area(int h, int w)
{
int a=h*w;
System.out.println("Area of Rectangle1:"+a);
}
}
class FindArea2 extends FindArea1
{
void area(int h, int w)
{
int a=h*w;
System.out.println("Area of Rectangle2:"+a);
}
}
class icoderweb
{
public static void main(String[] args)
{
FindArea2 obj=new FindArea2();
obj.area(80,80);
}
}
*****OUTPUT*****
Area of Rectangle2:6400
In the above example, there are two classes FindArea1 and FindArea2 containing the same function area with the same parameter.
We are extending FindArea1 into FindArea2, therefore, both function exists in the class FindArea2 but on priority bases.
when we call the function area from the object of class FindArea2 then the function inside FindArea2 will execute.
{
void area(int h, int w)
{
int a=h*w;
System.out.println("Area of Rectangle1:"+a);
}
}
class FindArea2 extends FindArea1
{
void area(int h, int w)
{
int a=h*w;
System.out.println("Area of Rectangle2:"+a);
}
}
class icoderweb
{
public static void main(String[] args)
{
FindArea2 obj=new FindArea2();
obj.area(80,80);
}
}
*****OUTPUT*****
Area of Rectangle2:6400