3

Okay, so let's say I have a class called Pancake.

Pancake.java would look something like this:

public class Pancake {
    public boolean shouldEat() {
        return false;
    }
}

Now, I don't want to edit Pancake.java at all. All I want to do is to change what shouldEat() returns to from a different class, such as "NotPancake.java".

I know if I had something like

public boolean eat = false;

I could easily just change that by doing something like Pancake.eat = true, but is there any way of changing what shouldEat() returns to without editing the class in a similar way?

Thanks.

trogdor
  • 1,626
  • 14
  • 17
sn0wman
  • 75
  • 4

2 Answers2

7

The way to do that is based on Object Oriented programming

public class NotPancake extends Pancake {

 @Override
 public boolean shouldEat() {
        return true;
    }
}

The magic that happens here is called "inheritance," and is one of four elements assigned to OO.

What happens here is that you define new structure that is delivered from Pancake.

This means that NotPancake inherits form Pancake. So a NotPancake is a Pancake.

 Pancake p = new Pancake();
 Pancake q = new NoPancake(); 

 print(p.shouldEat()); //Will be false
 print(q.shouldEat()); //Will be true

This relation gives us the possibility to change the behavior, we are allowed to redefine all public, protected and defaults methods from super class.

The when in our child class we define a method with the same signature we say that this method override the previous one. In Java you should add the annotation @Override to inform compiler additionally about it.

This is one way to do it. The inheritance is simple but create a lot of issue in coding complex programs an alternative solution is called composition. and you should always prefer composition over inheritance.

The composition is based on the abstractness. To use it a interface is required. The interface is an description of an contract that classes that implements it follow.

public interface Dish {

   boolean isEatable();

}

The name of interface should represent some abstract general concept.

 public class Pancake implements Dish {
  @Override
  public boolean shouldEat() {
     return false;
  }
}

public class Carrot implements Dish {

 @Override
 public boolean shouldEat() {
     return true;
  }
}

Usage is more less the same.

 Dish dish1 = new Pancake();
 Dish dish2 = new NoPancake(); 

 print(dish1.shouldEat()); //Will be false
 print(dish2.shouldEat()); //Will be true

On this small example is hard to show the benefits for composition. But is demonstrate the coding to interface approach.

Community
  • 1
  • 1
3
Pancake p = new Pancake() {
   public boolean shouldEat() {
      return true;
   }
 };
Ted Bigham
  • 4,237
  • 1
  • 26
  • 31
  • 1
    Which is an anonymous subclass. – Hot Licks Feb 26 '14 at 20:52
  • Yep. Vash's way is using a first class Class (http://en.wikipedia.org/wiki/First-class_function#Java). Mine uses an instance of an anonymous class. Both are good solutions depending on the situation. I honestly use Vash's solution more often because it easier to maintain "named" things. – Ted Bigham Feb 26 '14 at 21:09
  • The first class function is something differ ans should not be confused with basis of OO. That concept refer to functional programming. – Damian Leszczyński - Vash Feb 26 '14 at 21:22
  • Yeah, unfortunately there's not a wikipedia link to first-class type, which is what i believe we're talking about here. This stackoverflow page would have been a better reference had I found it the first time around. http://stackoverflow.com/questions/599978/what-is-a-first-class-type – Ted Bigham Feb 26 '14 at 22:21