Learn Python ABSTRACT CLASSES in 7 minutes! 👻

preview_player
Показать описание
# Abstract class: A class that cannot be instantiated on its own; Meant to be subclassed.
# They can contain abstract methods, which are declared but have no implementation.
# Abstract classes benefits:
# 1. Prevents instantiation of the class itself
# 2. Requires children to use inherited abstract methods

from abc import ABC, abstractmethod

class Vehicle(ABC):

@abstractmethod
def go(self):
pass

@abstractmethod
def stop(self):
pass

class Car(Vehicle):

def go(self):
print("You drive the car")

def stop(self):
print("You stop the car")

class Motorcycle(Vehicle):

def go(self):
print("You ride the motorcycle")

def stop(self):
print("You stop the motorcycle")

class Boat(Vehicle):

def go(self):
print("You sail the boat")

def stop(self):
print("You anchor the boat")

car = Car()
motorcycle = Motorcycle()
boat = Boat()
Рекомендации по теме
Комментарии
Автор

# Abstract class: A class that cannot be instantiated on its own; Meant to be subclassed.
# They can contain abstract methods, which are declared but have no implementation.
# Abstract classes benefits:
# 1. Prevents instantiation of the class itself
# 2. Requires children to use inherited abstract methods

from abc import ABC, abstractmethod

class Vehicle(ABC):

@abstractmethod
def go(self):
pass

@abstractmethod
def stop(self):
pass

class Car(Vehicle):

def go(self):
print("You drive the car")

def stop(self):
print("You stop the car")

class Motorcycle(Vehicle):

def go(self):
print("You ride the motorcycle")

def stop(self):
print("You stop the motorcycle")

class Boat(Vehicle):

def go(self):
print("You sail the boat")

def stop(self):
print("You anchor the boat")

car = Car()
motorcycle = Motorcycle()
boat = Boat()

BroCodez
Автор

Bro Code pls do C++ full course with Unreal Engine (making a game)

RayTheBos
Автор

am just going from one tutorial to other without achieving any thing

christofff
Автор

Questions:
1. Do we need to inherit from ABC? Isn't it enough if the class contains a few abstract methods?
2. Do all the methods in an abstract class need to be abstract? Can we have some methods implemented and others marked as abstract? For example, all the children classes can share some part of the initialization, so writing it anew in each child class violates the DRY principle.

victorsago
Автор

Why would we use abstract classes? why not just directly make classes?

IGCSENERD-upyv
Автор

is this really necessary, can't i just put a comment next to the class saying that its abstract?

voidwitha
Автор

Like if you want bro code to do a rust series 👇

Swxfty_
Автор

Bro Code pls do C++ full course with Unreal Engine (making a game)

Master-code
Автор

Bro Code pls do C++ full course with Unreal Engine (making a game)

Master-code
Автор

Bro Code pls do C++ full course with Unreal Engine (making a game)

Master-code
Автор

Bro Code pls do C++ full course with Unreal Engine (making a game)

Master-code