Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Struggling understanding what OOP is about

  1. Dec 24, 2015 #1
    Hello Forum,

    I understand that Java is an example of a computer language based on object oriented programming. I have read about objects, classes, etc. and I think I get those ideas. The other type of programming, opposite to OOP, is procedural programming: the computer simply receives a logical list of commands.

    I have used Matlab in the past. To me, it seems like everything needs to be specified in the matlab code but it is also possible to write m-files and call them from other matlab programs. For example, I could write a m-file to draw a house and call that file from my main program. That said, I don't think Matlab is considered OOP. Is that correct? I am trying to compare Matlab to Java to see if I can better understand what OOP is about and what it is not...

    thanks,
    fog37
     
  2. jcsd
  3. Dec 24, 2015 #2
    There is no definition of OOP that everyone agrees upon, there is only broad agreement on the set of key characteristics. I don't know a better brief treatment of them than http://www.stroustrup.com/whatis.pdf.
     
  4. Dec 24, 2015 #3

    Student100

    User Avatar
    Education Advisor
    Gold Member

  5. Dec 24, 2015 #4
    Thank you. I will read what is on the links.

    For instance, Let's say I want the matlab program I am writing to draw a sphere. I previously wrote a general and short m-file, a function called sphere.m, for drawing spheres of all sorts. I could call that m-file into the the matlab program I am writing. Am I doing OOP in that case?
     
  6. Dec 24, 2015 #5

    phinds

    User Avatar
    Gold Member

    Well, not quite. For most (all?) C++ compilers you are not required to use OOP, but JAVA is inherently OOP and you don't have a choice about it. You CAN write procedural code within a class but technically it's still OOP.

    This message was brought to you by the nitpicker in chief :smile:
     
    Last edited: Dec 24, 2015
  7. Dec 24, 2015 #6

    jtbell

    User Avatar

    Staff: Mentor

    And you can do some aspects of object-oriented programming in C by using function pointers in structs to implement member functions of objects. However, all struct members are "public"; IFAIK there's nothing like C++'s private member data and functions in classes. And there's no inheritance between structs in C.
     
  8. Dec 24, 2015 #7

    ChrisVer

    User Avatar
    Gold Member

    Is Matlab really considered a language? I thought it was more like an environment like mathematica etc.
     
  9. Dec 24, 2015 #8

    FactChecker

    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    MATLAB is a fairly sophisticated language. There is also a large environment that it works within. The environment includes an editor, tools for auto-generating C/C++ code, interfaces with Simulink, modeling standards checking, etc.
     
  10. Dec 24, 2015 #9

    Mark44

    Staff: Mentor

    Yes, it is a language.
     
  11. Dec 24, 2015 #10

    phinds

    User Avatar
    Gold Member

    All major programming languages have been evolving for decades and becoming not "just" languages but huge development environments. That is, the languages themselves have evolved only some (although some have become, really, new languages, such as VB6 -> VB.NET) but the development environments have evolved enormously to where today, unless you are really using a stripped down compiler environment to write a console app, if you had nothing but the language itself, you would have damned near nothing of any use.

    For example, many decades ago I wrote some programs in BASIC. The entire "development environment" was an interpreter. Almost 25 years ago Microsoft released Visual Basic which was a significant evolution from BASIC and the last incarnation of which was VB6. By the time it got to VB6 the development environment had swamped the language itself in terms of overall capabilities. Today I write in the successor language VB.NET, which is a significant evolution from VB6, not just an extenision, and the development environment is far more complex than VB6.

    If I had nothing but VB.NET as a language, and just a syntax compiler, I could not do squat without spending MANY hours doing each of the things that are just a mouse-click or two in the development environment that is the totality of VB.NET

    And so it goes ...

    My point being that trying to distinguish between a "language" and a "development environment" in today's world is a waste of time.
     
  12. Dec 25, 2015 #11

    meBigGuy

    User Avatar
    Gold Member

    I may be wrong, but from what I remember your sphere.m will probably create a sphere object (lets call it sphere1) and you can access sphere1.volume, or pass it to other objects that will inherit its methods and data. That is pretty much OOP. (maybe your sphere.m doesn't do that?)

    If it were procedural, you would define a sphere with some data in variable (say, sphere1_diam), and operate on it with math procedures to determine the volume. You could stick them and some function pointers in a generic sphere array, and that would start to resemble an object.

    In matlab, the sphere and its methods (functions) and data are encapsulated in a sphere object.
     
  13. Dec 25, 2015 #12
    You could consider your sphere.m file to be a module. One of the goals of object oriented programming is modularity. But what defines object oriented programming, while it may vary, is generally following the adherence to a set of programming principles. In contrast, an object oriented language is a language that has built in features to support this style of programming, and in some cases such as Java, the exclusion of features that support other paradigms.

    This would be procedural programming using an object oriented language. Again, there is a difference between the style of programming and the language.

    Maybe a few languages have become tied to an environment. Most are independent of the development environment. And you certainly don't need an IDE to make graphical programs. In most major programming languages, not using an IDE dosn't impose any limitations on what you can produce, although an IDE might offer some convenience for beginners, or include some nice integrated debugging or profiling tool.
     
    Last edited: Dec 25, 2015
  14. Dec 25, 2015 #13

    phinds

    User Avatar
    Gold Member

    Yeah, I think my thinking has somewhat been warped by VB.NET where you ARE tied to the environment and the basic language syntax is just a small part of the overall development process. I've done lots of programming other languages and I agree w/ you but all of the basic languages like C, C++, C##, Java and so forth are very dependent on system calls and each language's function libraries to do a lot of the heavy lifting. If you only know the syntax of the language and don't have a good knowledge of that other stuff, you aren't going to get much done outside of straight number crunching.

    I still do, on occasion, write console apps (using VB.NET) but they're pretty primitive compare to windows apps.
     
  15. Dec 25, 2015 #14
    I guess I was thinking about IDE's. I suppose more of what you said is true, and less of what I've said is true, if we're talking about libraries, run-time environments, and interpreters.
     
  16. Dec 25, 2015 #15

    phinds

    User Avatar
    Gold Member

    Right. Compilers and an interpreters are fundamental to developing code in a language and the syntactical aspects of the language ARE the language. Everything else is support and that's what I'm talking about. Without the support structure, you really don't get much beyond writing simple console apps and number crunching programs, especially compared with what you can do WITH all the support stuff. I can pop out a windows app today in a few hours that would have taken me weeks in the early days of windows when I was using C and bringing in the windows system libraries. Today I just drag and drop stuff from a toolbox onto a form and there's my GUI that used to take a ton of painstaking work.
     
  17. Dec 25, 2015 #16

    jtbell

    User Avatar

    Staff: Mentor

    For most general-purpose languages, IDEs and the libraries for programming for a specific OS are separate issues, in principle at least. You can write a call to a function for opening a window, using a plain old text editor, and compile/link using command-line tools. It just becomes a chore working out the correct parameters for e.g. window size and location, and remembering which libraries to link. The latter is where makefiles come into play on Unix-like systems.
     
  18. Dec 25, 2015 #17

    phinds

    User Avatar
    Gold Member

    So, @fog37, have we all just been talking to ourselves our are you getting anything out of this? :smile:
     
  19. Dec 26, 2015 #18
    Its rare to see so much chatter on a thread on PF (much of it interesting) without seeing a strait-forward answer to the OP's question. So I will throw mine in.

    OOP is a way to organize code in objects, which are collections of declared variables (sometimes called members or attributes of the object) and functions (called methods of the object). It can be contrasted with functional programming languages where the top level entities are functions, rather than objects, and the program executes by passing structured data into functions. In OOP, the structured data is grouped in an object, along with a collection of method functions which are part of that object and act upon the data of that object when called.

    I don't know your programming history, but if you have a structured collections of data (struct) which includes functions that can act upon it in the same class/object, that's OOP. If you have structs which are passed to independent functions which act upon them, its not.

    Simply, an OOP language will give you a person object, with a method sayName(), that will draw upon information (members/attributes) of the person, like what their name is. So you will write bob.sayName() to get "bob" output. In other paradigms, you will pass the Bob struct, which includes Bob's name, to the sayName() function to get the same result. Its a different way of structuring things to achieve the same results all languages achieve.
     
  20. Dec 26, 2015 #19
    You guys are great. Thank you.
    Still digesting all all these valuable infos. I know matlab to a superficial level (m-files, arrays, functions). I have never seen anything written as sphere1.volume. What type of object is that?

    Could any of you provide a trivial example and explain how I would write it in matlab and in java? Maybe it would make things less abstract. To me programming is simply a set of instructions and calling of functions (which are also programs stored somewhere else) to manipulate data. Arrays, structures, variables are efficient ways to organize the data. Functions, instead, act on the data to produce new output data, visualize the data, etc.

    I think I understand modular programming, i.e. creating small programs all linked together to achieve a specific task....
     
  21. Dec 26, 2015 #20

    anorlunda

    Staff: Mentor

    You will have to think a lot more about programming and different ways to approach programming problems before you'll be answer your own question "What is it all about." Learning how to do it in different languages won't answer that question. If you're not interested enough to study it, then you should drop the question. There are no trivial answers.
     
  22. Dec 26, 2015 #21

    ChrisVer

    User Avatar
    Gold Member

    I think he means using a method from a class of spheres... as an example suppose you make a class Sphere, in that class you define a method that is volume(), which determines the volume of your sphere. Then if you define an object sphere1 from class Sphere, you can access its method "volume" by writing sphere1.volume().
    I am not used in Java (I'm a newbie there) or Matlab (I only saw a couple of applications of it in my life)... but for the same example in c++ :
    Code (C):

    #include <iostream>
    #include <cmath>

    class Sphere {
    //attributes of  Sphere
    double radius;

    //methods of Sphere
    public:
    void set_radius(double rad){         //define a class method that allows you to set the radius of a circle
        radius= rad;    //the radius of the sphere is set equal to the radius (rad) you input
        }
    double get_radius(){
       return radius;
       }
    double volume(){         //method that returns the volume
       return 4*3.14*pow(radius,3.)/3;
       }
    };


    int main(){
        Sphere sphere1 ; // initiallize the object sphere1
        double vol, radius;
        sphere1.set_radius(1.2) ; //set the radius of the sphere equal to 1.2
        vol = sphere1.volume();  //assigns to my variable vol the result of the volume's method of the class Sphere
        radius = sphere1.get_radius();
        std::cout << "volume of sphere of radius R = " << radius << ", is : V = " << vol <<std::endl;
        return 0;
    }
     
    I didn't try if this works, but I think it should... see how the object.method works?
    Sometimes if you work with pointers instead the "." operator should be replaced with the operator "->"
    Of course you could play with more than one spheres...but still you'll be using the same methods for all of them.
     
    Last edited: Dec 26, 2015
  23. Dec 26, 2015 #22

    meBigGuy

    User Avatar
    Gold Member

    This talks about OOP in matlab. I used it this way once in the distant past, so assumed that was just the way it worked.
    http://www.mathworks.com/discovery/...amming.html?requestedDomain=www.mathworks.com

    In summary:
    A class is the definition of an encapsulation of the data and methods (functions) needed to define/use a particular object. You access these through the object handle that you created when you declared the object.
    Say you declare a sphere called sphere1: sphere sphere1 (diameter);
    You now have an object (handle) sphere1 that represents a sphere.
    You can access its data and methods through the handle. For example sphere1.volume would access the volume of sphere1
    The growVolume method sphere1.growVolume(1.3) would increase its volume by 1.3.

    You can declare sphere1, sphere2, sphere3, etc, all with different characteristics, and access them through their handles.

    You could pass sphere1 to another class that uses spheres. For example, planet. planet1 = planet (sphere1, density);
    planet1 would inherit all the data and methods of "sphere1", plus its own planet data and methods.
    for example it's gravity could be accessed as planet1.gravity.

    Of course, when designing the class you need to define all the functions and data storage to calculate gravity, etc. OOP is simply a way to keep them compartmentalized.
     
  24. Dec 27, 2015 #23
    Yeah, you give a good description there of programming, but when you say "Functions, instead, act on the data to produce new output data, visualize the data, etc.", where you present functions as above and acting on the data, that's not the OOP style, its functional style. In OOP, the functions are part of the data. The key thing is you can write in either style in either language. Using nothing but functions with the static keyword in Java, you can program in that functional style. Or you can do, OOP in Matlab.

    MeBigGuy gives a pretty good detailed explanation above, but in super simple terms, using psuedo code, an OOP program will look like this:

    Code (Text):

    //declare what all the customer objects will have, in the customer class
    class customer {
        //define each piece of data we are tracking on the customer
        string fullName;
        double moneySpent;
        //declare a function INSIDE the class that will be attached to each customer object
        boolean function isBigSpender() {
            if ( moneySpent > 1000) return True;
            return false;
       ....more here, constructurers etc
    }

    //now, we can define some customers
    cust1 = newCustomer();
    cust2 = newCustomer();
    cust1.fullName = "Alice Bobb";
    cust1.moneySpent = 200;
    cust2.fullName = "Charlie Debra";
    cust2.moneySpent = 2000;
    //now, we can call the functions attached to each customer through the customer class, its part of them
    cust1.isBigSpender() (returns false)
    cust2.isBigSpender() (returns true)

     
    The non-OOP way would make a top level function called isBigSpender(double moneySpent), and, the customer data would be stored in something like dictionaries structs or arrays, instead of storing it in the different customer objects, and it would accomplish the exact same thing. Most people working with mathy algorithm stuff use functional approach for its brevity, but when you get to teams of people working on some big enterprise thing, OOP is a must, its about not breaking your co-workers code by changing the structure of your data. With OOP its all in the same class, and there's an ordered way of subclassing and all this other stuff when you need to ad to or modify your coworkers code without screwing up their plans, and having it all work seemlessly together.
     
  25. Dec 27, 2015 #24

    FactChecker

    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    I am not sure that you understand the advantage that OOD can give you. Here is one example. Suppose you were programming a complicated display on the computer monitor with a large variety of dials, gauges, symbols, lists, etc. Suppose you want to be able to resize the entire display and have everything stay in the right proportions. Your main program would have a difficult time keeping track of how to resize and move each object and what functions to call for each one. But if the objects know how to resize themselves and they all have the same "resize" method that you can call, it would be easy. You could make a loop through all the objects and call resize:
    Code (Text):

    for( i=0; i<num_objects; i++ )
    {
       objects[i].resize(...);
    }
    NOTE: This is conceptual pseudocode.  Don't take it too literally
     
    You might have several dials that are of the same class "dial", but are in different positions and sizes. The class "dial" can define the general "resize" method for all dial objects. The different dials can keep track of their own position and size and do the appropriate resizing. So each object knows how to take care of itself and has the class methods for higher levels to call.
    In addition to resizing, you might want to add or delete things from the display. The "dial" class will define what has to be done to initialize a dial and to delete a dial. Those methods are called the "constructor" and "destructor" methods. Every class has those two methods. So you can make new objects at any time. and get rid of them when you are done with them.
     
  26. Dec 27, 2015 #25
    Despite the name, that's doesn't quite make it functional programming/style.

    https://en.wikipedia.org/wiki/Functional_programming
    https://en.wikipedia.org/wiki/Imperative_programming
     
    Last edited: Dec 27, 2015
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook