X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Encapsulation :

It is a process of grouping data and instructions which operates on the data within a single entity. Wrapping of data and instruction is called encapsulation.

If any Java program, class which follows data hiding and abstraction then such type of class is said to be encapsulated class.


X

entity :

It refers to a thing with distinct and independent existence.

X

Wrapping :

It refers to arrange or fold (something) about as cover or protection.

Advantages of Encapsulation :

Data Hiding :

Preventing data access from unrelated operation is called data hiding.

By using private modifier we can implement data hiding.

class Algo
{
 private int a = 10;
}
// the main advantage of data hiding is that we can achieve security.

Binding

Linking operations with related data is called binding.


Tightly encapsulated class :

A class is said to be tightly encapsulated if and only if every data members are declared as private.

class Algo
{
 private int a;
 public void m1()
 {
 
 }
}

Example :

class Example { private String name; private String idNum; private int age; public int getAge() { return age; } public String getName() { return name; } public String getIdNum() { return idNum; } public void setAge(int newAge) { age = newAge; } public void setName(String newName) { name = newName; } public void setIdNum(String newId) { idNum = newId; } }

The public methods are the access points to the class fields from outside Java world. Normally, these methods are referred as getters and setters. Therefore, any class that wants to access the variables should access them through these getters and setters.

Example :

class Example { private String name; private String idNum; private int age; public int getAge() { return age; } public String getName() { return name; } public String getIdNum() { return idNum; } public void setAge(int newAge) { age = newAge; } public void setName(String newName) { name = newName; } public void setIdNum(String newId) { idNum = newId; } public static void main(String [ ] args) { Example encap =new Example(); encap.setName("James"); encap.setAge(20); encap.setIdNum("12343ms"); System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge()); } }

Output :

Name : James Age : 20

Type of relationships in Java :

Based on reusing the data members from one class to another class in Java we have three types of relationships. They are :

  • Is-a relationship




  • Has-a relationship




  • Uses-a relationship




Is-a relationship :

  • Is-a relationship is one in which data members of one class are obtained into another class through the concept of inheritance.

class Base
{
 public void m1()
 {
 
 }
}
class Derived extends Base
{
 public void m2()
 {
 
 }
}
class Example
{
 public static void main(String [ ] args)
 {
  Base b = new Base();
  b.m1(); // valid
  b.m2(); /* Compile time error : cannot find symbol
                                  symbol : method m2()
                                  location : variable b of type Base */

  Derived d = new Derived();
  d.m1(); // valid
  d.m2(); // valid
  
  Base b = new Derived();
  b.m1(); // valid
  b.m2(); /* Compile time error : cannot find symbol
                                  symbol : method m2()
                                  location : variable b of type Base */
  Derived d = new Base(); /* Compile time error : incompatible types
                                                  required : Derived
                                                  found : Base */
 }
}
  • Whatever the base class has by default become available to the derived class reference and we can call both base and derived class methods.

  • Whatever the derived class has is not available to the base class hence, on the base class reference we can call only base class methods and we can't call derived class specific methods.

  • Base class reference can be used to hold derived class object. By using that reference we can call only base class methods but we can't call derived class specific methods.

  • We can't use derived class reference to hold base class objects. Example : The common functionality which is required for any Java class is defined in Object class and by keeping that class as super class it's functionalities by default available to every Java class. The common functionality which is required for all exceptions and errors is defined in Throwable class as Throwable is parent for all exceptions and errors. It's functionality will be available automatically to every child.

  • Java won't provide support for multiple inheritance but through interface it is possible.

  • Every class in Java is a child class of Object.

  • If our class doesn't extends any other class then only it is direct child class of Object.

  • If our class extends any other class then our class is not directly a child class of Object.

  • Cyclic inheritance is not allowed in Java.

Has-a relationship :

  • Has-a relationship is one in which an object of one class is created as a data member in another class.

  • It is also known as "Composition or Aggregation".

  • There is no specific keyword to implement has-a relationship, mostly we use new keyword.

  • The main advantage of Has-a relationship is reusability.

  • The main disadvantage of Has-a relationship is that it increases dependencies between classes and create maintenance problems.

class A
{

}
class B
{
 A obj1 = new A();
}
  • In Has-a relationship, we create an object of class A in the context of class B. In the context of class B, physical memory space is created for the features of class A. In general, if multiple classes creates an object of class A then in the context of all the classes, memory space is created for data members of class A explicitly.

Composition :

In the case of composition, whenever container object is destroyed then all contained objects will be destroyed automatically i.e. without existence of container object, there is no chance of existence of contained object i.e. container and contained objects has strong association. Example : University is composed of several departments. Whenever University is closed then automatically all departments of that university will be closed. The relationship between university and department is strong association which is nothing but composition.


Aggregation :

In the case of aggregation, whenever container object is destroyed then there is no guarantee of destruction of the contained objects i.e. without existence of container object there may be a chance of existence of contained object i.e. container object just maintain reference of contained objects. This relationship is called weak association which is nothing but aggregation. Example : Several professors will work in the department. Whenever we are closing the department, still there may be a chance for the professors to switch to other department. The relationship between department and professor is called weak association which is nothing but aggregation.


Uses-a relationship :

  • Uses-a relationship is one in which a method of one class is using an object of another class.

class A
{

}
class B
{
 void m1()
 {
  A obj1 = new A();
 }
}
  • In this relationship as long as m1() method of class B will be the executing, object of class A will be available and when m1() method completes its execution then the object of class A will be destroyed.

Note :

  • The default relationship in Java is 'Is-a relationship' because for each and every class in Java there exists an implicit predefined super class known as java.lang.Object.

  • System.out is the universal example for 'Has-a relationship' because out is an object of PrintStream class created as static data member in another predefined class called System. The relationship between System and PrintStream class is known as Has-a relationship.

  • Each and every execution logic method i.e. main() method of execution logic class is using an object of business logic class is known as Use-a relationship.


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.