Abstract Classes & Inheritance

Java Definition + Dart Example

I’ve used abstract classes during my projects, but I figured I need to be able to describe what abstract classes and interfaces are with words, so here goes. The explanation here refers to how these concepts are used in Java.

Abstract Classes

Abstract classes cannot be used to create objects (must be inherited). It can have regular methods (those that have a body) and abstract methods, which do not have body. It cannot contain constructors, because like interfaces, it cannot be used to create objects.

https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html#:~:text=An%20abstract%20class%20is%20a,but%20they%20can%20be%20subclassed.&text=When%20an%20abstract%20class%20is,methods%20in%20its%20parent%20class.

To access abstract classes, you extend it.

https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html#:~:text=An%20abstract%20class%20is%20a,but%20they%20can%20be%20subclassed.&text=When%20an%20abstract%20class%20is,methods%20in%20its%20parent%20class.

Interfaces

Dart does not explicitly provide the interface keyword but all classes implicitly define an interface. More on this later, but let’s explore what interface means in other languages.

In Java, “interface is a group of related methods with empty bodies.” Notice that it said methods, and not variables. By default, interface methods are abstract and public.

https://docs.oracle.com/javase/tutorial/java/concepts/interface.html

To access the interface, you must implement it, unlike abstract classes which extends. Implementing an interface means that all methods defined in the interface will appear in the class. And unlike abstract classes, you can implement multiple interfaces.

When to Use Abstract Classes v. Interface

Abstract Class

  • When you want to share code among several closely related classes
  • When you need fields or methods that are private, non-static, or non-final

Interface

  • When unrelated classes might implement the interface
  • When you want to specify a behavior of a particular data type
  • When you want to inherit multiple interfaces

Implements v. Extends

Implements is used for interfaces, whereas extends is used for abstract classes. Below will be how these concepts are used in Dart.

Extends

You can use super to refer to the parent class and use its objects. It needs to be invoked in a function

https://dart.dev/guides/language/language-tour#implicit-interfaces

You can use override to change parent class’ member, but it must match in several ways:

  1. Return type and argument types must be the same type/subtype
  2. There must be the same amount of parameters
  3. A generic method (<T>) cannot override a non-generic and vice versa

You can (but rarely) use covariant if you want to narrow the type of a method parameter/instance variable, but you need to make sure type error won’t occur at runtime. That means that whatever covariant you use, should be the subtype of its counterpart.

https://dart.dev/guides/language/sound-problems#the-covariant-keyword

Implements

As said above, all the methods in the parent class need to exist in the subclass. Unlike Java, implements isn’t limited to interface, so think(?) abstract classes for implementation can contain variables. I would use implements when I want the subclasses to have ALL of the methods and variables in the parent class.

Here’s my example.

Abstract Class v. Class

There is one major different when it comes to inheritance between an abstract class and a normal class. When abstract classes are extended, you NEED to implement its methods.

In conclusion, I’m still confused 😕

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store