Monday, June 23, 2025
HomeJavaWhy is Summary class Necessary in Java?

Why is Summary class Necessary in Java? [Example]


Summary class is a particular class in Java, it can’t be instantiated and that is why can’t be used instantly. At first idea of abstraction, summary class and interface all look ineffective to many builders, as a result of you can’t implement any technique in an interface, you can’t create an object of the summary class, so why do you want them. As soon as they face largest fixed of software program growth, sure that’s CHANGE, they perceive how abstraction on the high stage will help in writing versatile software program. A key problem whereas writing software program (Java Applications, C++ packages) is not only to cater right now’s requirement but additionally to make sure that nurture requirement will be dealt with with none architectural or design change in your code. Briefly, your software program have to be versatile sufficient to assist future adjustments.

The summary class and inheritance collectively ensures that a lot of the code are written utilizing summary and better stage lessons, in order that it may possibly leverage
Inheritance and Polymorphism to assist future adjustments.

That is truly one of the helpful design precept, also referred to as “Programming for interfaces quite than implementation”.  Summary class and summary technique are two methods by way of which Java help you on coding at a sure stage of abstraction.

We’ll have a look at syntax stage particulars, functionality, and limitation of summary class on the later half of article, however it’s essential to perceive why an summary class is necessary. Extra importantly, why you, a Java programmer, should perceive motive and use of an summary class.

From my years of expertise, I can say that making a category at first an interface or summary class is an effective apply, it won’t solely show you how to to check your code rapidly by supplying mock objects, but additionally show you how to to jot down code on the sure stage of abstraction.

On this article, we are going to perceive why we want summary class and summary technique and discover ways to use them by writing a easy instance.

The place do you utilize Summary Class in Java

Aside from typical use of Summary class to jot down versatile code, there are a number of intelligent makes use of of summary class, which ensures sure greatest practices are adopted. Most of those utilization benefit from necessary properties of summary class e.g. you can’t create object of summary class. Should you accomplish that, compiler will flag an error. 

One of many intelligent use of summary class I’ve seen is to make use of summary class at the side of Manufacturing unit technique sample to make sure that your shopper at all times use Manufacturing unit technique to create occasion of object, as an alternative of by accident calling constructor. By making Product class summary, compiler will monitor down any name to new Product() and power them to make use of manufacturing facility strategies like makeProduct() or getProduct()

Top-of-the-line instance of this system is EnumSet class in JDK, not like different implementation of Set interface, EnumSet is summary, which ensures that you simply at all times make use of manufacturing facility strategies like EnumSet.of() as an alternative of doing new EnumSet()

Think about, if EnumSet was not summary and you’re utilizing it first time, how would you have got created occasion of EnumSet? Manufacturing unit technique or Constructor? All the time bear in mind, compiler is your greatest buddy, it finds bugs for you, So make full use of your greatest buddy. A greatest apply will be forgotten however a rule enforced by compiler won’t ever.

Why Summary class is Necessary in Java

Although there are lot of distinction between summary class and interface, key factor to recollect is that they each offers abstraction. Let’s take an instance, you have to design a program, which might produce experiences for workers e.g. what number of hours they labored and the way a lot they’re paid each month. Let’s assume that at the moment your group solely has everlasting staff, that are paid month-to-month. 

You understand that and also you write code primarily based upon that, after someday your organization began recruiting contract staff, that are paid at hourly fee quite than month-to-month wage. Now, if you have to rewrite your program to assist this, they your program isn’t versatile sufficient. 

However, when you simply wants to jot down some code to plug this new kind of worker into system, they your program may be very a lot versatile and maintainable. Should you would have recognized about summary class, you’ll have made Worker an summary class and strategies like wage() summary, as a result of that’s what varies between various kinds of staff. 

Now introducing a brand new kind of Worker could be cakewalk, all you have to do is to create one other subclass of Worker to signify ContractEmployee, and their wage technique return wage primarily based upon a lot of hours they’d labored multiplied by their hourly fee. So, you get the thought proper, we code at a sure stage of abstraction, which permits us to accommodate new adjustments in our system.

Summary class and Methodology Instance in Java

Let’s examine one other instance of summary class and technique, this one is a quite tasty instance. I really like fruits, they’re tasty, present nutritional vitamins and also you needn’t cook dinner them to eat. All you do, you are taking fruit, wash them then you definately both reduce them or peel them earlier than consuming. 

Our instance relies on this conduct. We’ve got created an summary class Fruit, which incorporates some concrete conduct like coloration, whether or not it is a seasonal fruit or not, and summary conduct referred to as put together().

Abstract class and method Example in Java

 To be able to serve fruits, we have to put together them for consuming, which entails both slicing them in slices or peel them. We’ve got two concrete implementations of Fruit class, Mango and Banana, I selected these two as a result of first, they’re my favorites, and second they’ve completely different strategies of making ready them. You chop mangoes however you peel bananas.

import java.awt.Coloration;
import java.util.ArrayList;
import java.util.Assortment;
import java.util.Record;

/**
 * Java Program to show what's summary class and 
 * summary technique in Java, how you can use
 * them, when to make use of them with a sensible instance.
 *
 * @creator Javin Paul
 */
public class AbstractClassDemo{

    public static void predominant(String args[]) {
        Fruit mango = new Mango(Coloration.YELLOW, true); // mango is seasonal
        Fruit banana = new Banana(Coloration.YELLOW, false); 
        // banana isn't seasonal

        Record<Fruit> platter = new ArrayList<Fruit>();
        platter.add(mango);
        platter.add(banana);
        serve(platter);
    }

    public static void serve(Assortment<Fruit> fruits) {
        System.out.println("Making ready fruits to serve");
        for (Fruit f : fruits) {
            f.put together();
        }
    }
}


/*
 * Summary class to signify Fruit, outlined solely important
 * properties of Fruit right here and make issues summary which
 * is completely different for various fruits.
 */
summary class Fruit {
    personal Coloration coloration;
    personal boolean seasonal;

    public Fruit(Coloration coloration, boolean seasonal) {
        this.coloration = coloration;
        this.seasonal = seasonal;
    }

   /*
    * That is an summary technique, see it would not have technique physique, 
    * solely declaration
    */
    public summary void put together();

    public Coloration getColor() {
        return coloration;
    }

    public boolean isSeasonal() {
        return seasonal;
    }
}


/*
 * A concrete class to increase Fruit, since Mango IS-A Fruit
 * extending Fruit is justified. it received all properties of
 * fruit, and it defines how you can put together mango for consuming.
 */
class Mango extends Fruit {

    public Mango(Coloration coloration, boolean seasonal) {
        tremendous(coloration, seasonal);
    }

    @Override
    public void put together() {
        System.out.println("Lower the Mango");
    }
}


/*
 * One other concrete class to increase Fruit.
 */
class Banana extends Fruit {

    public Banana(Coloration coloration, boolean seasonal) {
        tremendous(coloration, seasonal);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void put together() {
        System.out.println("Peal the Banana");
    }
}

Output:
Making ready fruits to serve
Lower the Mango
Peal the Banana

Issues to recollect about summary class and technique in Java

Utility of one thing e.g. summary class is figure of creativity and takes time to grasp, however remembering syntax and necessary properties is quite straightforward, which ultimately show you how to to achieve the primary aim. As a Java programmer, it’s essential to at all times bear in mind following issues about summary class and technique.

1) You can’t create occasion of an summary class in Java. For instance if a category ABC is summary than code like Abc occasion = new ABC() will lead to compile time error. Consider two situations the place you may benefit of this property, at all times bear in mind a weapon can be utilized for each assault and defence. One state of affairs I’ve already mentioned right here is to creating positive that your shopper at all times use manufacturing facility technique to create object as an alternative of instantly calling constructor.

2) To create a concrete class by extending summary class, it’s essential to override all summary technique. For instance if a category ABC has an summary technique abc() then a category EFG, which extends ABC should override abc() to be a concrete class i.e. whose occasion will be created. 

On associated word, at all times use @Override annotation whereas overriding a way in Java. It not solely assist your fellow programmers to see your intent but additionally helps your buddy compiler to search out refined errors which might simply ship attributable to blind religion in your different buddy IDE’s content material help. Consider me its too straightforward to import a category with similar identify however from completely different bundle with code auto-completion function of contemporary IDEs.

3) A category will be summary even with none summary technique. Although, its not necessary (not ensured by any compiler rule) to have summary technique inside summary class, however this isn’t advisable, as a result of there is no such thing as a level doing it. 

In case your class have no summary behaviour (behaviour which adjustments between differing kinds) then its good candidate of being a concrete class quite than summary. There is just one case the place I believe summary class with out summary technique can be utilized is once you want a marker class as an alternative of marker interface, however to be frank, I’ve but to discover a sensible use case. Although considering a category as summary at begin will be good for locating some stage of abstraction.
4) Each high stage and nested class will be make summary in Java, no restriction on this from the Java programming language.

That is all in regards to the Summary class in Java. All the time keep in mind that something summary (e.g. summary class or summary technique) isn’t full in Java, and it’s essential to prolong the summary class and override the summary technique to utilize that class. Keep in mind, Abstraction is essential to design versatile programs. Flexibility comes from utilizing interfaces and summary class at high stage and leverage inheritance and Polymorphism to produce completely different implementation with out altering the code which makes use of them.

Different Java programming and OOP tutorials It’s possible you’ll like

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments