Classes and Inheritance in CoffeeScript

November 17, 2024

Classes and Inheritance in CoffeeScript

In this lesson, we introduce object-oriented programming concepts in CoffeeScript, focusing on classes and inheritance. You will learn how to define classes, create instances, and implement inheritance with practical examples.

Understanding Classes in CoffeeScript

Classes are a fundamental part of object-oriented programming, allowing you to create blueprints for objects. In CoffeeScript, defining a class is straightforward and concise.

Defining a Class

To define a class in CoffeeScript, you use the class keyword followed by the class name. Here’s a simple example:

class Animal
  constructor: (name) ->
    @name = name

  speak: ->
    console.log "#{@name} makes a noise"

In this example, we define an Animal class with a constructor that takes a name parameter. The speak method logs a message to the console.

Creating Instances

Once you have defined a class, you can create instances of it using the new keyword:

dog = new Animal('Dog')
dog.speak()  # Output: Dog makes a noise

This creates a new instance of the Animal class and calls the speak method, which outputs a message to the console.

Inheritance in CoffeeScript

Inheritance allows you to create a new class that is based on an existing class. This is useful for creating a hierarchy of classes that share common functionality.

Defining a Subclass

To create a subclass in CoffeeScript, you use the extends keyword. Here’s how you can create a subclass called Dog that inherits from the Animal class:

class Dog extends Animal
  speak: ->
    console.log "#{@name} barks"

In this example, the Dog class extends the Animal class and overrides the speak method to provide a specific implementation for dogs.

Creating Instances of Subclasses

You can create instances of the subclass just like you did with the parent class:

dog = new Dog('Rex')
dog.speak()  # Output: Rex barks

This creates a new instance of the Dog class, which inherits the properties and methods of the Animal class but has its own implementation of the speak method.

Using Super in Inheritance

Sometimes, you may want to call methods from the parent class within the subclass. You can achieve this using the super keyword.

class Dog extends Animal
  speak: ->
    super()  # Call the parent class speak method
    console.log "#{@name} barks"

In this modified speak method, we first call the parent class’s speak method using super(), followed by a custom message for the dog.

Example of Super in Action

dog = new Dog('Rex')
dog.speak()  # Output: Rex makes a noise
                # Rex barks

This demonstrates how the subclass can extend the functionality of the parent class while still retaining its behavior.

Conclusion

In this lesson, we covered the basics of classes and inheritance in CoffeeScript. You learned how to define classes, create instances, and implement inheritance with practical examples. Understanding these concepts is crucial for writing clean and manageable code in an object-oriented style.

In the next lesson, we will delve deeper into more advanced object-oriented programming features in CoffeeScript. Stay tuned!