OOPS CONCEPT IN JAVA

Suvam Banerjee
6 min readNov 8, 2020

Important oops features in java

  1. Inheritance
  2. Abstraction
  3. Encapsulation
  4. Polymorphism

INHERITANCE

Using inheritance a class can acquire all properties and methods of another class ie: parent class.

Syntax: class a{}

class b extends a{}

Advantages of inheritance

oops doesn't encourage to duplicate same code, inheritance is oops kind of way to copy and paste same code to another class, but it occurs internally ie: a developer can’t see the pasted code in the class.

for eg:

suppose we are creating a game, in that game, there are 3types of monster

1st type of monster can attack only by walking or movement on land

2nd type of monster can attack by walking as well as flying

3rd type of monster can attack by walking as well as swimming

so for type 2 and 3 monsters, we need to create classes which duplicate code of type 1 monster which can walk only and new code for fly and swim respectively.

without inheritance : run method in flyMonster is exact copy paste run method in baseMonster
Without inheritance: run method of flyMonster class is an exact copy of baseMonster class’s run method
With inheritance: flyMonster don't have a run method but all features of run will be present in flyMonster object because it inherits the run method from baseMonster class

Type of inheritance supported by java

  1. Single inheritance: one parent -one child
  2. Multi-level inheritance: more than one generations, eg: class b extends class a , class c extends class b, class d extends class c…n.
  3. Hierarchical Inheritance: one parent — multiple children

Disadvantages of inheritance

  1. tightly coupled: change in the parent class code will affect all its child which extends that parent.

eg: suppose car class extends engine class, and we create many cars with the same engine. Now our engine get evolved and we want to create a brand new car with the new engine but our already created car shouldn't get new engine ie: old cars should not be affected because of engine up-gradation. If we inherit engine class into car class then this problem will not be resolved until and unless we didn't create a fresh NewEngine class from scratch.

2. In complex projects its tough to keep track of all parents and its child.

Association

we can overcome drawbacks of inheritance, it is basically a technique to get property and methods of other class. It is done by passing objects or by creating objects, whose behaviour is required.

a)Aggregation: A type of association, where passed object and receiving class are not dependent on each other. ie: Both objects can live independently because an object is created outside class and it is passed by reference, so if we delete the object of receiving class, still passed object will be present.

radio object is passed while the creation of car object

b) Composition: A type of association, where we inherit property by creating its object, while creation of receiving class object. ie: both objects are strongly dependent on each other.

engine object is created while the creation of car object

ABSTRACTION

Hiding internal implementation and to show just the name of methods.

How to implement abstraction in our application

  1. using abstract classes
  2. using interface

Abstract class

I)A class can be declared as an abstract if we don’t want to create an object of that class or if it has partially implemented methods or without any implementation ie: abstract methods.

II)We can’t create an object of abstract class but still, an abstract class contain constructor, to initialize its instance variables. Its child class constructor should call super( ) ie: calling the abstract class constructor and pass required values for initialization of abstract class instance variables.

III) It is the responsibility of child class of that abstract class, for complete implementation of all abstract methods in that abstract class. If child class doesn't provide a complete implementation of its parent abstract class then child class should also be declared as abstract class.

IV) While implementing abstract methods of abstract class we can’t reduce the scope of the method. ie: if the abstract method is public then in its child class it shouldn't be default but should be public.

V) Using abstract class we can achieve (0–100%) abstraction in our application as , an abstract class can contain normal method and also partially implemented methods or abstract methods.

VI)Abstract method doesn't have a body just method signature and return type and ends with ; . Every abstract method should be declared inside abstract class eg: public int thisIsAbsMethod(int x, String s);

VII) If our object has a runtime dependency then also we should go for an abstract method. eg: suppose we are making bread and let its class name is, public abstract MakeBread{…..} and we know the process and define all those processes inside methods of MakeBread class, but our bread required different temperature to cook at a different altitude, so we will make an abstract method, public double temp(String altitude); . During runtime, we will get to know on which altitude we are cooking bread. If we are cooking in the mountain region then, we will make a public class mountBread extends MakeBread{} and implement the abstract method of MakeBread ie: public double temp(String altitude){…..} , like this we will create another public class groundBread extends MakeBread{……..}

Interface

I) With interface we can achieve complete abstraction. The interface contains only abstract methods, by default methods in the interface is public and abstract, and variables are static, public and final. Interface variables are static because we interface variables are not assigned at time of declaration, final because variables should be constant as many classes can implement that interface if interface variable changes then all other class will be effected which implements that interface. public because any class in the project can implement that interface.

II) A java class can implement many interfaces but can extend only one class. Using interface we can provide multiple inheritances in java class.

user will get to see only methods name specified in atm interface, but unable to see method implementations

Data hiding

The variables in a class are declared with private access modifier such that other class can’t access that variable directly from outside that class. Using Encapsulation we wrapped those variables inside a method, the only way the outer class can access those variables via a specific encapsulated method.

Encapsulation

Encapsulation can be achieved making all instance variable in class private ie: data hiding and making a public getter and setter methods to get and set values respectively. Such that we can keep track of who has accessed private variables and after authentication, only those variables can be set in the setter method.

empId can’t be accessed directly by any outer class

Polymorphism

Using polymorphism we can provide multiple functionalities under the same name. Code reusability or

  1. Method overriding
  2. Method overloading

Method overriding

I)After inheritance When child class is not satisfied with the parent class method definition, then child class can change method definition according to its requirements keeping method signature same as the parent class.

II)This is also known as runtime polymorphism or dynamic polymorphism because at runtime only it is decided which method (parent or child) method to execute based on the object.

III) To perform method overriding we must have at least one parent-child relationship.

show method is overridden in showChild class

Method overloading

I)Method with same name and return type but different arguments types or different number of arguments. Those methods can be inside same class or after inheritance overloading inside child class.

II)Method overloading provides flexibility to use the same method definition with different arguments. We can perform overloading with two or one class.

III)Also knows a compile-time polymorphism as during compilation only it is known which class method is going to execute by its argument type or number of arguments.

show method is overloaded in parent as well as child class

--

--