-
Create two classes,
Animal
andDog
, whereDog
inherits fromAnimal
. TheAnimal
class should have a methodspeak()
that prints “Animal speaks.” TheDog
class should have a methodbark()
that prints “Dog barks.” And identify it’s type of inheritance. -
Create three classes,
Person
,Employee
, andStudent
, whereEmployee
andStudent
both inherit fromPerson
. Each class should have a methodintroduction()
that prints an introduction specific to the class. And identify it’s type of inheritance.
Create two classes, Animal
and Dog
, where Dog
inherits from Animal
. The Animal
class should have a method speak()
that prints “Animal speaks.” The Dog
class should have a method bark()
that prints “Dog barks.” And identify it’s type of inheritance.
Sol:
class Animal:
def speak(self):
print(“Animal speaks”)
class Dog(Animal):
def bark(self):
print(“Dog barks”)
animal = Animal()
animal.speak() # Output: “Animal speaks”
dog = Dog()
dog.speak() # Output: “Animal speaks” (Inherited from Animal)
dog.bark() # Output: “Dog barks”
- Create three classes,
Person
,Employee
, andStudent
, whereEmployee
andStudent
both inherit fromPerson
. Each class should have a methodintroduction()
that prints an introduction specific to the class. And identify it’s type of inheritance.
Sol:
class Person:
def init(self, name):
self.name = name
def introduction(self):
print(f"Hi, I am {self.name}. I am a Person.")
class Employee(Person):
def init(self, name, employee_id):
super().init(name)
self.employee_id = employee_id
def introduction(self):
print(f"Hi, I am {self.name}. I am an Employee with ID {self.employee_id}.")
class Student(Person):
def init(self, name, student_id):
super().init(name)
self.student_id = student_id
def introduction(self):
print(f"Hi, I am {self.name}. I am a Student with ID {self.student_id}.")
person = Person(“John”)
employee = Employee(“Alice”, “E12345”)
student = Student(“Bob”, “S67890”)
person.introduction() # Output: “Hi, I am John. I am a Person.”
employee.introduction() # Output: “Hi, I am Alice. I am an Employee with ID E12345.”
student.introduction() # Output: “Hi, I am Bob. I am a Student with ID S67890.”
1.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
dog = Dog()
dog.speak()
dog.bark()
# Single Inheritence.
2.
class Person:
def introduction(self):
print("I am a person")
class Employee(Person):
def introduction(self):
print("I am an employee")
class Student(Person):
def introduction(self):
print("I am a student")
employee = Employee()
student = Student()
employee.introduction()
student.introduction()
# Hierarchical Inheritance
1)
class animal:
def speak(self):
print("Animal speak")
class dog(animal):
def bark (self):
print("Dog bark")
obj =dog()
obj.speak()
obj.bark()
2)
class person:
def person_introduction(self):
print("Person introduction")
class employee (person):
def employee_introduction(self):
print("employee introduction")
class student(person):
def student_introduction(self):
print("student introduction")
emp =employee()
emp.person_introduction()
emp.employee_introduction()
stu =student()
stu.person_introduction()
stu.student_introduction()
````
#1. Create two classes, Animal and Dog , where Dog inherits from Animal . The Animal class should have a method speak() that prints “Animal speaks.” The Dog class should have a method bark() that prints “Dog barks.” And identify it’s type of inheritance.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
dog = Dog()
dog.speak()
dog.bark()
# Single Inheritance: 1 parent with 1 child class
# 2. Create three classes, Person , Employee , and Student , where Employee and Student both inherit from Person . Each class should have a method introduction() that prints an introduction specific to the class. And identify it’s type of inheritance.
class Person:
def introduction(self):
print("Hi, I am Shruti")
class Employee(Person):
def introduction(self):
print("I work for Edyoda")
class Student(Person):
def introduction(self):
print("I am pursuing Data Science course")
emp = Employee()
std = Student()
emp.introduction()
std.introduction()
# Hierarchical Inheritance: 1 parent class with 2 child classes
# Create two classes, Animal and Dog , where Dog inherits from Animal .
# The Animal class should have a method speak() that prints “Animal speaks.”
# The Dog class should have a method bark() that prints “Dog barks.”
# And identify it’s type of inheritance.
class Animal:
def method_speak(self):
print("Animal Speaks.")
class Dog(Animal):
def method_bark(self):
print("Dog barks.")
obj=Dog()
obj.method_speak()
obj.method_bark()
# Create three classes, Person , Employee , and Student , where Employee and Student
# both inherit from Person . Each class should have a method introduction() that
# prints an introduction specific to the class. And identify it’s type of inheritance.
class person:
def introduction(self):
print("I am Surya")
class Employee(person):
def introduction(self):
print("I am working currently")
class Student(person):
def introduction(self):
print("I Am Completed My Graduation")
person = person()
employee = Employee()
student = Student()
person.introduction()
employee.introduction()
student.introduction()
# 1st question
# single inheritance
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
dog=Dog()
dog.bark()
dog.speak()
# 2nd question
# hierarchical inheritance
class Person:
def introduction(self):
print("person introduction")
class Employee(Person):
def employee_introduction(self):
print("Employee introduction")
class Student(Person):
def student_introduction(self):
print("Student introduction")
student=Student()
student.student_introduction()
student.introduction()
employee=Employee()
employee.employee_introduction()
employee.introduction()
#Create two classes, Animal and Dog , where Dog inherits from Animal The Animal class should have a method speak() that prints “Animal speaks.” The Dog class should have a method bark() that prints “Dog barks.” And identify it’s type of inheritance.
class Animal:
def speak(self):
print("Animal can speak:")
class Dog(Animal):
def bark(self):
print("The Dog barks")
d=Dog()
d.speak()
d.bark()
output:-
Animal can speak:
The Dog barks
1. Create three classes, Person
, Employee
, and Student
, where Employee
and Student
both inherit from Person
. Each class should have a method introduction()
that prints an introduction specific to the class. And identify it’s type of inheritance.
class Person:
def __init__(self, name):
self.name = name
def Person_Introduction(self):
print(f"my name is:{self.name}")
class Employee(Person):
def __init__(self, name, employee_id):
super().__init__(name)
self.employee_id = employee_id
def Employee_Introduction(self):
print(f"my name is {self.name} my employee_id is: {self.employee_id}")
class Student(Person):
def __init__(self, name, student_id):
super().__init__(name)
self.student_id = student_id
def Student_Introduction(self):
print(f"my name is {self.name} my student_id is {self.student_id}")
name=input("enter name:")
employee_id=int(input("enter employee_Id: "))
student_Id=int(input("enter student_Id:"))
person = Person(name)
employee = Employee(name,employee_id)
student = Student(name,student_Id)
person.Person_Introduction()
employee.Employee_Introduction()
student.Student_Introduction()
output:
enter name:mamatha
enter employee_Id: 1213
enter student_Id:600650
my name is:mamatha
my name is mamatha my employee_id is: 1213
my name is mamatha my student_id is 600650
Ans 1 -
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
obj = Animal()
obj.speak()
obj1 = Dog()
obj1.speak()
obj1.bark()
# Single inheritance
Ans 2-
class Person:
def introduction(self):
print("I am Amandip kaur")
class Employee(Person):
def introduction(self):
print("Employee ID - 123")
class Student(Person):
def introduction(self):
print("I am studing in MSc part II")
person = Person()
person.introduction()
emp = Employee()
std = Student()
emp.introduction()
std.introduction()
# hierarchical inheritance
# Create two classes, Animal and Dog , where Dog inherits from Animal . The Animal class should have a method speak() that prints “Animal speaks.” The Dog class should have a method bark() that prints “Dog barks.” And identify it’s type of inheritance.
# SINGLE INHERITANCE :
# PARENT CLASS animal
# CHILD CLASS dog
class animal():
def speak(self):
print("Animal speaks")
class dog(animal):
def bark(self):
print("Dog barks")
dog = dog()
dog.speak()
dog.bark()
# Create three classes, Person , Employee , and Student , where Employee and Student both inherit from Person .
# Each class should have a method introduction() that prints an introduction specific to the class.
# And identify it’s type of inheritance.
#Hierarchial inheritance
class person():
def introduction(self):
print("Hi I am a person")
class employee(person):
def introduction(self):
print("I am an employee")
class student(person):
def introduction(self):
print("I am a student")
employee = employee()
employee.introduction()
student = student()
student.introduction()
# 1] Create two classes, Animal and Dog , where Dog inherits from Animal . The Animal class should have a method speak() that prints “Animal speaks.”
# The Dog class should have a method bark() that prints “Dog barks.” And identify it’s type of inheritance.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
dog = Dog()
dog.speak()
dog.bark()
# Single Inheritance: 1 parent with 1 child class
# 2] Create three classes, Person , Employee , and Student , where Employee and Student both inherit from Person
# Each class should have a method introduction() that prints an introduction specific to the class. And identify it’s type of inheritance
class Person:
def introduction(self):
print("Hi, I am Vaibhavi")
class Employee(Person):
def introduction(self):
print("I am an Employee")
class Student(Person):
def introduction(self):
print("I am a student")
emp = Employee()
stu = Student()
emp.introduction()
stu.introduction()
# Create two classes, Animal and Dog , where Dog inherits from Animal . The Animal class should have a method speak() that prints “Animal speaks.” The Dog class should have a method bark() that prints “Dog barks.” And identify it’s type of inheritance.
class Animal:
def speak(self):
print("Animal Speaks")
class Dog(Animal):
def bark(self):
print("Dog Barks")
dog = Dog()
dog.speak()
dog.bark()
# Create three classes, Person , Employee , and Student , where Employee and Student both inherit from Person . Each class should have a method introduction() that prints an introduction specific to the class. And identify it’s type of inheritance.
class Person:
def introduction(self):
print("I am a person")
class Employee(Person):
def introduction(self):
print("I am an employee")
class Student(Person):
def introduction(self):
print("I am a student")
employee = Employee()
employee.introduction()
student = Student()
student.introduction()