How do I minimize the amount of code for a hierarchy

IJavaBad :

This is for learning. I have an interface that is implemented by 2 classes, and I am supposed to reduce the amount of code I use in order to keep things more clean and less messy. Currently, the code looks like this:

public abstract class VClass implements IntFace {

  protected String name;

  public VClass(String name) {
    this.name = name;
  }

  public int value (SClass sc) {//comes from a diff class
    return sc.lookup(name);
  }

  public String getName() {
   return name; 
  }

  @Override
  public String toString() {
   return getName(); 
  }
}

public abstract class NClass extends VClass implements IntFace {

  public Number(String name) {
   super(name);
     this.name = name;
  }

  public int value (SClass sc) {
    return sc.lookup(name);
  }

  public String getName() {
   return name; 
  }

  @Override
  public String toString() {
   return getName(); 
  }
}

public interface IntFace {

  public int value (SClass sc);

  public String toString (int num);
}

can this code be more condensed?

Bananon :

You can remove the following things from your code:

  1. implements IntFace from NClass declaration. Since NClass extends VClass, it implements IntFace as well.
  2. this.name = name; from NClass constructor. name is initialized in a superclass constructor
  3. value and getName methods from NClass. These methods are implemented in a superclass.
  4. public modifier from interface methods declaration. Methods in interfaces are public by default.

Now you can also make name field private since it's no longer used in a NClass.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I generate a QR code for a Bitcoin address with amount?

How do I refactor my code to reduce the amount of nested loops?

how do i reduce the amount of if statements in my code?

How can I minimize the code size of this program?

How do I make a winform minimize animation

How do I code dose-response (4PL) curve fitting with optimize.minimize()

How do I change my objective to "Minimize Max() - Min()" in OPL Code?

How do I inspect the view hierarchy in iOS?

How do I find the highest pair(two dices) among n amount of dice. c code

How do I specify the amount of random numbers in my code in C++

How can I make Tkinter in python to do every code in certain amount of time?

How do I make it so my code displays a cerrtain amount of lines (like it does) but backwards

How to minimize this ajax code?

How minimize this PHP CODE

How to minimize code in jQuery

How do I concatenate a certain amount of arrays?

How do I set the ErrorProvider blink amount?

How do I outdent by a specific amount of tabs?

How do I display the amount without refresh?

How do I limit the amount of letters in a string

RegEx: How do I capture the dollar amount?

How do I show the amount of true and falses?

How do i limit the amount of objects are instantiated?

How can I minimize boilerplate code associated with std::thread?

How do I minimize mercator projection using D3

How do I minimize my installer in Inno Setup?

How do I remove the maximize and minimize buttons from a JFrame?

How do I disable Ctrl+Alt+s to minimize a window?

How do I minimize Signal to the system tray on Windows?