‫Welcome back.
‫In this video, we're going to solve a very important exercise about abstraction and inheritance in
‫Java.
‫Think about a program that allows us to calculate the area of a circle, rectangle or other shapes.
‫First of all, I'll start as a beginner trying to create and solve this exercise.
‫I'll start with creating the circle and this class will have the radius.
‫Then I'll create a constructor Alt+ insert in the constructor and I'll create a method here called Calculate
‫area and it will do the calculation.
‫So 3.14, which is pi times radius squared and you can write like this times radius times radius.
‫Let me create another method that allows us to calculate the perimeter of this circle in the same way
‫it returns a double and calculate perimeter here and returns two times pi times radius.
‫This is how we create a circle class with double as a variable radius and we calculate the area and
‫the perimeter of this circle.
‫Let me create another shape called Rectangle.
‫The rectangle has length and width as variable and I'll create the constructor with the length and width.
‫So whenever I need to create a rectangle instance, I need to pass the length and the width.
‫Now I'll create two methods that calculate area and calculate perimeter of this rectangle.
‫The area of the rectangle is length times width, and the perimeter of the rectangle is two times length
‫plus width.
‫Now let's move to the My class.
‫This is the tester class, so I need to create a circle C equal to new circle and I need to pass the
‫radius radius is 5.0 and I need to create a rectangle instance called R equal to new rectangle.
‫I need to pass the length which is 4.0 and the width is six point z.
‫Now let's print the area and the perimeter for both the rectangle and the circle.
‫I created these four messages circle area, circle, perimeter rectangle area and rectangle perimeter,
‫and I'm calling the respective methods.
‫Let's run our program and here we go.
‫The program is working fine area is calculated for both rectangle and circle and also the perimeter.
‫But there is a very important concept.
‫You should know if you need to create another shape, like, for example, triangle.
‫And by mistake you didn't define the calculate perimeter.
‫You define everything good, like the height base, and you create the constructor and you create the
‫area.
‫But the triangle is a shape that should contain the calculate area and calculate perimeter method.
‫And by mistake, you didn't provide it.
‫You didn't provide the calculate perimeter.
‫So what should I do?
‫If we look among these three classes that we define, there are common codes, like, for example,
‫the calculate area, calculate perimeter, but different approaches like the area calculation of the
‫circle is different from the rectangle and different from the triangle and so on.
‫There is a common concept to calculate the perimeter and calculate the area why we are making.
‫And for every concrete class, the same methods again and again.
‫And we need to always use the area and perimeter calculation in every shape how to create it in a better
‫approach.
‫The trick here is to create a new interface or an abstract class called shape.
‫But I prefer shape if you need to define only the methods that should be forced in to implement in every
‫class here, calculate, calculate area and calculate perimeter.
‫Now every class implements this shape should implement and be forced to implement the calculate area
‫and calculate perimeter methods.
‫Let's take the example of triangle implements the shape interface Alt+ Enter implement the method and
‫you can see that this shape tells us that you missed the calculate perimeter.
‫Here we go.
‫And for the circle I need to implement the shape and you can see that the circle implements, calculate
‫area and calculate perimeter.
‫Exactly.
‫The rectangle is the same.
‫You can see there is no error here.
‫And you can notice that I can I which is the interface implements method in shape.
‫So calculate area is a method that is implemented from the shape.
‫Now if I need to create another.
‫Shape.
‫So, for example, square and this shape square implements shape.
‫So I don't need to take care about what methods that I should make here.
‫Alt+ Enter.
‫Implement the methods with one click, you can see that calculate area and calculate perimeter methods
‫are forced to be implemented here in this square.
‫This allows for a flexible and maintainable code base as you can easily add more shapes by implementing
‫the shape interface without modifying existing code.