filmov
tv
✔ Abstract Class & Methods / Abstraction In Java OOP | (Video 118)

Показать описание
Abstract Class & Methods / Abstraction In Java OOP
Social Media Contact
Transcript
An abstract class is a class with empty methods and optional concrete methods. The empty method is an abstract method and the concrete method is an instance method with fully defined details. In this example, we see 2 diagrams: a square and a rectangle.
To calculate the perimeter, we can define one generic abstract method in the superclass then let the subclass for square and subclass for rectangle provide details on how to calculate the perimeter. The calculations for square are 4 times the side and rectangle is 2 times length plus 2 times width. It’s a difference because the square is the same on each side but rectangle has a longer length than width.
Now there are 2 errors, one at the class level and another one at the method level. Hover the method and we see “The abstract method calculatePerimeter in type Shape can only be defined by an abstract class”. If we have one or more abstract methods then we must make the class abstract. That’s why we see the following quick fixes: Remove abstract modifier and Make type Shape abstract. Hover the class and we see “The type Shape must be an abstract class to define abstract methods” with 1 quick fix Make type Shape abstract. Let’s re-add abstract. Finally, the errors go away.
Recall an abstract class is a class with an empty method which means the method is abstract just like calculatePerimeter and it is not required to have a defined method meaning the method has a body. Let’s add a defined method which is an instance method
public void printShapeMessage () {
}
If we are not going to implement the abstract method then we must make the Square class abstract because the superclass contains an abstract method. Select add unimplemented methods and we see the Override annotation which indicates the calculatePerimeter method is overridden. Remove the comment then implement the details. private int side = 10. Method details are int perimeter = 4 * side; / sysout(“Square Perimeter = “ + perimeter) / return perimeter;
The Rectangle class is similar to the Square class but has different details for the calculatePerimeter method. Rectangle extends Shape
private int length = 5; / private int width = 10; / @Override /
public int calculatePerimeter () {
int perimeter = (2 * length) + (2 * width);
return perimeter;
If necessary, you can also create a constructor in an abstract class. That’s it for creating an abstract class and abstract methods in a superclass with a subclass implementing the details. Now, let’s test out this code in the ShapeTest class. Write main CTRL + SPACE for the shortcut.
With abstraction, we cannot create an object of abstract classes. Our abstract class is Shape, let’s add shape as an object = new Shape ();. A compiler error states “Cannot instantiate the type Shape”. Comment this line // and say Abstract Classes Cannot Create An Object. There are no objects of an abstract class because abstract classes do not define a complete implementation.
However, we will not get an error by declaring a reference of the abstract class Shape shape1 = new Square (); or Shape shape2 = new Rectangle ();
#Java #Abstraction #Encapsulation #Inheritance #Polymorphism
Social Media Contact
Transcript
An abstract class is a class with empty methods and optional concrete methods. The empty method is an abstract method and the concrete method is an instance method with fully defined details. In this example, we see 2 diagrams: a square and a rectangle.
To calculate the perimeter, we can define one generic abstract method in the superclass then let the subclass for square and subclass for rectangle provide details on how to calculate the perimeter. The calculations for square are 4 times the side and rectangle is 2 times length plus 2 times width. It’s a difference because the square is the same on each side but rectangle has a longer length than width.
Now there are 2 errors, one at the class level and another one at the method level. Hover the method and we see “The abstract method calculatePerimeter in type Shape can only be defined by an abstract class”. If we have one or more abstract methods then we must make the class abstract. That’s why we see the following quick fixes: Remove abstract modifier and Make type Shape abstract. Hover the class and we see “The type Shape must be an abstract class to define abstract methods” with 1 quick fix Make type Shape abstract. Let’s re-add abstract. Finally, the errors go away.
Recall an abstract class is a class with an empty method which means the method is abstract just like calculatePerimeter and it is not required to have a defined method meaning the method has a body. Let’s add a defined method which is an instance method
public void printShapeMessage () {
}
If we are not going to implement the abstract method then we must make the Square class abstract because the superclass contains an abstract method. Select add unimplemented methods and we see the Override annotation which indicates the calculatePerimeter method is overridden. Remove the comment then implement the details. private int side = 10. Method details are int perimeter = 4 * side; / sysout(“Square Perimeter = “ + perimeter) / return perimeter;
The Rectangle class is similar to the Square class but has different details for the calculatePerimeter method. Rectangle extends Shape
private int length = 5; / private int width = 10; / @Override /
public int calculatePerimeter () {
int perimeter = (2 * length) + (2 * width);
return perimeter;
If necessary, you can also create a constructor in an abstract class. That’s it for creating an abstract class and abstract methods in a superclass with a subclass implementing the details. Now, let’s test out this code in the ShapeTest class. Write main CTRL + SPACE for the shortcut.
With abstraction, we cannot create an object of abstract classes. Our abstract class is Shape, let’s add shape as an object = new Shape ();. A compiler error states “Cannot instantiate the type Shape”. Comment this line // and say Abstract Classes Cannot Create An Object. There are no objects of an abstract class because abstract classes do not define a complete implementation.
However, we will not get an error by declaring a reference of the abstract class Shape shape1 = new Square (); or Shape shape2 = new Rectangle ();
#Java #Abstraction #Encapsulation #Inheritance #Polymorphism