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

General Discussion

Software Singh
Software Singh
7,123 Points

what is the best way to learn object oriented programming?

i want to know the best possible way to learn object oriented programming. right now i am learning php here and having a hard time in learning object oriented part in php. need some guidance form someone who has passed the stage i struggling in right now.

2 Answers

Hi!

The best way to learn OOP is just to write object-oriented programs. Re-visit software you have already re-written and see how you could re-factor it to use OOP concepts and practices. As well as this think of some real life examples to practice things like objects and inheritance. For example, why not create a car class and have it inherit from a class called vehicles which contains some general vehicle methods and properties.

See if you can figure out how to do this. I know it might seem hard but one of the best ways to learn something like this is just to dive into the deep end and see what you can figure out.

If you have any more questions then don't hesitate to ask them!

-Luke

Software Singh
Software Singh
7,123 Points

thanks luke i like the way you participate in community and help students like me. And I appreciate your effort from my heart..... keep going buddy..

Thank you very much Jasmohan, I appreciate your kind words a lot!

Happy coding :)

Erik McClintock
Erik McClintock
45,783 Points

Jasmohan,

This type of question is somewhat tricky to answer in a satisfying way. Hopefully others will chime in with their experiences, though, so you've got a wide range of views to look at! Object-oriented programming is a rather abstract concept, and is difficult to grasp the usefulness of until you're already in a situation where you would benefit from utilizing it. Small programs that aren't really living, breathing things (such that they aren't really dynamic and don't need to handle change, for instance) won't get as much use out of the practices found in OOP (object-oriented programming) as a larger program that is constantly changing and handling new information.

For me, the tough part of object-oriented programming (which tends to be the tough part of learning most anything about programming) was understanding the "why" aspect. Why does OOP exist; what purpose does it serve? Thankfully, that is more easily understood when we learn and grasp the four major principles of OOP: abstraction, encapsulation, inheritance, and polymorphism. That said, of course, these terms themselves can be rather confusing and seemingly abstract, and really the best way to fully understand them is to dig in and start messing around with some code. What follows, though, are some brief definitions of these terms/concepts:

1) Abstraction - this is the process of hiding the details and exposing only the essential features of a particular concept or object without including the background details or explanation. As a simple example, consider playing a video game, like Super Mario Bros. You know that pressing the A button will make Mario jump, but you don't need to know the details of HOW pressing that button makes Mario jump. The only important information that you need here, and thus the only information that should be exposed to you as the player, should be the fact that "pressing the A button will cause Mario to jump."

2) Encapsulation - this is tied rather closely to abstraction, and there are a lot of articles available to try to help explain the difference and the relationship between the two. For its own definition, though, encapsulation is the practice of hiding the internal workings of classes and objects from outside forces, making them accessible only through methods commonly called "getters" and "setters." This helps prevent the accidental (or intentional...) alteration/potential destruction of data and functions within the given class/object from external sources, which could cause serious damage in an untold number of locations throughout your program. By only allowing data to be accessed via methods, those classes and objects can effectively define/limit the way(s) in which external sources are able to use or alter the data contained within, based on what those available methods do. This frees us from the worry of breaking other code in our program that is using our particular class or object, even if we make changes in another location.

3) Inheritance - this refers to the ability for classes or objects to inherit the interface and possibly the implementation of a parent class or object, making them "sub" and "super" classes, respectively. An easy way to think about inheritance is to think of a quick example using a generic Animal superclass. Say we have an Animal superclass, which contains some common information that all animals will likely share, such as a property for the type of animal, and the weight of the animal. All animals will have a type, and all animals will have a weight. That said, not all Animals will be able to fly, so it is here that we can create a subclass of Animal, let's call it Bird, which needs the ability to fly. Bird will also still need to know its type and its weight, like all Animals, so it will first INHERIT from the superclass Animal, and then it will extend its functionality to include a property or perhaps a method that defines its ability as a Bird to fly. The superclass is generic, the subclasses that inherit from it can get more specific.

4) Polymorphism - this is a Greek term that means "one name, many forms", which is precisely what the concept refers to. Like abstraction and encapsulation are closely related, so too are inheritance and polymorphism (and encapsulation). In OOP, polymorphism allows for the same action to be performed in different ways depending on where it is being used. It describes a pattern in which classes have different functionality while sharing the same interface. Related to the previous example of the Animal superclass and the Bird subclass: let's create another subclass called Dog. Dogs and Birds both make sounds, but dogs bark whereas birds caw. Each subclass will have a method, named makeSound(), and while they are named the same thing, they will produce different results. Dog's makeSound() method will bark, whereas Bird's makeSound() method will caw.

These can be intimidating concepts to understand, even once you become more familiar with how to write your programs to utilize them. OOP is just like learning any other facet of programming: it is going to take a long time to master. That said, you will be able to start picking up on it and using it fairly quickly. With time and practice, however, you will become more familiar with it, and will be able to write more efficient programs through the use of these principles.

The unfortunate reality is that there doesn't seem to be one tried-and-true, best, end-all-be-all method of learning these or any concepts, beyond simply sitting down with a book or some videos, studying, and practicing. Every person's learning style is different, and certain concepts will be easy for one person and impossibly difficult for the next. Practice practice practice, and don't restrict yourself to one resource. Treehouse is phenomenal, and I wouldn't have my job today if it weren't for this site and its wonderful teachers and community. BUT, Treehouse isn't the only resource I've used. I also highly recommend the Head First series of books from O'Reilly Media, and suggest also looking into some other online courses, such as the ones found at Codecademy. It's been a while since I have used other course sites (like Codecademy), so I couldn't speak to whether or not it has OOP classes in it, but do some research and you'll stumble onto some other sites pretty easily that have classes you can take (some free, some paid). Opening yourself up to multiple avenues of learning will likely increase your understanding of these tough concepts much more quickly, as it can be very beneficial to learn the same information in slightly different ways. Perhaps all it will take for you is the right example of objects and OOP concepts, maybe in the form of cars, or video games, or sports. Who knows? The only way to find out is to jump in and poke around, and soon enough, you'll find what works for you :) Personally, I'm partial to games, so here are two samples for you that are in that realm: here is an article that explains OOP through the use of RPG systems, and here is one that explains it through a video game tutorial series.

The biggest thing to take away from this: do NOT let yourself get down for having difficulty with these (or any other) concepts related to programming or computer science. These are very tough, abstract things to understand, and the VAST majority of people struggle with them just as you are when they are first learning, and even continue to struggle with them as they get more experience and grow. The goods news is that you have more information at your fingertips now (and mostly for free!) than ever before, and great communities like the one here on Treehouse to help you out when you hit a road block.

I apologize for the lengthy reply, though I hope you'll find the message useful.

Happy coding!

Erik

Software Singh
Software Singh
7,123 Points

thank you so much eric I loved your brief explanation and helpful reply. Guys like you make treehouse and this community great. Your reply was very good and i loved it . And i will surely work on the resources you provided me.

Your reply was very inspiring and boosted my confidence. And your effort inspired me to contribute in the community with the skills i got.

It would be great for me if u can guide me on some other stuff i have in my head which is disturbing me .My email id is jasmohansingh13@gmail.com. It would be great if u reply and if not, then its still okay!!! Keep the good deeds High!!

you are doing a great job! Friend