DEV Community

Lovepreet Singh
Lovepreet Singh

Posted on

OOPS | Write Scalable and Modular Code

🤜 I know that OOPS seems very simple but when it comes to real-world usage we have to do more than OOPS. There are some design patterns for industry-level code. To learn more about Design and Coding in General make sure you follow the Blog.

👽 Today We'll see Inheritance and Polymorphism by using some good examples. So, stay tuned.....

🫡 But before that make sure you are clear with the Abstraction and Encapsulation we discussed in our previous Blog. Before going to the inheritance and polymorphism we should look at the partial abstraction and full abstraction (talking with respect to JAVA). In Java we have

Abstraction via Interfaces vs Abstract Classes

  • Interfaces

  • Abstract Classes

Test1

😎 With Interfaces, we can achieve 100% Abstraction while using Abstract classes we can achieve abstraction between 0-100%.

import java.lang.*;

interface Exam{
    void setMarks();
    void printMarks();
}

class Test_A implements Exam{
    public int marks;

    @Override
    public void setMarks(){
        this.marks = 50;
    }
    @Override
    public void printMarks(){
        System.out.println(this.marks);
    }
}

class Test_B implements Exam{
    public int marks;

    @Override
    public void setMarks(){
        this.marks = 100;
    }
    @Override
    public void printMarks(){
        System.out.println(this.marks);
    }
}

public class Test{

    public static void main(String[] args){
        System.out.println("Hello World");
        Test_A obj1 = new Test_A();
        obj1.setMarks();
        obj1.printMarks();

        Test_B obj2 = new Test_B();
        obj2.setMarks();
        obj2.printMarks();
    }
}
Enter fullscreen mode Exit fullscreen mode

🫵 You can see that we have an interface Exam that Test_A and Test_B implement, both are setting marks differently (They can override the interface methods according to their convenience). The point to note here is that in the interface each and every method is abstract i.e. we can't have a default implementation of the method in the Base class (Interface in this case).

  • So, If we want to have some method abstract and some have default implementation we should use Abstract. Like, here printMarks() method has the same implementation for both Tests. So, we are violating the DRY(Do not repeat yourself) Principle here.*

📍 Instead we can do the:-

import java.lang.*;

abstract class Exam{
    int marks;
    abstract void setMarks();
    void printMarks(){
        System.out.println(this.marks);
    };
}

class Test_A extends Exam{

    @Override
    public void setMarks(){
        this.marks = 50;
    }
}

class Test_B extends Exam{

    @Override
    public void setMarks(){
        this.marks = 100;
    }
}

public class Test{

    public static void main(String[] args){
        System.out.println("Hello World");
        Test_A obj1 = new Test_A();
        obj1.setMarks();
        obj1.printMarks();

        Test_B obj2 = new Test_B();
        obj2.setMarks();
        obj2.printMarks();
    }
}
Enter fullscreen mode Exit fullscreen mode

😌 Now we can see that the abstract method has made only one method abstract and the common method has a default implementation.

Test2

Let's get to the Real Topic of the Day i.e. Inheritance and Polymorphism.

Inheritance

I think It should be clear from the abstraction explanation above. But there are a few points to note for inheritance.

📍 Inheritance is a process of inheriting properties and methods from the parent class. In general, we have different types of inheritance:-

Image description

Image description

😇 As stated above, Multiple and Hybrid inheritance is not possible in Java. But it is possible by using interfaces. In (d) part Class A and Class B could be Interface A and Interface B and Class C can extend from Interface A and Interface B.

Similarly, in (e) part It can be done by using interfaces. While inheriting Access-modifiers does affect us too. Accessing methods and classes with specific Access-modifiers:-

Image description

NOTE:- This is with respect to JAVA only. In C++ it is different.

🔥 For more, Kindly see the Internet because this is not that important today. We have discussed all necessary points regarding Inheritance.

Polymorphism

Image description

🤓 It simply means, functions existing with the same name but with different functionality. It is like we want to calculate a rectangle's area and expected integer length and breadth. But what if float/double value comes?

Method Overloading

class Rectangle{
    int calculateArea(int length, int breadth){
        return length*breadth;
    }
    double calculateArea(double length, double breadth){
        return length*breadth;
    }
}
Enter fullscreen mode Exit fullscreen mode

Method overloading is

  • Multiple methods have the same name but differences in the parameters

  • Can be done by changing no. of args

  • Can be done by changing the datatype(int, double, String, etc.) of args

  • Can be done by changing the order of parameters

  • Method overloading is resolved in compile time

  • Changing only the return type doesn't refer to method overloading.

Method Overriding

It is done in inheritance and It is resolved in runtime only. You can refer to the Abstraction and Interfaces above to see the inheritance.

  • Only inherited methods can be overridden

  • The overriding method should have a same return type

  • If the

    • the overridden method has default access, then the overriding method must be the default, protected, or public
    • the overridden method is protected, then the overriding method must be the protected, or public
    • the overridden method is public, then the overriding method must only be public
  • Note:- Overriding method is the method that is overriding and overridden method is the method that is being overridden.

📍 This was It for today. I hope you found something useful. If so, Kindly react to this blog and put your valuable comments below.

Follow for more on Software Design, Backend Development and much more.....

Top comments (0)