DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Inheritance in Java Is More Useful Than You Think

Inheritance in Java Is More Useful Than You Think

Inheritance in Java is much more useful than you might think. Check out this article to learn more about using inheritance in Java and how it can simplify your code!

Murat Gungor user avatar by
Murat Gungor
CORE ·
Aug. 17, 18 · Opinion
Like (8)
Save
Tweet
Share
14.47K Views

Join the DZone community and get the full member experience.

Join For Free

A friend of mine came to me with a code problem that I am going to attempt to explain in this article. My friend is using a pre-existing graphics framework. In that framework, you have to extend from a certain interface, which has a specific function that takes only one parameter (which is another class). Up to this moment, there is no problem, but my friend says, "I need to send one more parameter along with that parameter." This means that the function's signature has to change. Then, this becomes another function, which that framework is not aware of.

I will try to simulate the same condition below for you and how to solve this kind of situation without changing a lot of code.

Here is the interface code that you want if you are going to add one more parameter next to Colours. However, you are not allowed to change this interface. Even if you could change this interface, there are tons of other classes that are already using this interface.

// This is part of the framework 
// You are not allowed to change 
public interface IColourMaker {
    public void printColour(Colours colours);
}


Below is the Colours class:

//You should not change this class
public class Colours {

    private String[] colourArray;

    public Colours() {
        colourArray = new String[2];
        colourArray[0] = "Blue";
        colourArray[1] = "Red";
    }

    public String[] getColours() {
        return colourArray;
    }
}


Below are two sample classes that come with the framework:

//You should not change this class
public class PaleMaker implements IColourMaker {

    @Override
    public void printColour(Colours colours) {
        System.out.println("PaleMaker Colour Maker");
        for (String string : colours.getColours()) {
            System.out.println("Pale: " + string);
        }
        System.out.println("PaleMaker Colour Maker");
    }
}

// You should not change this class
public class ShinyMaker implements IColourMaker {

    @Override
    public void printColour(Colours colours) {
        System.out.println("ShinyMaker Colour Maker");
        for (String string : colours.getColours()) {
            System.out.println("Shiny: " + string);
        }
        System.out.println("ShinyMaker Colour Maker");
    }
}


Here is a new class that you might want to add, but it needs one more parameter.

public class AdvancedColourMaker implements IColourMaker {

/*
 * Problem here AdvancedColourMaker needs one more parameter to do its job And
 * AdvancedColourMaker does need one more parameter The printColour signature
 * below is not helping us since we can not call polymorphically What do you
 * offer as a solution? Which does not have ripple effect on other classes
 * Assume there are tons of different Maker classes - and those are not ours, we
 * can not modify *
 */

    public void printColour(Colours colours, int count) {
        System.out.println("Advanced Colour Maker Count is: " + count);
        for (String string : colours.getColours()) {
            System.out.println("Advanced: " + string);
        }
        System.out.println("Advanced Colour Maker"); 
    }

}


Below is the main method. This does not work currently since the AdvancedColourMaker is not implemented yet.

import java.util.ArrayList;

public class Main {

public static void main(String[] args) {
System.out.println("Before Refactoring");

Colours colours = new Colours();

ArrayList<IColourMaker> maker = new ArrayList<>();
maker.add(new PaleMaker());
maker.add(new ShinyMaker());
maker.add(new AdvancedColourMaker());
// This code wants to use its own method however it needs two parameters
// However we do not know tomorrow customer may want to send three or for
// parameters...
// How can we fix it, without changing base class or other derived classes
// except ColourMakerAdvanced
// And this fix should accommodate upcoming new parameters as well.

for (IColourMaker colourMaker : maker) {
colourMaker.printColour(colours);
}
}
}


Wrong Direction

Now, I am trying to push one more parameter to the interface, insisting on changing the function signature. However, this is not helping us and making life complicated by bringing us cascading new changes.

You may have already seen the solution, but this was the mental journey I went through, and it was wrong!

The solution was hidden in just the extending Colours class!

public class MyColour extends Colours {
    public int count;
}


Now, I can add as many parameters as I want without changing the function signature.

Here is my AdvancedColourMaker :

public class AdvancedColourMaker implements IColourMaker {

public void printColour(Colours colours) {
        MyColour myColour = (MyColour) colours;
        System.out.println("Advanced Colour Maker Count is: " + myColour.count);
        for (String string : colours.getColours()) {
            System.out.println("Advanced: " + string);
        }
        System.out.println("Advanced Colour Maker");
    }
}


Here is the running main method:

import java.util.ArrayList;

public class Main {

public static void main(String[] args) {

MyColour colours = new MyColour();
colours.count = 10;

ArrayList<IColourMaker> maker = new ArrayList<>();
maker.add(new PaleMaker());
maker.add(new ShinyMaker());
maker.add(new AdvancedColourMaker());

for (IColourMaker colourMaker : maker) {
colourMaker.printColour(colours);
}
}
}


Below is the sample run:

PaleMaker Colour Maker
Pale: Blue
Pale: Red
PaleMaker Colour Maker
ShinyMaker Colour Maker
Shiny: Blue
Shiny: Red
ShinyMaker Colour Maker
Advanced Colour Maker Count is: 10
Advanced: Blue
Advanced: Red
Advanced Colour Maker



Conclusion

Sometimes, we try to search for solutions in the wrong place and try to force a change in others' code. Instead of extending it and adding your own style without disturbing other codes, I noticed that I can extend the Colour class and add as many parameters as I want — this is fabulous. I did not have to change the pre-existing classes. Because of this, I realized just how powerful is the inheritance.

Hope you enjoyed this demonstration!

Inheritance (object-oriented programming) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Are You Missing by Debugging in VS Code?
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • 5 Factors When Selecting a Database
  • How to Create a Dockerfile?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: