Fatskills
Practice. Master. Repeat.
Study Guide: University
Source: https://www.fatskills.com/ap/chapter/university

University

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~10 min read

What Is This?

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.

Why It Matters

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.

Core Concepts

To master this topic, you need to understand the following core concepts:


  • Polymorphism: The ability of an object to take on multiple forms, depending on the context in which it is used.
  • Dynamic Dispatch: The process of determining the method to be called at runtime, based on the object's type.
  • Abstract Classes: Classes that cannot be instantiated on their own and are designed to be inherited by other classes.
  • Interfaces: Abstract classes that define a contract or a set of methods that must be implemented by any class that implements them.

Prerequisites

Before tackling this topic, you should have a solid understanding of:


  • Object-Oriented Programming (OOP) principles: You should know how to design classes, encapsulate data, and use inheritance.
  • Class and Object relationships: You should understand how classes relate to each other and how objects interact with each other.
  • Method Overloading and Overriding: You should know how to overload and override methods in a class.

The Rule-Book (How It Works)

The primary rule of inheritance is:


  • A subclass inherits all the properties and methods of its superclass.

Sub-rules and exceptions include:


  • Method overriding: A subclass can override a method of its superclass with its own implementation.
  • Method overloading: A class can have multiple methods with the same name but different parameters.
  • Abstract classes: A class that cannot be instantiated on its own and is designed to be inherited by other classes.

Visual pattern:


Class Properties Methods
Superclass name, age eat(), sleep()
Subclass name, age, breed eat(), sleep(), bark()

Exam / Job / Audit Weighting

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.

Difficulty Level

Intermediate

Must-Know Rules, Formulas, Standards, or Principles

The three most important rules for this topic are:


  1. A subclass inherits all the properties and methods of its superclass.
  2. Method overriding allows a subclass to override a method of its superclass with its own implementation.
  3. Method overloading allows a class to have multiple methods with the same name but different parameters.

Worked Examples (Step-by-Step)


Example 1: Easy

Question: Design a simple class hierarchy for a university system.


# University
  # Student
# Undergraduate
# Graduate

Solution:


  • The University class has properties name and address.
  • The Student class inherits from University and has properties name, age, and student_id.
  • The Undergraduate and Graduate classes inherit from Student and have properties major and gpa, respectively.

Key rule applied: A subclass inherits all the properties and methods of its superclass.

Example 2: Medium

Question: Implement an interface for a payment system.


# Payment
  def pay(amount)
  def refund(amount)

Solution:


  • The Payment interface defines two methods: pay and refund.
  • A class that implements Payment must implement these two methods.
  • The CreditCard class implements Payment and overrides the pay and refund methods.

Key rule applied: An interface defines a contract that must be implemented by any class that implements it.

Example 3: Hard

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

Solution:


  • The Character abstract class defines two methods: attack and defend.
  • The Warrior and Mage classes inherit from Character and override the attack and defend methods.
  • The Game class uses the Character abstract class to create a polymorphic game system.

Key rule applied: An abstract class defines a blueprint for a class hierarchy and allows for polymorphism.

Common Exam Traps & Mistakes


Trap 1: Incorrect Inheritance

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.

Trap 2: Incorrect Method Overriding

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.

Trap 3: Incorrect Method Overloading

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.

Trap 4: Incorrect Interface Implementation

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.

Trap 5: Incorrect Abstract Class Usage

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.

Shortcut Strategies & Exam Hacks


Hack 1: Use a simple class hierarchy diagram to visualize the relationships between classes.

# University
  # Student
# Undergraduate
# Graduate

Hack 2: Use a table to compare the properties and methods of classes.

Class Properties Methods
Superclass name, age eat(), sleep()
Subclass name, age, breed eat(), sleep(), bark()

Hack 3: Use a simple mnemonic to remember the rules of inheritance.

"Parents give properties and methods to children, but children cannot add new properties and methods that are not defined in parents."

Question-Type Taxonomy


Format 1: Design a class hierarchy

Example: Design a simple class hierarchy for a university system.


# University
  # Student
# Undergraduate
# Graduate

Format 2: Implement an interface

Example: Implement an interface for a payment system.


# Payment
  def pay(amount)
  def refund(amount)

Format 3: Use an abstract class to achieve polymorphism

Example: 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

Format 4: Write a method to achieve polymorphism

Example: Write a method to achieve polymorphism in a game system.


# Game
  def play(character)
character.attack
character.defend

Practice Set (MCQs)


Question 1: Easy

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 2: Medium

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 3: Hard

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 4: Easy

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 5: Medium

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.

30-Second Cheat Sheet

  • A subclass inherits all the properties and methods of its superclass.
  • Method overriding allows a subclass to override a method of its superclass with a method that has a different signature.
  • Method overloading allows a class to have multiple methods with the same name but different parameters.
  • An abstract class provides a blueprint for a class hierarchy and allows for polymorphism.
  • An interface defines a contract that must be implemented by any class that implements it.

Learning Path

  1. Beginner foundation: Understand the basics of object-oriented programming (OOP) and class relationships.
  2. Core rules: Learn the rules of inheritance, method overriding, and method overloading.
  3. Practice: Practice designing class hierarchies, implementing interfaces, and using abstract classes to achieve polymorphism.
  4. Timed drills: Practice solving problems under time pressure to improve your speed and accuracy.
  5. Mock tests: Take mock tests to assess your knowledge and identify areas for improvement.

Related Topics

  • Encapsulation: The ability of an object to hide its internal state and expose only the necessary information to the outside world.
  • Abstraction: The ability of an object to represent complex behavior in a simple way.
  • Composition: The ability of an object to contain other objects and use their properties and methods.