Struggling understanding what OOP is about

  • Thread starter fog37
  • Start date
  • Tags
    Oop
In summary, Java is a computer language based on object oriented programming while Matlab can be used for procedural programming.
  • #1
fog37
1,568
108
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
 
Technology news on Phys.org
  • #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
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?
 
  • #5
Student100 said:
Matlab can be used for OOP: http://www.mathworks.com/company/ne...to-object-oriented-programming-in-matlab.html

Java, Matlab, C, C++, etc. can all be used procedurally or object oriented, depending on the person doing the programming.
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:
  • #6
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.
 
  • #7
Is Matlab really considered a language? I thought it was more like an environment like mathematica etc.
 
  • #8
ChrisVer said:
Is Matlab really considered a language? I thought it was more like an environment like mathematica etc.
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.
 
  • #9
ChrisVer said:
Is Matlab really considered a language?
Yes, it is a language.
 
  • #10
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.
 
  • Like
Likes PeroK
  • #11
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.
 
  • #12
fog37 said:
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?

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.

phinds said:
You CAN write procedural code within a class but technically it's still OOP.
This would be procedural programming using an object oriented language. Again, there is a difference between the style of programming and the language.

phinds said:
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.
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:
  • #13
Jarvis323 said:
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. I've actually never used a language where you have to use a specific environment, or even where not using an IDE limits what you can do in any significant way.
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.
 
  • #14
phinds said:
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.

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.
 
  • #15
Jarvis323 said:
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.
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.
 
  • #16
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.
 
  • #17
So, @fog37, have we all just been talking to ourselves our are you getting anything out of this? :smile:
 
  • #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.
 
  • #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...
 
  • #20
fog37 said:
To me programming is simply a set of instructions and calling of functions (which are also programs stored somewhere else) to manipulate data.

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.
 
  • #21
fog37 said:
I have never seen anything written as sphere1.volume. What type of object is that?

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++ :
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:
  • #22
fog37 said:
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.
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/object-oriented-programming.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.
 
  • #23
fog37 said:
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.
...

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:
//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.
 
  • #24
fog37 said:
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...
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:
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.
 
  • #25
Fooality said:
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.

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:

Related to Struggling understanding what OOP is about

What is OOP?

OOP stands for Object-Oriented Programming. It is a programming paradigm that focuses on creating objects that contain both data and functions. This allows for more organized and modular code.

Why is it important to understand OOP?

Understanding OOP is important because it is one of the most widely used programming paradigms, and many popular programming languages such as Java, C++, and Python are based on it. It also allows for easier maintenance and scalability of code.

What are the main principles of OOP?

The main principles of OOP are encapsulation, inheritance, and polymorphism. Encapsulation refers to the ability to hide the internal workings of an object and only expose necessary information. Inheritance allows for the creation of new classes based on existing ones. Polymorphism allows for different objects to respond to the same function in different ways.

How does OOP differ from other programming paradigms?

OOP differs from other programming paradigms, such as procedural programming, by focusing on objects and their interactions, rather than just functions and procedures. This allows for more organized and reusable code.

What are some common applications of OOP?

OOP is commonly used in the development of software applications, web development, and game development. It is also used in artificial intelligence and machine learning applications.

Similar threads

  • Programming and Computer Science
Replies
4
Views
2K
Replies
6
Views
865
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
28
Views
1K
Back
Top