Provide Best Programming Tutorials

Object Oriented Programming features

Object Oriented Programming features

These four features are the main OOPs Concepts that you must learn to understand the Object Oriented Programming in Java

Abstraction

Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user. For example, when you login to your bank account online, you enter your user_id and password and press login, what happens when you press login, how the input data sent to server, how it gets verified is all abstracted away from the you.

Encapsulation

Encapsulation simply means binding object state(fields) and behavior(methods) together. If you are creating class, you are doing encapsulation.

Encapsulation example in Java

How to

  • Make the instance variables private so that they cannot be accessed directly from outside the class. You can only set and get values of these variables through the methods of the class.

  • Have getter and setter methods in the class to set and get the values of the fields.

class EmployeeCount{
   private int numOfEmployees = 0;
   public void setNoOfEmployees (int count){
       numOfEmployees = count;
   }
   public double getNoOfEmployees () {
       return numOfEmployees;
   }
}
public class EncapsulationExample{
   public static void main(String args[]){
      EmployeeCount obj = new EmployeeCount ();
      obj.setNoOfEmployees(5613);
      System.out.println("No Of Employees: "+(int)obj.getNoOfEmployees());
    }
}

Output:

No Of Employees: 5613

The class EncapsulationExample is using the Object of class EmployeeCount will not able to get the NoOfEmployees directly. It has to use the setter and getter methods of the same class to set and get the value. So what is the benefit of encapsulation in java programming Well, at some point of time, if you want to change the implementation details of the class EmployeeCount, you can freely do so without affecting the classes that are using it.

Inheritance

The process by which one class acquires the properties and functionalities of another class is called inheritance. Inheritance provides the idea of reusability of code and each subclass defines only those features that are unique to it, rest of the features can be inherited from the parent class.

  1. Inheritance is a process of defining a new class based on an existing class by extending its common data members and methods.

  2. Inheritance allows us to reuse of code, it improves reusability in your java application.

  3. The parent class is called the base class or superclass. The child class that extends the base class is called the derived class or subclass or child class.

Note: The biggest advantage of Inheritance is that the code in base class need not be rewritten in the child class. The variables and methods of the base class can be used in the child class as well.

Syntax: Inheritance in Java

To inherit a class we use extends keyword. Here class A is child class and class B is parent class.

class A extends B{
}

Inheritance Example

In this example, we have a parent class Teacher and a child class MathTeacher. In the MathTeacherclass we need not to write the same code which is already present in the present class. Here we have college name, designation and does() method that is common for all the teachers, thus MathTeacher class does not need to write this code, the common data members and methods can inherited from the Teacher class.

class Teacher {
   String designation = "Teacher";
   String college = "Beginnersbook";
   void does(){
    System.out.println("Teaching");
   }
}
public class MathTeacher extends Teacher{
   String mainSubject = "Maths";
   public static void main(String args[]){
      MathTeacher obj = new MathTeacher();
      System.out.println(obj.college);
      System.out.println(obj.designation);
      System.out.println(obj.mainSubject);
      obj.does();
   }
}

Output:

Beginnersbook
Teacher
Maths
Teaching

Note: Multi-level inheritance is allowed in Java but not multiple inheritance

Types of Inheritance

Single Inheritance: refers to a child and parent class relationship where a class extends the another class.

Multilevel inheritance: refers to a child and parent class relationship where a class extends the child class. For example class A extends class B and class B extends class C.

Hierarchical inheritance: refers to a child and parent class relationship where more than one classes extends the same class. For example, class B extends class A and class C extends class A.

Multiple Inheritance: refers to the concept of one class extending more than one classes, which means a child class has two parent classes. Java doesn’t support multiple inheritance, read more about it here.

Most of the new OO languages like Small Talk, Java, C# do not support Multiple inheritance. Multiple Inheritance is supported in C++.

Polymorphism

Polymorphism is a object oriented programming feature that allows us to perform a single action in different ways. For example, lets say we have a class Animal that has a method animalSound(), here we cannot give implementation to this method as we do not know which Animal class would extend Animal class. So, we make this method abstract like this:

public abstract class Animal{
   ...
   public abstract void animalSound();
}

Now suppose we have two Animal classes Dog and Lion that extends Animal class. We can provide the implementation detail there.

public class Lion extends Animal{
...
    @Override
    public void animalSound(){
        System.out.println("Roar");
    }
}

and

public class Dog extends Animal{
...
    @Override
    public void animalSound(){
        System.out.println("Woof");
    }
}

As you can see that although we had the common action for all subclasses animalSound() but there were different ways to do the same action. This is a perfect example of polymorphism (feature that allows us to perform a single action in different ways).

Types of Polymorphism

  • Static Polymorphism

  • Dynamic Polymorphism

Static Polymorphism:

Polymorphism that is resolved during compiler time is known as static polymorphism. Method overloading can be considered as static polymorphism example. Method Overloading: This allows us to have more than one methods with same name in a class that differs in signature.

class DisplayOverloading{
    public void disp(char c){
         System.out.println(c);
    }
    public void disp(char c, int num)  {
         System.out.println(c + " "+num);
    }
}
public class ExampleOverloading{
   public static void main(String args[]){
       DisplayOverloading obj = new DisplayOverloading();
       obj.disp('a');
       obj.disp('a',10);
   }
}

Output:

a
a 10

When I say method signature I am not talking about return type of the method, for example if two methods have same name, same parameters and have different return type, then this is not a valid method overloading example. This will throw compilation error.

Dynamic Polymorphism

It is also known as Dynamic Method Dispatch. Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime rather, thats why it is called runtime polymorphism.

Example

class Animal{
   public void animalSound(){
    System.out.println("Default Sound");
   }
}
public class Dog extends Animal{

   public void animalSound(){
    System.out.println("Woof");
   }
   public static void main(String args[]){
    Animal obj = new Dog();
    obj.animalSound();
   }
}

Output:

Woof

Since both the classes, child class and parent class have the same method animalSound. Which of the method will be called is determined at runtime by JVM.

Few more overriding examples:

Animal obj = new Animal();
obj.animalSound();
// This would call the Animal class method

Dog obj = new Dog();
obj.animalSound();
// This would call the Dog class method

Animal obj = new Dog();
obj.animalSound();
// This would call the Dog class method

Leave a Reply

Close Menu