0% found this document useful (0 votes)
7 views

Class-13 - Abstract Classes

The document discusses abstract classes and abstract methods in object oriented programming. Abstract classes cannot be instantiated directly but are meant to be inherited from, allowing subclasses to provide concrete implementations of abstract methods. The document provides examples of defining abstract classes and methods, and overriding them in subclasses.

Uploaded by

Arzu Akhundov
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Class-13 - Abstract Classes

The document discusses abstract classes and abstract methods in object oriented programming. Abstract classes cannot be instantiated directly but are meant to be inherited from, allowing subclasses to provide concrete implementations of abstract methods. The document provides examples of defining abstract classes and methods, and overriding them in subclasses.

Uploaded by

Arzu Akhundov
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Abstract Classes and Abstract methods

Abstract classes
Sometimes we have to work with entities that don't have a specific implementation. For
example, the entity "animal". There are specific animals - a cat, a dog, and so on, but the
animal as such does not have a specific incarnation. Or the entity "geometric figure". There
is a rectangle, square, circle, triangle, but the geometric figure itself also does not have a
specific embodiment. And usually abstract classes are used to describe such entities.

Abstract classes are defined with the abstract keyword . For example, let's define an
abstract geometric shape class:

abstract class Shape {


}
Abstract classes are similar to ordinary classes (can also define fields, methods,
constructors) except that we cannot directly create an object of an abstract class using its
constructor.

abstract class Shape { }


void main (){

Shape shape = Shape();// ! Not correct form


}
As a rule, abstract classes declare some common functionality that inherited classes
implement in their own way. For example, a geometric figure class may have methods for
calculating perimeter, area, and so on.
abstract class Shape {
void calculateArea() {
print("Not Implemented");
}
}
class Rectangle extends Shape {
int width;
int height;
Rectangle(this.width, this.height);

@override
void calculateArea() {
int area = width * height;
print("area = $area");
}
}

void main() {
Shape rect = Rectangle(20, 30);
rect.calculateArea(); // area = 600

Here, the geometric figure abstract class defines a method calculateArea()that prints the
area of the figure to the console. The rectangle class defines its implementation for this
method.

Abstract Methods
In the example above, the calculateArea method in the Shape base class doesn't do any
useful work because an abstract shape can't have an area. And in this case, such a method
is better defined as abstract:
abstract class Shape {
void calculateArea();
}

class Rectangle extends Shape {


int width;
int height;
Rectangle(this.width, this.height);

@override
void calculateArea() {
int area = width * height;
print("area = $area");
}
}

void main() {
Shape rect = Rectangle(20, 30);
rect.calculateArea(); // area = 600
}

An abstract method is defined in the same way as a regular method, but instead of the
body of the method, after the list of parameters, there is a semicolon:
void calculateArea();.

It is important to note that abstract methods can only be defined in abstract classes. In
addition, if the base class defines an abstract method, then the descendant class must
implement it, that is, define the body of the method.

Activity №5
Variant-1
Create parent class Car with properties: Name, Maximum speed (in km/h). Define 2 virtual
methods: "Cost" method - the cost of the car, calculated by the formula = Maximum speed
* 100 and the "Model update" method, which increases the maximum speed by 10. Also
define the "Info" method, which prints a string containing information about the object:
Name, Maximum speed and Cost.
Create also a child class Executive car, in which to override the methods: the "Cost"
method returns the number = Maximum speed * 250, and the "Model update" method
increases the speed by 5 km/h.
In the main program, create an object of class Car with a maximum speed of 140 km/h
and an object of class Executive car with a maximum speed of 160 km/h.
Display vehicle information. Update car models and display information about them again.

Variant-2
Create an Aircraft parent class with properties: Make, Model, Maximum speed (in km/h),
Maximum altitude (in meters). Define the virtual method "Cost" - the cost of the aircraft,
calculated by the formula (Maximum speed * 1000 + Maximum altitude * 100)
Also define an <Info> method that prints a string containing information about the object:
Make, Model, Max Speed, Max Height, and Cost.
Also, create a child class Bomber, in which to override the "Cost" method, which will return
twice the cost relative to the formula for the Aircraft class. Also create a class Fighter - the
child class of the Aircraft class, for which override the "Cost" method as a triple cost,
relative to the formula
aircraft cost.

You might also like