# 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.

· Java Zone · Code Snippet
Save
2.09K Views
[Question/Problem Statement is the Property of Techgig]

Java Inheritance / Simple OOPs [www.techgig.com]
Create Two Classes:

BaseClass
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.

DerivedClass
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.

Input Format
The First and Only Line of Input contains two space-separated Integers denoting the width and height of the Rectangle.

Constraints
1 <= width,height <= 10^3

Output Format
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).

[Source Code, Sumith Puri (c) 2021 — Free to Use and Distribute]
Java

x
67

1
` /*    `
2
`  * Techgig Core Java Basics Problem - Get Simple OOPs Right!  `
3
`  * Author: Sumith Puri [I Bleed Java!]; GitHub: @sumithpuri;  `
4
`  */   `
5
`     `
6
`  import java.io.*;   `
7
`  import java.util.*;   `
8
`   `
9
`   `
10
`  class Rectangle {  `
11
`   `
12
`    private int width;  `
13
`    private int height;  `
14
`   `
15
`    public void display() {  `
16
`   `
17
`      System.out.println(width + " " + height);  `
18
`    }  `
19
`   `
20
`    public int getWidth() {  `
21
`   `
22
`      return width;  `
23
`    }  `
24
`   `
25
`    public void setWidth(int width) {  `
26
`   `
27
`      this.width=width;  `
28
`    }  `
29
`   `
30
`    public int getHeight() {  `
31
`   `
32
`      return height;  `
33
`    }  `
34
`   `
35
`    public void setHeight(int height) {  `
36
`   `
37
`      this.height=height;  `
38
`    }  `
39
`  }  `
40
`   `
41
`  class RectangleArea extends Rectangle {  `
42
`   `
43
`    public void read_input() {  `
44
`   `
45
`     Scanner scanner = new Scanner (System.in);   `
46
`       `
47
`     setWidth(scanner.nextInt());   `
48
`     setHeight(scanner.nextInt());  `
49
`    }  `
50
`   `
51
`    public void display() {  `
52
`   `
53
`      super.display();  `
54
`      System.out.println(getWidth()*getHeight());  `
55
`    }  `
56
`  }  `
57
`     `
58
`  public class CandidateCode {   `
59
`     `
60
`   public static void main(String args[] ) throws Exception {   `
61
`     `
62
`     RectangleArea rectangleArea = new RectangleArea();  `
63
`     rectangleArea.read_input();  `
64
`   `
65
`     rectangleArea.display();  `
66
`   }   `
67
` } `

Happy Problem Solving using Java!

Topics:
techgig, core java, algorithms, data structures, problem solving, mathematical programming, complexity metrics, iq, competitive, java program

Published at DZone with permission of Sumith Puri, DZone MVB.

Opinions expressed by DZone contributors are their own.