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

Unit 4

unit 4 description
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Unit 4

unit 4 description
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

UNIT – 4

OBJECT ORIENTED PROGRAMMING

CLASSES AND OBJECTS

In Python, object-oriented
oriented Programming (OOPs) is a programming paradigm that uses objects
and classes in programming. It aims to implement real-worldreal world entities like inheritance,
polymorphisms, encapsulation, etc. in the programming. The main concept of OOPs is to bind
the data and the functions that work on that together as a single unit so that no other part of the
code can access this data.

OOPs Concepts in Python


 Class
 Objects
 Polymorphism
 Encapsulation
 Inheritance
 Data Abstraction

Python Class

A class is a collection of objects. A class contains the blueprints or the prototype from which
the objects are being created. It is a logical entity that contains some attributes and methods.
 Classes are created by keyword class.
 Attributes are the variables that belong to a class.
 Attributes are always public and can be accessed using the dot (.) operator. Eg.:
Myclass.Myattribute
Class Definition Syntax:
class ClassName:
# Statement-1
# Statement-N

Creating an Empty Class in Python

# Python3 program to # demonstrate defining# a class

class Dog:

pass

Python Objects

The object is an entity that has a state and behavior associated with it. It may be any real-world
object like a mouse, keyboard, chair, table, pen, etc. Integers, strings, floating-point numbers,
even arrays, and dictionaries, are all objects.

An object consists of:


 Identity: It gives a unique name to an object and enables one object to interact with other
objects.
 State: It is represented by the attributes of an object. It also reflects the properties of an
object.
 Behavior: It is represented by the methods of an object. It also reflects the response of an
object to other objects.
To understand the state, behavior, and identity let us take the example of the class dog
(explained above).
 The identity can be considered as the name of the dog.
 State or Attributes can be considered as the breed, age, or color of the dog.
 The behavior can be considered as to whether the dog is eating or sleeping.
Creating an Object

This will create an object named obj of the class Dog defined above.
Example:
obj = Dog()

The Python self

1. Class methods must have an extra first parameter in the method definition. We do not give
a value for this parameter when we call the method, Python provides it
2. If we have a method that takes no arguments, then we still have to have one argument.

Self represents the instance of the class. By using the “self” we can access the attributes and
methods of the
he class in python. It binds the attributes with the given arguments.

Program

#it is clearly seen that self and obj is referring to the same object

class check:
def __init__(self):
print("Address of self = ",id(self))

obj = check()
print("Address of class object = ",id(obj))
Output:
Address of self = 140124194801032
Address of class object = 140124194801032

# Write Python3 code here

class car():

# init method or constructor


def __init__(self, model, color):
self.model = model
self.color = color

def show(self):
print("Model is", self.model )
print("color is", self.color )

# both objects have different self which


# contain their attributes
audi = car("audi a4", "blue")
ferrari = car("ferrari 488", "green")

audi.show() # same output as car.show(audi)


ferrari.show() # same output as car.show(ferrari)

#note:we can also do like this


print("Model for audi is ",audi.model)
print("Colour for ferrari is ",ferrari.color)
#this happens because after assigning in the constructor the attributes are linked to that
particular object
#here attributes(model,colour) are linked to objects(audi,ferrari) as we initialize them
# Behind the scene, in every instance method
# call, python sends the instances also with
# that method call like car.show(audi)

Output:

Model is audi a4
color is blue
Model is ferrari 488
color is green

The Python __init__ Method

The __init__ method is similar to constructors in C++ and Java. It is run as soon as an object of
a class is instantiated. The method is useful to do any initialization you want to do with your
object. Now let us define a class and create some objects using the self and __init__ method.

Creating a class and object with class and instance attributes

class Dog:

# class attribute

attr1 = "mammal"

# Instance attribute

def __init__(self, name):

self.name = name

# Driver code

# Object instantiation

Rodger = Dog("Rodger")

Tommy = Dog("Tommy")

# Accessing class attributes

print("Rodger is a {}".format(Rodger.__class__.attr1))

print("Tommy is also a {}".format(Tommy.__class__.attr1))

# Accessing instance attributes

print("My name is {}".format(Rodger.name))


print("My name is {}".format(Tommy.name))

Output:

Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy

Here, The Dog class is defined with two attributes:


 attr1 is a class attribute set to the value “mammal”. Class attributes are shared by all
instances of the class.

 __init__ is a special
cial method (constructor) that initializes an instance of the Dog class. It
takes two parameters: self (referring to the instance being created) and name (representing
the name of the dog). The name parameter is used to assign a name attribute to each
instance
ance of Dog.
Creating Classes and objects with methods

The speak method is defined within the Dog class. This method prints a string that includes
the name of the dog instance.
class Dog:

# class attribute

attr1 = "mammal"

# Instance attribute

def __init__(self, name):

self.name = name

def speak(self):

print("My name is {}".format(self.name))

# Driver code

# Object instantiation

Rodger = Dog("Rodger")

Tommy = Dog("Tommy")

# Accessing class methods

Rodger.speak()

Tommy.speak()

Output:

My name is Rodger
My name is Tommy

CONSTRUCTOR IN PYTHON
Constructors are generally used for instantiating an object. The task of constructors is to
initialize(assign values) to the data members of the class when an object of the class is created.
In Python the __init__() method is called the constructor and is always called when an object is
created.
Syntax of constructor declaration :
def __init__(self):
# body of the constructor
Types of constructors :

 default constructor: The default constructor is a simple constructor which doesn’t accept
any arguments. Its definition has only one argument which is a reference to the instance
being constructed.

 parameterized constructor: constructor with parameters is known as parameterized


constructor. The parameterized constructor takes its first argument as a reference to the
instance being constructed known as self and the rest of the arguments are provided by the
programmer.

class fruit:

# default constructor
def __init__(self):
self.fruit = "apple"

# a method for printing data members


def print_fruit(self):
print(self.fru)

# creating object of the class


obj = fruit()

# calling the instance method using the object obj


obj.print_fruit()

Output:
Apple

PARAMETERIZED CONSTRUCTOR
class Concat_Str:
first = 0
second = 0
answer = 0

# parameterized constructor
def __init__(self, f, s):
self.first = f
self.second = s

def display(self):
print("First string = " + (self.first))
print("Second string = " + (self.second))
print("Concatination of two strings = " + (self.answer))

def calculate(self):
self.answer = self.first + self.second

# creating object of the class


# this will invoke parameterized constructor
obj1 = Concat_Str (“Good”, “Morning”)

# creating second object of same class


obj2 = Addition(“Welcome”, “Home”)

# perform Addition on obj1


obj1.calculate()

# perform Addition on obj2


obj2.calculate()

# display result of obj1


obj1.display()

# display result of obj2


obj2.display()

Output:
First string = 1000
Second string = 2000
Concatenation of two string = 3000
First string = 10
Second string = 20
Concatenation of two string = 30

DESTRUCTOR IN PYTHON
Destructors are called when an object gets destroyed. Python has a garbage collector that
handles memory management automatically.
The __del__() method is a known as a destructor method in Python. It is called when all
references to the object have been deleted i.e when an object is garbage collected.
Syntax of destructor declaration :

def __del__(self):
# body of destructor
Program
# Python program to illustrate destructor
class Employee:
# Initializing
def __init__(self):
print('Employee created.')
# Deleting (Calling destructor)
def __del__(self):
print('Destructor called, Employee deleted.')
obj = Employee()
del obj

Output
Employee created.
Destructor called, Employee deleted.
GETTER AND SETTER METHOD
In Python, getters and setters are not the same as those in other object-oriented programming
languages. Basically, the main purpose of using getters and setters in object-oriented programs
is to ensure data encapsulation.
In python Getters and Setters are often used

1. Add validation logic around getting and setting a value.


2. To avoid direct access of a class field i.e. private variables cannot be accessed
directly or modified by external user.

Using normal function to achieve getters and setters behavior


# Python program showing a use
# of get() and set() method in
# normal function
class person_age:
def __init__(self, age = 0):
self._age = age
# getter method
def get_age(self):
return self._age

# setter method
def set_age(self, x):
self._age = x
raj = person_age ()

# setting the age using setter


raj.set_age(21)
# retrieving age using getter
print(raj.get_age())
print(raj._age)

Output:
21
21
In above code functions get_age() and set_age() acts as normal functions and doesn’t play any
impact as getters and setters, to achieve such functionality Python has a special
function property().

Using property() function to achieve getters and setters behavior

In Python property()is a built-in function that creates and returns a property object. A property
object has three methods, getter(), setter(), and delete(). property() function in Python has four
arguments property(fget, fset, fdel, doc), fget is a function for retrieving an attribute
value. fset is a function for setting an attribute value. fdel is a function for deleting an attribute
value. doc creates a docstring for attribute. A property object has three
methods, getter(), setter(), and delete() to specify fget, fset and fdel individually. For Example

# Python program showing a


# use of property() function

class person_age:
def __init__(self):
self._age = 0

# function to get value of _age


def get_age(self):
print("getter method called")
return self._age

# function to set value of _age


def set_age(self, a):
print("setter method called")
self._age = a
# function to delete _age attribute
def del_age(self):
del self._age

age = property(get_age, set_age, del_age)

mark = person_age()

mark.age = 10

print(mark.age)

ENCAPSULATION

Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It


describes the idea of wrapping data and the methods that work on data within one unit. This
puts restrictions on accessing variables and methods directly and can prevent the accidental
modification of data.

A class is an example of encapsulation as it encapsulates all the data that is member functions,
variables, etc. The goal of information hiding is to ensure that an object’s state is always valid
by controlling access to attributes that are hidden from the outside world.
PROTECTED MEMBERS

Protected members (in C++ and JAVA) are those members of the class that cannot be accessed
outside the class but can be accessed from within the class and its subclasses. To accomplish
this in Python, just follow the convention by prefixing the name of the member by a single
underscore “_”.

# Python program to

# demonstrate protected members

# Creating a base class

class Base:

def __init__(self):

# Protected member

self._a = 2

# Creating a derived class

class Derived(Base):

def __init__(self):

# Calling constructor of

# Base class

Base.__init__(self)

print("Calling protected member of base class: ",

self._a)

# Modify the protected variable:

self._a = 3
print("Calling modified protected member outside class: ",

self._a)

obj1 = Derived()

obj2 = Base()

# Calling protected member

# Can be accessed but should not be done due to convention

print("Accessing protected member of obj1: ", obj1._a)

# Accessing the protected variable outside

print("Accessing protected member of obj2: ", obj2._a)

Output:

Calling protected member of base class: 2


Calling modified protected member outside class: 3
Accessing protected member of obj1: 3
Accessing protected member of obj2: 2

PRIVATE MEMBERS

Private members are similar to protected members, the difference is that the class
members declared private should neither be accessed outside the class nor by any base
class. In Python, there is no existence of Private instance variables that cannot be accessed
except inside a class.

# Python program to
# demonstrate private members

# Creating a Base class

class Base:

def __init__(self):

self.a = "GeeksforGeeks"

self.__c = "GeeksforGeeks"

# Creating a derived class

class Derived(Base):

def __init__(self):

# Calling constructor of

# Base class

Base.__init__(self)

print("Calling private member of base class: ")

print(self.__c)

# Driver code

obj1 = Base()

print(obj1.a)

# Uncommenting print(obj1.c) will

# raise an AttributeError

# Uncommenting obj2 = Derived() will


# also raise an AttributeError as

# private member of base class

# is called inside derived class

Output:

GeeksforGeeks
Traceback (most recent call last):
File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in <module>
print(obj1.c)
AttributeError: 'Base' object has no attribute 'c'

Traceback (most recent call last):


File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in <module>
obj2 = Derived()
File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__
print(self.__c)
AttributeError: 'Derived' object has no attribute '_Derived__c'

INHERITANCE

One of the core concepts in object-oriented programming (OOP) languages is inheritance.


It is a mechanism that allows you to create a hierarchy of classes that share a set of
properties and methods by deriving a class from another class. Inheritance is the
capability of one class to derive or inherit the properties from another class.

Benefits of inheritance are:


Inheritance allows you to inherit the properties of a class, i.e., base class to another, i.e.,
derived class. The benefits of Inheritance in Python are as follows:
 It represents real-world relationships well.
 It provides the reusability of a code. We don’t have to write the same code again and
again. Also, it allows us to add more features to a class without modifying it.
 It is transitive in nature, which means that if class B inherits from another class A, then all
the subclasses of B would automatically inherit from class A.
 Inheritance offers a simple, understandable model structure.
 Less development and maintenance expenses result from an inheritance.

Syntax

Class BaseClass:
{Body}
Class DerivedClass(BaseClass):
{Body}

Creating a Parent Class

A parent class is a class whose properties are inherited by the child class. Let’s create a parent
class called Person which has a Display method to display the person’s information.

# A Python program to demonstrate inheritance

class Person(object):

# Constructor

def __init__(self, name, id):

self.name = name

self.id = id

# To check if this person is an employee

def Display(self):

print(self.name, self.id)

# Driver code
emp = Person("Satyam", 102) # An Object of Person

emp.Display()

Creating a Child Class

A child class is a class that drives the properties from its parent class. Here Emp is another
class that is going to inherit the properties of the Person class (base class).

class Emp(Person):

def Print(self):

print("Emp class called")

Emp_details = Emp("Mayank", 103)

# calling parent class function

Emp_details.Display()

# Calling child class function

Emp_details.Print()

Output:
Mayank 103
Emp class called

The super() Function

The super() function is a built-in function that returns the objects that represent the parent
class. It allows to access the parent class’s methods and attributes in the child class.

Example:

# parent class

class Person():
def __init__(self, name, age):

self.name = name

self.age = age

def display(self):

print(self.name, self.age)

# child class

class Student(Person):

def __init__(self, name, age):

self.sName = name

self.sAge = age

# inheriting the properties of parent class

super().__init__("Rahul", age)

def displayInfo(self):

print(self.sName, self.sAge)

obj = Student("Mayank", 23)

obj.display()

obj.displayInfo()

Output:
Rahul 23
Mayank 23
Adding Properties

One of the features that inheritance provides is inheriting the properties of the parent class as
well as adding new properties of our own to the child class. Let us see this with an example:

# parent class

class Person():

def __init__(self, name, age):

self.name = name

self.age = age

def display(self):

print(self.name, self.age)

# child class

class Student(Person):

def __init__(self, name, age, dob):

self.sName = name

self.sAge = age

self.dob = dob

# inheriting the properties of parent class

super().__init__("Rahul", age)

def displayInfo(self):

print(self.sName, self.sAge, self.dob)

obj = Student("Mayank", 23, "16-03-2000")

obj.display()

obj.displayInfo()

Output:
Rahul 23
Mayank 23 16-03-2000

Different types of Python Inheritance

There are 5 different types of inheritance in Python. They are as follows:


 Single inheritance: When a child class inherits from only one parent class, it is called
single inheritance. We saw an example above.
 Multiple inheritances: When a child class inherits from multiple parent classes, it is called
multiple inheritances.
 Multilevel inheritance: When we have a child and grandchild relationship. This means
that a child class will inherit from its parent class, which in turn is inheriting from its parent
class.
 Hierarchical inheritance More than one derived class can be created from a single base.
 Hybrid inheritance: This form combines more than one form of inheritance. Basically, it
is a blend of more than one type of inheritance.

Single inheritance:
Single inheritance enables a derived class to inherit properties from a single parent class,
thus enabling code reusability and the addition of new features to existing code.

Multiple Inheritance:

When a class can be derived from more than one base class this type of inheritance is called
multiple inheritances. In multiple inheritances, all the features of the base classes are inherited
into the derived class.
# Python example to show the working of multiple inheritance

class Base1(object):
def __init__(self):
self.str1 = "Geek1"
print("Base1")

class Base2(object):
def __init__(self):
self.str2 = "Geek2"
print("Base2")

class Derived(Base1, Base2):


def __init__(self):

# Calling constructors of Base1


# and Base2 classes
Base1.__init__(self)
Base2.__init__(self)
print("Derived")

def printStrs(self):
print(self.str1, self.str2)

ob = Derived()
ob.printStrs()

Multilevel Inheritance:

In multilevel inheritance, features of the base class and the derived class are further
inherited into the new derived class. This is similar to a relationship representing a child
and a grandfather.

# A Python program to demonstrate Multilevel inheritance


# Base or Super class. Note object in bracket.
# (Generally, object is made ancestor of all classes)
# In Python 3.x "class Person" is
# equivalent to "class Person(object)"

class Base(object):

# Constructor
def __init__(self, name):
self.name = name

# To get name
def getName(self):
return self.name

# Inherited or Sub class (Note Person in bracket)


class Child(Base):

# Constructor
def __init__(self, name, age):
Base.__init__(self, name)
self.age = age

# To get name
def getAge(self):
return self.age

# Inherited or Sub class (Note Person in bracket)

class GrandChild(Child):

# Constructor
def __init__(self, name, age, address):
Child.__init__(self, name, age)
self.address = address

# To get address
def getAddress(self):
return self.address

# Driver code
g = GrandChild("Geek1", 23, "Noida")
print(g.getName(), g.getAge(), g.getAddress())

Output:
Geek1 23 Noida
Hierarchical Inheritance:
When more than one derived class are created from a single base this type of inheritance is
called hierarchical inheritance. In this program, we have a parent (base) class and two child
(derived) classes.

# Python program to demonstrate

# Hierarchical inheritance

# Base class

class Parent:
def func1(self):

print("This function is in parent class.")

# Derived class1

class Child1(Parent):

def func2(self):

print("This function is in child 1.")

# Derivied class2

class Child2(Parent):

def func3(self):

print("This function is in child 2.")

# Driver's code

object1 = Child1()

object2 = Child2()

object1.func1()

object1.func2()

object2.func1()

object2.func3()

Output:
This function is in parent class.
This function is in child 1.
This function is in parent class.
This function is in child 2.
Hybrid Inheritance:
Inheritance consisting of multiple types of inheritance is called hybrid inheritance

# Python program to demonstrate

# hybrid inheritance

class School:

def func1(self):

print("This function is in school.")

class Student1(School):
def func2(self):

print("This function is in student 1. ")

class Student2(School):

def func3(self):

print("This function is in student 2.")

class Student3(Student1, School):

def func4(self):

print("This function is in student 3.")

# Driver's code

object = Student3()

object.func1()

object.func2()

Output:
This function is in school.
This function is in student 1.
POLYMORPHISM

The word polymorphism means having many forms. In programming, polymorphism means
the same function name (but different signatures) being used for different types. The key
difference is the data types and number of arguments used in function.

# Python program to demonstrate in-built poly-


# morphic functions

# len() being used for a string

print(len("geeks"))

# len() being used for a list

print(len([10, 20, 30]))

Polymorphism with Inheritance:

In Python, Polymorphism lets us define methods in the child class that have the same name as
the methods in the parent class. In inheritance, the child class inherits the methods from the
parent class. However, it is possible to modify a method in a child class that it has inherited
from the parent class. This is particularly useful in cases where the method inherited from the
parent class doesn’t quite fit the child class. In such cases, we re-implement the method in the
child class. This process of re-implementing a method in the child class is known as Method
Overriding.

class Bird:

def intro(self):

print("There are many types of birds.")

def flight(self):

print("Most of the birds can fly but some cannot.")

class sparrow(Bird):

def flight(self):

print("Sparrows can fly.")


class ostrich(Bird):

def flight(self):

print("Ostriches cannot fly.")

obj_bird = Bird()

obj_spr = sparrow()

obj_ost = ostrich()

obj_bird.intro()

obj_bird.flight()

obj_spr.intro()

obj_spr.flight()

obj_ost.intro()

obj_ost.flight()

Output

There are many types of birds.

Most of the birds can fly but some cannot.


There are many types of birds.

Sparrows can fly.

There are many types of birds.

Ostriches cannot fly.


An abstract
An abstract class can be considered as a blueprint for other classes. It allows you to create a
set of methods that must be created within any child classes built from the abstract class. A
class which contains one or more abstract methods is called an abstract class. An abstract
method is a method that has a declaration but does not have an implementation. While we are
designing large functional units we use an abstract class. When we want to provide a common
interface for different implementations of a component, we use an abstract class.

Why use Abstract Base Classes :

By defining an abstract base class, you can define a common Application Program
Interface(API) for a set of subclasses. This capability is especially useful in situations where a
third-party is going to provide implementations, such as with plugins, but can also help you
when working in a large team or with a large code-base where keeping all classes in your mind
is difficult or not possible.

How Abstract Base classes work :

By default, Python does not provide abstract classes. Python comes with a module that
provides the base for defining Abstract Base classes(ABC) and that module name is
ABC. ABC works by decorating methods of the base class as abstract and then registering
concrete classes as implementations of the abstract base. A method becomes abstract when
decorated with the keyword @abstractmethod.

# Python program showing

# abstract base class work

from abc import ABC, abstractmethod

class Polygon(ABC):

@abstractmethod

def noofsides(self):
pass

class Triangle(Polygon):

# overriding abstract method

def noofsides(self):

print("I have 3 sides")

class Pentagon(Polygon):

# overriding abstract method

def noofsides(self):

print("I have 5 sides")

class Hexagon(Polygon):

# overriding abstract method

def noofsides(self):

print("I have 6 sides")

class Quadrilateral(Polygon):

# overriding abstract method

def noofsides(self):

print("I have 4 sides")


# Driver code

R = Triangle()

R.noofsides()

K = Quadrilateral()

K.noofsides()

R = Pentagon()

R.noofsides()

K = Hexagon()

K.noofsides()

Output:

I have 3 sides

I have 4 sides
I have 5 sides

I have 6 sides

INTERFACE

 An interface in Python is a collection of method signatures that should be provided by the


implementing class.
 An interface contains methods that are abstract in nature. The abstract methods will have
the only declaration as there is no implementation.
 An interface in Python is defined using Python class and is a subclass of an interface.
Interface which is the parent interface for all interfaces.
 The implementations will be done by the classes which will inherit the
interface. Interfaces in Python are a little different from other languages like Java or C#
or C++.
 Implementing an interface is a way of writing organized code.

INFORMAL INTERFACE

Python informal interface is also a class that defines methods that can be overridden but without
force enforcement. An informal interface also called Protocols or Duck Typing. The duck typing
is actually we execute a method on the object as we expected an object to have, instead of
checking the type of an object.

An informal interface in python is termed as a protocol because it is informal and cannot be


formally enforced. It is mostly defined by templates or demonstrates in the documentations.
Consider some of the methods we usually used – __len__, __iter__, __contains__ and all, which
are used to perform some operation or set of protocols.

Program
classFruits:
def__init__( self, ele):
self.__ele = ele
def__contains__( self, ele):
return ele in self.__ele
def__len__( self ):
returnlen( self.__ele)
Fruits_list = Fruits(["Apple","Banana","Orange"])
print(len(Fruits_list))
print("Apple"in Fruits_list)
print("Mango"in Fruits_list)
print("Orange"notin Fruits_list)

Output

3
True
False
False

As in the above example code, class Fruits implement the __len__, and __contains__ methods,
so on the instance of the Fruits class, we can directly use the len function to get the size and can
check the membership by using the in operator. As in the above code, the __iter__ method
(iterable protocol) is not implemented, so we would not iterate over the instance of the Fruits.
Therefore an informal interface cannot be enforced formally.

FORMAL INTERFACE

A formal Interface is an interface which enforced formally. To create a formal interface, we need
to use ABCs (Abstract Base Classes).
An ABC is simple as an interface or base classes define as an abstract class in nature and the
abstract class contains some methods as abstract. Next, if any classes or objects implement or
drive from these base classes, then these bases classes forced to implements all those methods.
Note that the interface cannot be instantiated, which means that we cannot create the object of
the interface. So we use a base class to create an object, and we can say that the object
implements an interface. And we will use the type function to confirm that the object implements
a particular interface or not.

Program

import abc
classMyinterface( abc.ABC ):
@abc.abstractclassmethod
defdisp():
pass
classMyclass( Myinterface ):
pass
o1 = Myclass()

Output:

In the above code, the Myclass class inherits abstract class Myinterface but not provided
the disp() abstract method’s implementation. The class Myclass also becomes an abstract class
and hence cannot be instantiated.
Program:

import abc
classMyinterface( abc.ABC ):
@abc.abstractclassmethod
defdisp():
pass
#print(" Hello from Myclass ")
classMyclass(Myinterface):
defdisp(s):
print(" Hello from Myclass ")
o1=Myclass()
o1.disp()

Output

Hello from Myclass.

You might also like