Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift Inheritance What is Inheritance?

Karthikeya Nadendla
Karthikeya Nadendla
9,747 Points

What does it mean "You forgot to inherit from Button" ?

I have wrote the code for the Question, So write a subclass named RoundButton which inherits from Button but i'm getting an error. Please let me know how to write the code for that. Thanks

Button.swift
class RoundButton : Button {
  var width: Double
  var height: Double

  init(width:Double, height:Double){
    self.width = width
    self.height = height
  }
}

2 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

I am not sure if you have posted your entire code, but take care not to remove the original Button class, because RoundButton will have to be a subclass of Button. This way, you don't have to rewrite variables and functions that RoundButton shares with Button.

So this code should work:

// Button, superclass for RoundButton
class Button {
  var width: Double
  var height: Double

  init(width:Double, height:Double){
    self.width = width
    self.height = height
  }
}

// Subclass of Button class
class RoundButton : Button {
    // This class inherits variables and functions
    // from the Button superclass. That is, you
    // can access width and height. You can also
    // use the initializer of Button.
}

The concept of inheritance is a fundamental part of OOP (Object Oriented Programming). Make sure to have a look at the Apple Docs: Inheritance chapter, it's explained in detail there.

Hope that helps! :)