Advantages
The following are a few advantages of using Encapsulation:
- Provides the ability to change one part of code without affecting another part of code.
- Controls the access of the user interface.
- With new requirements it is easy to change encapsulated code.
- Helps to write immutable classes in Java that are beneficial in multi-threading environments.
- Encapsulation in Java makes unit testing easy.
- Reduces the coupling of modules siince all pieces of the same type are encapsulated in one place.
2. Inheritance
The main feature of Inheritance is code re-usability. So when making a new class we can use a previously written class and further extend it. In Java, classes may inherit or acquire the properties and methods of other classes. A class derived from another class is called a subclass, whereas the class from which a subclass is derived is called a super class. A subclass can have only one super class, whereas a super class may have one or more sub-classes.
Example:
class StudentRec
{
//GET STUDENT RECORD.....
String name;
int rollno;
int get(String n, int r)
{
name=n; rollno=r; return(0);
}
void showDetails()
{
System.out.println("Name : "+name);
}
}
class InClassDemo extends StudentRec
{
public static void main(String args[])
{
//CREATE OBJECT OF STUDENT RECORD CLASS
StudentRec studObj = new StudentRec();
studObj.get("SANDEEP SHARMA", 92);
studObj.showDetails();
}
void displayDetails()
{
System.out.println("Sample Info Display");
}
}
Output:
3. Polymorphism:
In Core Java Polymorphism is an easy concept to understand. Polymorphism in Greek is a combination of poly, which means many and morphism which means forms. It refers to the object's ability to be Polymorphic depending on its type.
There are two types of Polymorphism available in Java.
1) Static Polymorphism
2) Dynamic Polymorphism
Let's discuss Static Polymorphism. It's compile-time Polymorphism. We have two important concepts in Polymorphism, i.e Method Overloading and Method Overriding.
1. Method Overloading
In Java method overloading we define two or more methods with the same name but different parameters. The methods are said to be overloaded, and the process is referred to as method overloading.
Example for Method overloading
The following example program will make you understand Method Overloading:
class Sub
{
void add(int tamil, int english)
{
System.out.println("The total of tamil and english is "+(tamil+english));
}
void add(int tamil,int english,int maths)
{
System.out.println("The total of tamil english and maths is "+(tamil+english+maths));
}
}
public class MetOvlDemo
{
public static void main(String arg[])
{
//create Subjects class object
Sub sb=new Sub();
// we have to call add() method by passing 2 values
sb.add(90, 80);
//here also we are calling add() method by passing 3 values, So the 3 arguments (parameters) method will get execute.
sb.add(95,85,100);
}
}
Output
2. Method Overriding
Now we will discuss what dynamic polymorphism is. It's run time polymorphism. We can also call it Method Overriding. In a class hierarchy, when a method in a sub class has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. This feature is called method overriding.
Example
class MathsSqr1
{
void calculate(double price)
{
System.out.println("Sqare value "+(price*price));
}
}
class MathsSqr2 extends MathsSqr1
{
void calculate(double price)
{
System.out.println("Sqare value "+(Math.sqrt(price)));
}
}
public class Metovrr
{
public static void main(String arg[])
{
MathsSqr2 msd=new MathsSqr2();
msd.calculate(25);
}
}
Output
4. Abstraction
When we hide the unnecessary detail and defining the useful (relevant) detail, then the procedure is said to be abstraction. An interface or abstract class is something that is not concrete, something that is incomplete. Another way of providing a simple explanation is to use a more complex system as an example. One does not want to understand how an engine works. Similarly one does not need to understand the internal implementation of the software objects.
Abstraction in Java is done by using an interface and abstract class in Java. In order to use an interface or abstract class we need to explain the methods of an interface or abstract class in a sub-class.
Abstraction Example: Engine, Driving.
Main advantage: the user gets data according to their needs but they don't need to use unnecessary data. The following example will show Abstraction.
Example