Interface and Abstraction in java

from Head First Java chapter 08

As you know an OOP concept is interconnected with each other oop concepts. Based on this, this is chapter is talking about the interface and abstract class.

Inheritance makes polymorphism .abstrct class and interfaces help to achieve serious polymorphism and Abstraction.

Abstract class

Abstract method

A method with an abstract keyword is known as the abstract method. The method can not have an implementation and ends with a semicolon.

An abstract method can not be in a non-abstract class.

simple class diagram

based on the above class diagram:

The shape is an abstract class and it has instance methods getColor() and abstract method area(). The Circle and Triangle are subclasses of the Shape class

When you extend the Shape class, the subclasses must have to implement the method area() while the getColor() method can be overridden if you want.

You can not create a new instance directly from the abstract class but can create array elements as objects.

Interface

In java abstraction can be achieved in two ways,

From the above class diagram: if the shape class is interfaced out code looks like

code for showing how the keyword works

Here, the Circle class implements the Shape interface and have an implementation from the method area().

--

--

Software Engineer at Virtusa

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