Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I want to learn OOP. I know Python and I know very little things about OOP.

But when I search for "learn OOP" in forums I saw a guy saying that "Python is so new that's why you can't learn OOP on Python. you should learn Java then understand OOP on Java"

Is it true? Is there any difference understanding OOP on different programming languages? like learn it on Java, C#, C++, Perl or Python?

share|improve this question

closed as too broad by gnat, Kilian Foth, Bart van Ingen Schenau, MichaelT, amon Feb 19 at 16:22

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

12  
"Python is so new that's why you can't learn OOP on Pyton. you learn Java then understand OOP on Java" That makes no sense to me. If you want to learn OOP using Python, I don't see any reason why not. Go for it! –  FrustratedWithFormsDesigner Feb 18 at 21:28
13  
Isn't Python older than Java? Off the top of my head I believe it is but I could be off by a couple years. –  Jimmy Hoffa Feb 18 at 23:18
2  
@JimmyHoffa It is indeed. 91 vs. 95 according to Wikipedia. –  Evicatos Feb 18 at 23:42
2  
@JimmyHoffa: come on, that can't be true, can it! We all know Java was the first OO language ever. "Sun loudly heralds Java's novelty"... — As for the question... why are you determined to learn OO? That'll be forced upon you soon enough. Python is an excellent starting point, to get insights to the benefits of multiple programming paradigms. –  leftaroundabout Feb 19 at 0:28

9 Answers 9

up vote 10 down vote accepted

An object as a theoretical construct is very simple: It's a structure that stores data, functions, or both. The idea being that these structures have a sense of "self" which is implicit in most languages outside of Python. This is called a "descriptor" and gives the object a point of self reference that binds data (variables or fields) and functions (typically called methods) to the particular object in question. The idea being that you are specifically using a variable or method that belongs to that particular instance (allocated block of memory typically underneath) rather than to some larger, more general construct.

Object systems tend to vary with regards to two big categories: Inheritance and Access.

Some, like Java or C++, have you declare classes that act as "blueprints" for objects that then become allocated. These classes and their objects cannot be structurally modified once instantiated. They can have their contents overridden in the sense that variables can change but their structure is static. You can't, for example, add new methods to HashMap in Java. You can extend the interface (basically partially implemented classes serving as contracts) or make a subclass to get the extra methods or variables you need along with all of the original variables and methods of the particular class in question.

Other class based languages, the most typically cited one being Ruby, allow you to easily open up an existing class and just add in methods as you see fit. This is a bone of contention and considered by many to be very, very dangerous.

Javascript is even looser, Objects are nothing more than a collection of slots for either variables or functions. They can be changed or overwritten whenever the programmer feels the need to do so. They can even be arbitrarily cloned as "Prototypes" for other objects, thereby passing all of their abilities on.

Access control is the other big point of difference between the various languages.

Some languages like Java have very strictly enforced access modifiers like "private" and "protected", that define exactly what classes and subclasses can use a given variable or method.

Others, such as Python are less formal, using the convention of an underscore before the method or variable name to indicate that it is private.

Ultimately, Python is a perfectly legitimate language to program in an object-oriented fashion, it just doesn't enforce it quite as rigorously as some of the others.

share|improve this answer
    
Might be a bit too technical for this question. –  Zeroth Feb 18 at 23:29
    
@zeroth, and too specific too. The 'self' (or 'this') parameter is far from universal. –  Javier Feb 19 at 1:06
    
@Javier Which is why I said that it is explicit in Python and implicit elsewhere. –  World Engineer Feb 19 at 1:13
    
The question is about learning OOP and I think discussing how a language is written to accomplish certain concepts such as Encapsulation where we have private for Java and underscore (__) for Python is fairly important. I think the abstraction level of Python might make it a little more challenging for a beginner, but definitely not impossible. Java spells it out for the programmer which might make some concepts stick a little more easily. –  Derek W Feb 19 at 3:22
    
@WorldEngineer i'm not talking about being explicit/implicit (also, Python is not unusual on this), but about simply existing as a concept. Some languages do polymorphic dispatch on all parameters, not just on the first. Some use other hints for the specific version, and the main style might not privilege an argument as 'this'. –  Javier Feb 19 at 13:39

Learning OOP principles is not language specific at all, so if by "learn OOP" you mean "learn what the terminology means, what OOP is and why I might want to use it" then the language doesn't matter.

If you mean "learn how to develop using OOP" then yes, different languages handle it differently, but they all share the same set of principles. If you're anything like me you'll learn it best by just doing it. Choose an object-oriented language with a good tutorial or book that covers the object oriented aspects and have at it. If you have the principles of object oriented design down, you'll be able to use them in any other OO language.

"Python is so new that's why you can't learn OOP on Pyton. you learn Java then understand OOP on Java"

This just makes my head hurt. So much wrong crammed into one sentence.

Python dates to 1989.

http://python-history.blogspot.com/2009/01/brief-timeline-of-python.html

Java to 1995.

http://www.oracle.com/technetwork/java/javase/overview/javahistory-index-198355.html

You can draw your own conclusions on the validity of that bit of advice...

share|improve this answer
1  
Yes but Python 1.0 wasn't until 1994. Java was in development for quite sometime before it was released too. But I agree that Python has OO and you CAN learn the concepts of OO. It's absurd to say otherwise. –  chubbsondubs Feb 19 at 0:55
2  
@chubbsondubs Yep, that's still before Java! ;) –  Izkata Feb 19 at 1:34
1  
To just provide a light counter-point, class-based OOP (C++, Java, Python, and many others) is fairly different from prototype-based OOP (JavaScript is the only language I know that uses this). They’re still OOP and have the same fundamentals and serve a lot of the same goals, but a lot of people consider classes a part of what they consider OOP, while JavaScript doesn’t have them and is definitely object-oriented. –  KRyan Feb 19 at 2:52
    
@KRyan: In case you care, there is one other prototype-based OO language that's named Self. –  Jerry Coffin Feb 19 at 7:39
    
ActionScript has both prototypes and classes. –  OrangeDog Feb 19 at 10:38

I think the person you're quoting was simply displaying language chauvinism.

In reality, there is a difference between concepts of OOP and implementation of OOP. Poetically, this is best understood once you have a better grasp of some of the concepts of OOP.

A lot of programmers become comfortable with just a few similar languages, so they don't have to expand their capabilities or suffer not being capable for a time.

So the question comes down to, really, will Python teach you the concepts of OOP?

I will say you can, provided you have adequate learning support that pushes you try things you wouldn't normally explore in self-directed learning. A book or a mentor of some sort would be best. Mark Lutz writes very in-depth, very detailed, and very excellent books on Python, and I would recommend his books because they will push you to do and learn more.

What you have to remember is that Python's way is not the Only Way, nor is it the Only Correct Way. The more programming paradigms you master, the better a programmer you will become. The way Python does OOP is not exactly how C++ or Java does it, but the concepts transfer well.

share|improve this answer
4  
In terms of OOP, Java and C++ differ from each other as much as either differs from Python. –  Steven Burnap Feb 18 at 22:50
    
To an extent, but that is largely due to changing language design knowledge and different constraints. –  Zeroth Feb 18 at 22:51
    
And frankly, 90% of the OOP concepts transfer well between them, almost invisibly even. –  Zeroth Feb 18 at 22:51
    
Yup, I agree. I find it a bit funny to think of Java and C++ being "the same" in terms of OOP. –  Steven Burnap Feb 18 at 22:55

Yes, OOP implementations are very different. The theory and principles are the same though, but so many people consider only "real OOP" what Java and C++ do, that you get many references that are supposed to be "language agnostic" but in fact assume class-based, statically typed languages.

That doesn't mean those references are bad, or even limited; for example the "gang of four" (GoF) seminal book "Design Patterns: Elements of Reusable Object-Oriented Software" is a prime example of a great work that says "OOP" when it really means "static class based OOP".

So, in my opinion: Yes, you can learn a lot of OOP on Python, Javascript, C, and many other languages; but some people (maybe a future employer) when ask for "OOP experience" mean Java/C++/C#. So, it would be wise to check that other view too.

(and those aren't the only two 'types' of OOP....)

share|improve this answer
2  
Ironically static, class-based OOP isn't what Alan Key originally suggested as OOP. But a lot of "learning" OOP is about learning design/coding-sense that isn't strictly limited to a particular OOP implemenation –  jozefg Feb 18 at 21:46
2  
It might also make sense to look at CLOS: I do not think many languages / frameworks offer multimethods. –  Giorgio Feb 18 at 22:18
2  
@jozefg: right, Alan Kay once said "Actually I made up the term "object-oriented", and I can tell you I did not have C++ in mind." –  Javier Feb 18 at 22:58
1  
@Giorgio Actually Dylan might provide a slightly simpler version of CLOS (no way to specify when a multimethod is run, but much simpler to approach) –  jozefg Feb 18 at 23:13

Object Oriented Programming is an idea about how to structure programming language to foster low coupling, information hiding (aka encapsulation), bundling data and methods that operate on that data together, and code reuse. Lots of languages implement their take on these ideas so there are variations between languages about how they approach Object Orientation. For example, Java only allows a class to extend 1 class. However, Python and C++ allow you to extend any number of classes. Java have specific reasons for its limitations. Limitations that are meant to correct things from C++, but also because Smalltalk only supports a single base class.

OO languages can be grouped into two families. The Smalltalk family (or class based OOP) of languages include C++, Java, Smalltalk, Ruby, C#, Python to name a few (there are tons in this family). These are a mix of statically and dynamically type languages and while they differ slightly on some concepts they are very similar in how they think about OOP. What I mean by that is how they approach coupling, encapsulation, binding data and methods, and code reuse, and the tools they provide for you to do that. Within this family lot of the concepts are the same.

The other family is the protoype based OOP. These languages look very different in their OOP implementation. Probably the best known example of this is Javascript, but Javascript copied these ideas from Scheme and Object LISP. These are lesser known languages and typically dynamically typed. I can't think of a statically typed prototype based language, but that doesn't mean there isn't one. You can read up on it here: http://en.wikipedia.org/wiki/Prototype-based_programming. The point is they approach OOP in a very different way than the class based languages. That means concepts aren't as portable between these two families. Just because you know OO in one family doesn't mean you'll easily transition those ideas over to the other family.

Bear in mind that most programming languages mix concepts from lots of ideas. Python and Ruby incorporate both OOP and functional programming ideas into their languages. And you can mix prototype based OO with certain extensions to class based languages so it's even more complex.

share|improve this answer
    
I wouldn't count C++ into the Smalltalk family. Smalltalk is based on Simula, and Alan Kay deliberately ignored most of the changes in Simula-II, whereas C++ is based on that. C# is strange, it was designed by C++ users and Pascal designers to be like Java. –  Jörg W Mittag Feb 19 at 15:23

OOP is a principle of programming - it's fundamentally an idea. The implementation of OOP varies throughout programming languages - but the pillars (Abstraction, Inheritance, Encapsulation, and Polymorphism) of OOP are usually present in some way or form.

I will say without preference to either language, that Java pushes the semantics a little harder on the programmer than say Python.

For example,

Java code: class Cat extends Animal {}

Is a little more obvious as to what you are doing in the OOP viewpoint of things than:

Python code: class Cat(Animal):

Sure they both define a class hierarchy in which Cat inherits from Animal. But I feel that for a programmer just starting out in OOP, the application and implications of OOP ideas might stick a little better in Java since it is spelling it out for the programmer.

share|improve this answer

You are explicitly asking what the easiest language is to learn OOP concepts. I think the answer is clear: python.

To show why this is the case, let's look at the typical beginner program in Java vs Python. Let's make a simple higher-lower guessing game.

Java

In Java, you will write a Game class.

public class Game {
    public int secretNumber;
    public int tries;

    public Game(int tries, int secretNumber) {
        this.tries = tries;
        this.secretNumber = secretNumber;
    }

    public void guess(int myNumber) {
        if(myNumber > secretNumber)
            System.out.println("Your guess is too high!");
        else if (myNumber < secretNumber)
            System.out.println("Your guess is too low!");
        else
            System.out.println("You win!!");
        tries = tries - 1;
        if(tries == 0) {
            System.out.println("No more tries: you lose :(");
        }
    }

    public static void main(String[] args) {
        Game game = new Game(10, 47);
        while(true) {
            // how do I read a newline again? Something with BufferedInputStreamReader?
            // how do I convert a String to a number?
        }
    }
}

I am an experienced programmer, and even I am having difficulties. Furthermore, look at what you need to explain to a prospective pupil for this simple program:

  • Static methods
  • Visibility (public vs private). You should always mark fields as private.
  • this. notation for referring to variables when they are masked by others
  • The constructor resembles a method, but is not a method.
  • System.out is an outputStream. And yes, it is a field, but it's a static field.
  • Sometimes, you can omit the curly brackets if there is only one statement
  • String[] is an array. It is a special type of object, but then again, not really.
  • int is a primitive type. It is special.
  • You need a lot of framework methods

Python

Python is a lot 'purer'. There are no primitive types. A constructor does not exist, there is only a special method that is called at initialization.

You do not need to interact with the console, as you have a REPL. You can simply play the game using g.guess(35) and returning a string.

This makes the language easier to learn and grasp the basic OOP concepts.

share|improve this answer

The core idea in OOP is encapsulating (which means bundling or hiding) variables and methods together inside of classes (which python absolutely supports). It's about designing your code around Nouns. Then it kind of goes on from there.

While there are implementation differences (for example, python doesn't support visibility the way Java does) and syntax differences (in Javascript, you have to inherit methods yourself), the basic design stays the same.

I do think it's easier to learn OOP in a language like Java, though, because the language requires it and the community is less adverse to it than, say, the python community.

But there is tons of writing about OOP design & practices out there, and reading it's not wasted effort. Even when I write python (which is a lot), I write lots of objects, and I use a lot of objects from third-party libraries.

share|improve this answer
    
The code idea is message passing. Also no source. –  User Feb 19 at 9:25
1  
OOP is not about classes. –  Jörg W Mittag Feb 19 at 15:24
1  
@User Alan Kay's original idea was about messaging, but since then the practice of OO has evolved into design by noun, or classes of objects. For example, Dr. Kay has also opinioned that Java is not Object Oriented, because it doesn't support late binding. @ Jorg lol ya ya –  Rob Y Feb 19 at 16:06

When you learn OOP in a language you start to think in this language. The language influences what you think can be done and how and also adds a flavor to OOP.

  • Should there be a Garbage Collection?
  • Can I add methods to integers?
  • Do I use classes or prototypes?
  • How do objects reflect on themselves?

Some people can not do OOP without classes. Some must kill their objects with the process.

There is a core of OOP and the original ideas about it. You can have a look also at Smalltalk, Self, Simula, LISP, Newspeak. And also a look at non-OOP language types like dataflow languages bash, APL, J. Logical ones Prolog. Haskell (type classes). They will all teach you a different thinking and you may learn that

  • OOP is not about imperative languages
  • OOP is not about classes

And in the end you may see what OOP is good for. At least you will have another idea about it. I suggest finding talks by Alan Kay.


If you do not look at the languages this post is useless.

As you can see here: We can not agree on what OOP is.

share|improve this answer

protected by World Engineer Feb 19 at 3:19

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.