How to Override Methods and Properties?

Sometimes, we may want to modify or extend the behavior of a method or a property that is inherited from a superclass.

  • For example, we may want to change how an animal makes noise or add more information to its description.
  • To do this, we can override the method or property in the subclass and provide a new implementation.
  • This is called method overriding or property overriding.
  • In Swift, we use the override keyword to indicate that we are overriding a method or a property in a subclass.

Below is Swift program to override methods and properties:

Swift




// Swift program to override methods
// and properties
// Define a superclass
class Animal
{
  // Properties and methods of the superclass
  var name: String = ""
 
  var description: String
  {
    return "I am an animal named \(name)"
  }
   
  func makeNoise()
  {
    print("I can make noise")
  }
}
 
// Define a subclass that inherits from Animal
class Dog: Animal
{
  // Override the computed property
  override var description: String
  {
    return "I am a dog named \(name)"
  }
   
  // Override the method
  override func makeNoise()
  {
    print("I can bark")
  }
}
 
// Create an instance of Dog
let dog = Dog()
 
// Assign a name to the dog
dog.name = "Rex"
 
// Access the overridden computed property
// I am a dog named Rex
print(dog.description)
 
// Call the overridden method
// I can bark
dog.makeNoise()
 
// Define a subclass that inherits from Dog
class Labrador: Dog
{
  // Override the computed property again
  override var description: String
  {
    return "I am a labrador named \(name), \(super.description)"
  }
}
 
// Create an instance of Labrador
let lab = Labrador()
 
// Assign a name to the labrador
lab.name = "Max"
 
// Access the overridden computed property
// I am a labrador named Max, I am a dog named Max
print(lab.description)
 
// Call the inherited method
// I can bark
lab.makeNoise()


Output:

I am a dog named Rex
I can bark
I am a labrador named Max, I am a dog named Max
I can bark

Explanation:

  • The code defines a superclass Animal with properties and methods.
  • A subclass Dog inherits from Animal and customizes the description and makeNoise() properties and methods.
  • An instance of a Dog named Rex is created.
  • The overridden description property of Dog displays “I am a dog named Rex.”
  • The overridden makeNoise() method of Dog prints “I can bark.”
  • Another subclass Labrador is defined, as inheriting from “Dog.”
  • The description property of Labrador is overridden, including the original superclass description using super.
  • An instance of Labrador named Max is created.
  • The overridden description property of Labrador combines both class names, displaying “I am a labrador named Max, I am a dog named Max.”
  • The makeNoise() method inherited from Dog still prints “I can bark.”

Swift – Inheritance

Inheritance is one of the fundamental concepts of object-oriented programming (OOP) languages. It allows us to create a new class from an existing class and reuse the existing code and functionality. In this article, we will learn about inheritance in Swift and how to use it to create subclasses, override methods and properties, and implement polymorphism.

Similar Reads

What is Inheritance?

Inheritance is a relationship between two classes, where one class inherits the methods, properties, and other characteristics from another class....

How to Override Methods and Properties?

...

Types of Inheritance

Sometimes, we may want to modify or extend the behavior of a method or a property that is inherited from a superclass....

Is-a Relationship

...

Method Overriding

Swift supports different types of inheritance, depending on the relationship between the classes involved. Some of the common types of inheritance are:...

super Keyword

In Swift, inheritance is an is-a relationship. That is, we use inheritance only if there exists an is-a relationship between two classes. An is-a relationship means that a subclass is a specific type or kind of its superclass. For example:...

Property Overriding

...

Final Property

A subclass can provide its custom implementation of an instance method, type method, instance property, type property or subscript that it would otherwise inherit from a superclass. This is known as overriding. To override a characteristic that would otherwise be inherited, we prefix our overriding definition with the override keyword....

Conclusion

...