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

all in one class

hi team

i understand about class and inheritence

but why cant we just write all in one class

for example we have a class of product which have title and price and then we write a new subclass of electronics inherit from product which have another property like camera

why cant we just put that all in 1 class that cointain all their property? and if we dont need the other property just dont use it. why bother creating another subclass??

thanks i hope my question is clear

2 Answers

Calvin, it's a very good question! And unfortunately the videos don't really go into the reason in enough detail. The problem with already knowing this stuff is that the instructors sometimes forget that people just starting out don't.

There are a number of reasons for subclasses and inheritance. But let's take an example first.

Suppose you have a Shape class, and instead of having several classes that inherit from it: Triangle, Rectangle, Circle, Square, Hexagon, etc.. you just have that one big class.

Let's further suppose that one of the things you will want to do is create a calculateArea() method that returns the area of shape objects.

What formula are you going to put inside your calculateArea() method? Clearly, you can't have just one formula there, because every shape needs a different formula. It's possible, I suppose, to have a big if... else if... else if... else if... else if... else.... statement, but you would also have to have a parameter to indicate which of the formulas to use, etc.

And we haven't even gotten to number of sides vs no sides, radius vs no radius, calculating perimeter vs circumference, what the constructor(s) would look like, etc., etc.

In short, there are immediate problems. But they are just the tip of the iceberg. I won't go into more details, but believe me, if you don't want inheritance now you will later.

Nathan Tallack
Nathan Tallack
22,164 Points

From Wikipedia article on Classes.

The benefits of organizing software into object classes fall into three categories:

  1. Rapid development
  2. Ease of maintenance
  3. Reuse of code and designs

Object classes facilitate rapid development because they lessen the semantic gap between the code and the users. System analysts can talk to both developers and users using essentially the same vocabulary, talking about accounts, customers, bills, etc. Object classes often facilitate rapid development because most object-oriented environments come with powerful debugging and testing tools. Instances of classes can be inspected at run time to verify that the system is performing as expected. Also, rather than get dumps of core memory, most object-oriented environments have interpreted debugging capabilities so that the developer can analyze exactly where in the program the error occurred and can see which methods were called to which arguments and with what arguments.

Object classes facilitate ease of maintenance via encapsulation. When developers need to change the behavior of an object they can localize the change to just that object and its component parts. This reduces the potential for unwanted side effects from maintenance enhancements.

Software re-use is also a major benefit of using Object classes. Classes facilitate re-use via inheritance and interfaces. When a new behavior is required it can often be achieved by creating a new class and having that class inherit the default behaviors and data of its superclass and then tailor some aspect of the behavior or data accordingly. Re-use via interfaces (also known as methods) occurs when another object wants to invoke (rather than create a new kind of) some object class. This method for re-use removes many of the common errors that can make their way into software when one program re-uses code from another.

These benefits come with a cost of course. One of most serious obstacles to using object classes has been performance. Interpreted environments that support languages such as Smalltalk and CLOS provided rapid development but the resulting code was not nearly as fast as what could be achieved in some procedural languages such as C. This has been partly addressed by the development of object-oriented languages that are not interpreted such as C++ and Java. Also, due to Moore's law the processing power of computers has increased to the point where efficient code is not as critical for most systems as it was in the past. Still, no matter how well designed the language, there will always be an inevitable bit of required extra overhead to create a class rather than use procedural code and in some circumstances, especially where performance or memory are required to be optimal, that using object classes may not be the best approach.

Also, getting the benefits of object classes requires that they be used appropriately and that requires training. Without the proper training developers may simply code procedural programs in an object-oriented environment and end up with the worst of both worlds.