Real scenario

In this example, Shape is the abstract class, and its implementation is provided by the Rectangle and Circle classes. Mostly, we don't know about the implementation class (which is hidden to the end user), and an object of the implementation class is provided by the factory method. A factory method is a method that returns the instance of the class. We will learn about the factory method later. In this example, if you create the instance of Rectangle class, draw() method of Rectangle class will be invoked.

File: TestAbstraction1.java


\begin{lstlisting}
abstract class Shape{
abstract void draw();
}
//In real s...
...ded through method, e.g., getShape() method
s.draw();
}
}
\end{lstlisting}

Output


\begin{lstlisting}
drawing circle
\end{lstlisting}