Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java need interfaces and Smalltalk does not?

I have been programming in Smalltalk for some time, but I never really needed interfaces to implement anything. Then why can't languages such as Java get rid of interfaces? Is it only Smalltalk or is there another language which doesn't need interfaces?

like image 507
user869097 Avatar asked Nov 01 '11 20:11

user869097


People also ask

Why do you need interfaces in Java?

In Java, an interface specifies the behavior of a class by providing an abstract type. As one of Java's core concepts, abstraction, polymorphism, and multiple inheritance are supported through this technology. Interfaces are used in Java to achieve abstraction.

Why do we use interface instead of abstract class in Java?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

Why do we need interface in OOP?

Interfaces allow you to specify what methods a class should implement. Interfaces make it easy to use a variety of different classes in the same way. When one or more classes use the same interface, it is referred to as "polymorphism".

What is Smalltalk in Java?

Smalltalk uses keyword message selectors, whereas Java uses traditional C-like selectors (virtual function names). • Java supports a method overloading based on static types, whereas there is no static type information in Smalltalk.


2 Answers

Because Java is statically typed while Smalltalk is not. Interfaces don't serve any purpose when you don't declare types and your variables aren't going to be typechecked. But in a statically typed language like Java, they're extremely handy, because they let you have a variable whose type is defined by the methods the object implements instead of its class. It brings you a lot closer to the dynamic typing Smalltalk has natively without giving up the benefits of typechecking.

like image 145
Chuck Avatar answered Oct 13 '22 16:10

Chuck


It is a polymorphism issue: in Java you have static types and therefore you need to know which messages can your object answer... in Smalltalk (and other non-static languages) you just need to implement right methods to have polymorphism.

For instance:

  • In Java you need to implement Cloneable, who defines method Cloneable.clone to have cloneble objects. Then, the compiler knows your object will understand that method (otherwise it will throw an error)
  • In smalltalk, you just need to implement method #clone. Compiler never knows/don't care about which messages understands your objects until it is called.

That also means you can have polymorphic objects without being part of same hierarchy... multi inheritance, mixins and other approachs (traits are present on Pharo) are just reuse technics, not a design constraint.

This way of do things is often called "duck typing"... see: http://en.wikipedia.org/wiki/Duck_typing

like image 34
EstebanLM Avatar answered Oct 13 '22 15:10

EstebanLM