By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Inheritance — Polymorphism: Dynamic Dispatch, Abstract Classes, Interface is a fundamental concept in object-oriented programming (OOP) that enables objects of different classes to be treated as objects of a common superclass. This allows for more flexibility and generic code.
This topic appears in exams to test your understanding of how to write efficient, maintainable, and scalable code. You can expect questions on designing classes, implementing interfaces, and using abstract classes to achieve polymorphism.
This topic is crucial for exams that focus on OOP, software design, and programming languages. It typically carries 20-30% of the total marks and appears in exams like the Certified Associate in Python Programming (CAP) and the Java Programming Certification (OCPJP). The skill being tested is your ability to apply OOP principles to real-world problems.
To master this topic, you need to understand the following core concepts:
Before tackling this topic, you should have a solid understanding of:
The primary rule of inheritance is:
Sub-rules and exceptions include:
Visual pattern:
name
age
eat()
sleep()
breed
bark()
Frequency: 20-30% Difficulty Rating: Intermediate Question Type or Real-World Task Type: Design a class hierarchy, implement an interface, or use an abstract class to achieve polymorphism.
Intermediate
The three most important rules for this topic are:
Question: Design a simple class hierarchy for a university system.
# University # Student # Undergraduate # Graduate
Solution:
University
address
Student
student_id
Undergraduate
Graduate
major
gpa
Key rule applied: A subclass inherits all the properties and methods of its superclass.
Question: Implement an interface for a payment system.
# Payment def pay(amount) def refund(amount)
Payment
pay
refund
CreditCard
Key rule applied: An interface defines a contract that must be implemented by any class that implements it.
Question: Use an abstract class to achieve polymorphism in a game system.
# Character def attack def defend # Warrior def attack def defend # Mage def attack def defend
Character
attack
defend
Warrior
Mage
Game
Key rule applied: An abstract class defines a blueprint for a class hierarchy and allows for polymorphism.
Mistake: A subclass inherits properties and methods from its superclass, but also adds new properties and methods that are not defined in the superclass.
Wrong answer: The Student class inherits from University and adds a new property student_id.
Correct approach: A subclass inherits all the properties and methods of its superclass, but cannot add new properties and methods that are not defined in the superclass.
Mistake: A subclass overrides a method of its superclass with a method that has a different signature.
Wrong answer: The Warrior class overrides the attack method of its superclass Character with a method that takes an additional parameter.
Correct approach: A subclass can override a method of its superclass with a method that has the same signature.
Mistake: A class has multiple methods with the same name but different parameters, but the methods are not overloaded correctly.
Wrong answer: The Game class has multiple methods attack with different parameters, but the methods are not overloaded correctly.
Correct approach: A class can have multiple methods with the same name but different parameters, but the methods must be overloaded correctly.
Mistake: A class implements an interface, but does not implement all the methods defined in the interface.
Wrong answer: The CreditCard class implements the Payment interface, but does not implement the refund method.
Correct approach: A class that implements an interface must implement all the methods defined in the interface.
Mistake: An abstract class is used as a concrete class, rather than as a blueprint for a class hierarchy.
Wrong answer: The Character abstract class is used as a concrete class to create a game system.
Correct approach: An abstract class is used as a blueprint for a class hierarchy and allows for polymorphism.
"Parents give properties and methods to children, but children cannot add new properties and methods that are not defined in parents."
Example: Design a simple class hierarchy for a university system.
Example: Implement an interface for a payment system.
Example: Use an abstract class to achieve polymorphism in a game system.
Example: Write a method to achieve polymorphism in a game system.
# Game def play(character) character.attack character.defend
Question: What is the primary rule of inheritance?
A) A subclass inherits all the properties and methods of its superclass.B) A subclass can add new properties and methods that are not defined in its superclass.C) A subclass can override a method of its superclass with a method that has a different signature.D) A subclass can implement an interface without implementing all the methods defined in the interface.
Correct answer: A) A subclass inherits all the properties and methods of its superclass.
Explanation: A subclass inherits all the properties and methods of its superclass, but cannot add new properties and methods that are not defined in the superclass.
Question: What is the difference between method overriding and method overloading?
A) Method overriding allows a subclass to override a method of its superclass with a method that has a different signature.B) Method overloading allows a class to have multiple methods with the same name but different parameters.C) Method overriding allows a class to have multiple methods with the same name but different parameters.D) Method overloading allows a subclass to override a method of its superclass with a method that has the same signature.
Correct answer: A) Method overriding allows a subclass to override a method of its superclass with a method that has a different signature.
Explanation: Method overriding allows a subclass to override a method of its superclass with a method that has a different signature, while method overloading allows a class to have multiple methods with the same name but different parameters.
Question: What is the purpose of an abstract class?
A) To define a contract that must be implemented by any class that implements it.B) To provide a blueprint for a class hierarchy and allow for polymorphism.C) To create a concrete class that can be instantiated on its own.D) To define a method that can be overridden by a subclass.
Correct answer: B) To provide a blueprint for a class hierarchy and allow for polymorphism.
Explanation: An abstract class provides a blueprint for a class hierarchy and allows for polymorphism, while an interface defines a contract that must be implemented by any class that implements it.
Question: What is the difference between a class and an object?
A) A class is a blueprint for a class hierarchy, while an object is an instance of a class.B) A class is an instance of a class, while an object is a blueprint for a class hierarchy.C) A class is a method that can be overridden by a subclass, while an object is a property that can be inherited by a subclass.D) A class is a property that can be inherited by a subclass, while an object is a method that can be overridden by a subclass.
Correct answer: A) A class is a blueprint for a class hierarchy, while an object is an instance of a class.
Explanation: A class is a blueprint for a class hierarchy, while an object is an instance of a class.
Question: What is the purpose of method overloading?
A) To allow a class to have multiple methods with the same name but different parameters.B) To allow a subclass to override a method of its superclass with a method that has a different signature.C) To allow a class to implement an interface without implementing all the methods defined in the interface.D) To allow a subclass to inherit properties and methods from its superclass.
Correct answer: A) To allow a class to have multiple methods with the same name but different parameters.
Explanation: Method overloading allows a class to have multiple methods with the same name but different parameters.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.