Encapsulation is a process of wrapping code and data together into a single unit.
We can create a fully encapsulated class in java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.
Encapsulation provides control over data.
Example of Encapsulation.
//save this program as customer.java
package com.testpackage;
public class customer{
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name=name
}
}
//save this program as test.java
package com. testpackage;
class test{
public static void main(String[] args){
customer c=new customer();
c.setName("Raj");
System.out.println(c.getName());
}
}
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
i.e Hiding internal details and showing functionality is known as abstraction.
For example: Television, we don't know the internal processing, but we know its functionality and how to use it.
In java, there are two ways to achieve abstraction in java
Abstract class
Interface
Abstract Class
A class that is declared with abstract keyword is known as abstract class in java. It can have abstract methods (methods without body) and non-abstract methods (method with body).
Abstract class cannot be instantiated.
Example:
abstract class abs { }
Abstract Method
A method that is declared using abstract keyword and do not have implementation is known as abstract method.
Example:
abstract void add(); // no body , only method declaration.
Example of abstract class having abstract method.
abstract class Shape{
abstract void draw();
}
//Implementation is provided by others i.e. hidden from end user
class circle extends Shape{
void draw()
{ System.out.println("Draw Circle");
}
}
class rectangle extends Shape{
void draw()
{ System.out.println("Draw rectangle");
}
}
Class line extends shape {
Void draw()
{ System.out.println(“Draw line”);
}
}
class AbstractionTest {
public static void main(String args[]) {
shape s = new rectangle(); //Object of rectangle class (rectangle class instantiated)
s.draw(); // draw() method is called with respect to rectangle class object
}
}
Note:
Abstract class can have both abstract and non-abstract methods.
Interface can have only abstract methods (Method without body, only declaration allowed).
Interface
The interface in java is a mechanism to achieve abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve abstraction and multiple inheritances in Java.
Java Interface also represents IS-A relationship.
It cannot be instantiated just like abstract class.
Why Interface used:
To support the functionality of multiple inheritances.
To achieve abstraction.
The java compiler adds public and abstract keywords before the interface methods and it adds public, static and final keywords before data members.
Fig 3.1 Interpretation of methods and members in interface by java compiler
Interface Example
Interface display {
Void print(); // only method declaration, no body.
}
Class abc implements display {
Void print () { // print method is implemented here.
System.out.println(“print inside class abc”) ;
}
Public static void main(string args[]) {
Abc obj = new abc();
Obj.print();
}
}
Example: Multiple Inheritance in Java using Interface
interface display {
void print();
}
interface show {
void print();
}
class minheritance implements display, show{
public void print(){System.out.println("print method inside class minherit");}
public static void main(String args[]){
minheritance obj = new minheritance ();
obj.print();
}
}
Inheritance is a mechanism in which one object (the Child) acquires all properties & behaviors of other (the parent) object.
Using inheritance we can create new class built upon existing classes.
When we inherit from an existing class we can reuse the properties and methods of an existing class (parent class/super class).
Also we can add new properties and methods to the child class (sub class).
Inheritance represents IS-A relationship, also known as parent-child relationship.
Syntax of Java Inheritance
Class child_class_name extends parent_class_name
{
// Properties & methods of child class
}
Inheritance Example
class Citizen {
string name = “Raj” ;
Int SSN = 876554226784;
}
class Employee extends Citizen {
Float Salary=190000.64;
public static void main(String args[]){
Employee E=new Employee ();
System.out.println("Employee name is:"+E.bonus);
System.out.println("Employee SSN is:"+E.SSN);
System.out.println("Employee salary is:"+E.salary);
}
}
Types of Inheritance in Java
There are 3 types of inheritance in java 1) Single 2) Multilevel 3) Hierarchical . Below picture illustrates these 3 types of inheritance.
Fig 2.1 Inheritance types in Java
Note: Multiple inheritance is not supported in java through class.
In java programming, multiple inheritance is supported through interface only.
Lets us understand, Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Fig 2.2 Multiple Inheritance not supported in Java
In the above figure 2.2, class C is inherited using both class A and class B (multiple inheritance).
Both class A and class B have same method called add().
Now suppose we create object of the class C and call method add() there will be ambiguity about which method to call, whether to call method of class A or class B.
C obj = new C()
obj.add() // ambiguity error
Above scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class.
Since compile time errors are better than run time errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error.