News for hackers. Stuff that matters.
Re:Inheritance Is Terrible by teh64 on Saturday March 19, 2016 @06:47PM (#11320843)
This can be done in Java just fine:

  public interface PriceAble
  {
	int getPrice();
  }

  class Book implements PriceAble
  {
	int royalty = 2;
	int markup = 8;

	@Override
	public int getPrice()
	{
		return royalty + markup;
	}
  }

  class Movie implements PriceAble
  {
	int price = 15;

	@Override
	public int getPrice()
	{
		return price;
	}
  }

  class Cart
  {
	int cartTotal = 0;

	void increaseTotal(PriceAble priceAble)
	{
		cartTotal += priceAble.getPrice();
	}
  }

  public class forExample
  {
	public static void main(String[] args)
	{
		PriceAble movie = new Movie();
		PriceAble book = new Book();
		Cart cart = new Cart();

		System.out.println(cart.cartTotal); //prints 0
		
		cart.increaseTotal(movie);

		System.out.println(cart.cartTotal); //prints 15

		cart.increaseTotal(book);

		System.out.println(cart.cartTotal); //prints 25
	}
  }

There are no comments on this comment yet.