SKP's Algorithms and Data Structures #8: Java Problem: Simple Inheritance (OOPs)
This Article Series Focuses on Algorithms, Data Structures, or Applying them to Problem Solving. In this Article, Let us Revisit Simple Object-Oriented Principle of Inheritance.
Join the DZone community and get the full member experience.Join For Free
Java Inheritance / Simple OOPs [www.techgig.com]
Create Two Classes:
The Rectangle class should have two data fields-width and height of int types. The class should have
display()method, to print the width and height of the rectangle separated by space.
The RectangleArea class is Derived from Rectangle class, i.e., it is the Sub-Class of Rectangle class. The class should have
read_input()method, to Read the Values of width and height of the Rectangle. The RectangleArea class should also Overload the
display()Method to Print the Area (width*height) of the Rectangle.
The First and Only Line of Input contains two space-separated Integers denoting the width and height of the Rectangle.
1 <= width,height <= 10^3
The Output Should Consist of Exactly Two Lines.
In the First Line, Print the Width and Height of the Rectangle Separated by Space.
In the Second Line, Print the Area of the Rectangle.
[Explanation of the Solution]
This is the Simplest of all OOPs Questions! Demonstration of Inheritance and Overriding (Very Loosely, Liskov Substitution of SOLID).
Happy Problem Solving using Java!
Published at DZone with permission of Sumith Puri, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.