Questions on Object Oriented Programming

  • Thread starter cesaruelas
  • Start date
  • Tags
    Programming
In summary: Could you please provide a more detailed explanation of what you mean by "object oriented programming"?
  • #1
cesaruelas
53
0
I don't think I fully understand what "object oriented" actually means. Anyway, is it useful for a physics major hoping to get into grad. school to know object oriented programming? I have yet to take two compulsory computational physics courses where we will cover several numerical methods for solving equations (non-linear, odes, pdes, integrals, schrödinger etc.), monte carlo methods, genetic algorithms and molecular dynamics. Would it be a waste of credits to take object oriented programming beforehand?
 
Physics news on Phys.org
  • #2
cesaruelas said:
I don't think I fully understand what "object oriented" actually means. Anyway, is it useful for a physics major hoping to get into grad. school to know object oriented programming? I have yet to take two compulsory computational physics courses where we will cover several numerical methods for solving equations (non-linear, odes, pdes, integrals, schrödinger etc.), monte carlo methods, genetic algorithms and molecular dynamics. Would it be a waste of credits to take object oriented programming beforehand?

I can answer this question from a game development point of view, but no more. If you have done Java, "classes" are objects. An object is a way to visualize certain elements of your code. In a FPS game, an object could be your character, your gun, the game map etc.
In a circuit model, an object could be a resistor, a capacitor, a transistor etc. The object is a chunk of code that distinguishes it from other objects by its unique name as well its unique methods that allows it to interact with other objects in the program.

The advantage of objects is that each object has its own set of methods, and those methods can be controlled selectively by the invocation of the object's class name. In addition, objects can be duplicated with ease, and they can also be destroyed with ease. Objects also make planning the program much easier because they destroy a barrier between the abstraction of code and the intuition of the program. This is extremely important.

An object in the sense of the computer science definition, I should clarify, refers not to a single object in the regular sense of the word, but rather to a species of objects (in the regular sense of the word). So the "player" class in a blackjack program is a general template for the players of the game. Each player of the game is an "instance" of the more general "player" class. Each instance can interact with other instances of the player object, or even with instances of other objects.

BiP
 
  • #3
Thank you, BiP! Now could you give some insight as to whether or not it would be an advantage when taking computational physics courses? Would taking a course on object oriented programming be a waste of credits or would it actually improve my programming skills, regarding the implementation of numerical methods?
 
  • #4
cesaruelas said:
Thank you, BiP! Now could you give some insight as to whether or not it would be an advantage when taking computational physics courses? Would taking a course on object oriented programming be a waste of credits or would it actually improve my programming skills, regarding the implementation of numerical methods?

I don't know much about computational physics. To be honest, I cannot think of any programming courses that are taught other than object oriented programming. Perhaps this is because I am young, so I learned only the new programming languages, all of which have OOP. Old languages didn't pack the feature of OOP.

If you are comparing OOP with a non-OOP course, I don't really know what to say because I can't imagine programming without objects. I would say take OOP, since it is so fundamental to good technique, and is not too hard to pick up.

Bt OOP can also easily be self-taught. In fact, programming in general is *mostly* a self-taught skill; you just need the books (or videos).

But if you compare OOP with a math course, take the math course and self-learn the OOP.

Just to clarify, have you done programming before? What languages, if any?

BiP
 
  • #5
I've programmed some basic methods in C. Newton-Raphson, bisection, finite difference, romberg integration, matrix inversion, etc... I have also several numerical methods programmed in MATLAB. Maybe I've been doing OOP all along and I don't even know because I didn't know what the term exactly means haha.
 
  • #6
If you are using C, you haven't done object oriented programming. C++ includes object orientation.

The advantage to object orientation is that it let's you organise your data in a natural way, as opposed to the way that is convenient in the language. For example, a simple simulation of the solar system needs you to store the x, y and z positions, x, y and z velocities, masses, and names of all the planets. In C, that would be something like:
Code:
int main(void) {
    double x[9],y[9],z[9];
    double vx[9],vy[9],vz[9];
    double M[9];
    char *name[9];
    //...
}
Note that you have arranged all of the x values in one data structure, all of the y values in another structure, and so on. That is completely orthogonal to the "natural" way to think about the simulation, which is nine objects. In C++, you could define a class:
Code:
class Planet {
    double x,y,z;
    double vx,vy,vz;
    double M;
    char *name;
};
and create nine instances of it (I know, I know, arrays of objects on the stack, but I don't want to get into malloc/new):
Code:
int main(void) {
    Planet planets[9];
    // ...
}
Now all of your data about one planet is wrapped together, and you just create an array of 9 Planets - a much more natural way to think about the model. Object orientation also let's you define functions inside the a class (called "methods") that let you manipulate the data in the class - you'd probably want to define a "move" method updates the position and velocity of the Planet, and probably others. Also, you can declare that the variables defined in the class can only be manipulated by the methods of that class - anything outside the class must call a method called something like getX() to get the x-coordinate. Then, if you suddenly realize that everything would have been easier if you'd done it all in spherical polar coordinates, you know that you do not need to look beyond the bounds of the class for dependencies - nothing outside ever knew how the guts worked. Object orientation also provides an extremely powerful way to extend classes - for example, you can define a Spaceship class as "just like a Planet, but with a few extra attributes and a slightly different way of moving".

That's a very quick overview of object oriented programming. It is a very useful tool for constructing complicated programs by breaking the task down into manageable chunks and formally specifying the way that the chunks will talk to one another.

It is not, I think, really useful for numerical methods. For all their complexity, numerical methods boil down to traversing arrays of numbers, doing mathematical operations on them, and doing it fast. They don't need to be split up into separate black-boxes, and would suffer performance penalties in doing so - unacceptable inside a do-this-a-billion-times loop. While I would very much recommend learning object oriented programming if you are ever intending to develop anything with complicated data structures, the nuts and bolts of numerical computing is about the only area I can think of that wouldn't benefit from it.
 
  • #7
cesaruelas said:
Would taking a course on object oriented programming be a waste of credits or would it actually improve my programming skills, regarding the implementation of numerical methods?
Why such a narrow focus? An OO programming class probably won't help you do better in a numerical methods class. Your numerical methods classes are going to be focused on algorithms, and possibly some down and dirty stuff such as how to tailor an algorithm to better take advantage of cache memory.

That doesn't mean it's not a good idea to take such a class. The mindset should be that taking classes such as this as technical electives will make you a better and more employable scientist or engineer. From far too many years of experience, the ability to program properly is a skill that most scientists and engineers have not learned.

To answer your narrow focus question, there is a computer science class that will help you improve your skills with regard to numerical methods. That's the data structures & algorithms class offered by almost every computer science department, typically at the sophomore level. This class typically has "Introduction to Programming" as a prerequisite, and that intro to programming class nowadays is most likely going to use an object oriented language.
 
  • #8
D H said:
Why such a narrow focus?

I have such a "narrow focus" because I have only certain amount of credits for free electives and I would rather use them on courses more related to physics (atomic force microscopy, intro nuclear and particle physics, molecular modelling, that sort of stuff which isn't compulsory for the major). If I can understand computational physics without needing to take a course on OOP then I'll do it that way. Courses on algorithms you mention sound interesting, though. I'll try to look into that. Thank you.
 
  • #9
Ibix said:
That's a very quick overview of object oriented programming. It is a very useful tool for constructing complicated programs by breaking the task down into manageable chunks and formally specifying the way that the chunks will talk to one another.

It is not, I think, really useful for numerical methods. For all their complexity, numerical methods boil down to traversing arrays of numbers, doing mathematical operations on them, and doing it fast. They don't need to be split up into separate black-boxes, and would suffer performance penalties in doing so - unacceptable inside a do-this-a-billion-times loop. While I would very much recommend learning object oriented programming if you are ever intending to develop anything with complicated data structures, the nuts and bolts of numerical computing is about the only area I can think of that wouldn't benefit from it.

Thank you, Ibix! You answered my question and even more. I'll take this into account when registering my courses for the next semester.
 
  • #10
Hey cesaruelas.

From a memory point of view objects are just data structures.

You can embed structures within structures and this is what inheritance does.

The structures can contain not just actual data (like integers, floats, and so on) but also pointers and these pointers can point to functions as well as data objects.

This is basically what a class instance looks like in memory: it's just a struct object in C with a lot of pointers alongside the data to accomplish all the Object Oriented Stuff that the compiler and the language provide.

Accessing the elements is just looking up the right memory address for the start of the instance and then moving to the offset for the particular element of the structure.

The linguistic perspective is different and C++ books cover this in depth but all it really is in memory is a struct and this is just a standard C-based procedural programming construct.
 
  • #11
Ibix said:
For all their complexity, numerical methods boil down to traversing arrays of numbers, doing mathematical operations on them, and doing it fast. They don't need to be split up into separate black-boxes, and would suffer performance penalties in doing so - unacceptable inside a do-this-a-billion-times loop. While I would very much recommend learning object oriented programming if you are ever intending to develop anything with complicated data structures, the nuts and bolts of numerical computing is about the only area I can think of that wouldn't benefit from it.

That was true for programming back in about 1960, but as a methodology for developing numerical software it passed its sell-by date round about 1980.

(It's probably all you need to know to pass a "cook-book" first course in numerical methods, but that's beside the point).

As D.H. said, the key to writing efficient large-scale software that runs on multiprocessing hardware is what is covered in a typical "data structures and (non-numerical) algorithms" course, not the (small number of) lines of code that do the clever arithmetic.

You can take whatever view you like about OOP as a philosophy for software development, but for any modern programming language the libraries that implemnt those "data structures and algorithms" will almost certainly have an interface that is designed on OOP principles - so in real-world programming, "resistance to OOP is futile".
 
  • #12
AlephZero said:
That was true for programming back in about 1960, but as a methodology for developing numerical software it passed its sell-by date round about 1980.

(It's probably all you need to know to pass a "cook-book" first course in numerical methods, but that's beside the point).
Yes. In among all of my post, I only answered half the question, and the less important half at that.

You do not need object oriented programming techniques to understand how (say) a numerical integrator works (which is what I said). And if you want to write numerical integrators, you can probably get away without object oriented programming. However, you will need to understand (at least) the basics to do more or less anything else, including actually using the numerical integrator (which I should have said). It will almost certainly be written for you by someone who wrapped it in an object oriented package.

I used to use numpy quite a lot. It's a numerical library for the python programming language, and a case in point. Its Fast Fourier Transform (FFT) capability is based (I think) on the FFTW library, which is written in C (i.e., non-object oriented). But python is object oriented, and there is almost nothing FFT-related that you could do without understanding object orientation. You can, if you like, write C code and link it against the FFTW library - but (from experience!) it'll take four times as long and be much harder than doing in python.
 

What is Object Oriented Programming?

Object Oriented Programming (OOP) is a programming paradigm that focuses on creating objects that contain both data and functions. It allows for the organization and management of complex code, making it easier to maintain and reuse.

What are the main principles of Object Oriented Programming?

The main principles of Object Oriented Programming are encapsulation, inheritance, and polymorphism. Encapsulation refers to the bundling of data and functions within objects, inheritance allows for the reuse of code from parent objects, and polymorphism allows for objects to have different forms or behaviors based on their type or class.

What is the difference between a class and an object?

A class is a blueprint or template for creating objects, while an object is an instance of a class. A class defines the properties and behaviors that an object will have, while an object is an actual entity that exists in memory.

What is the purpose of constructors in Object Oriented Programming?

Constructors are special methods used to initialize objects when they are created. They allow for the setting of initial values for object properties, making it easier to create and work with objects in a consistent manner.

How does Object Oriented Programming promote code reusability?

Object Oriented Programming promotes code reusability through the use of inheritance. By creating parent classes with common properties and behaviors, child classes can inherit and reuse code from their parent. This reduces the need to write repetitive code and makes it easier to maintain and update code in the future.

Similar threads

  • STEM Academic Advising
Replies
2
Views
1K
  • STEM Academic Advising
Replies
11
Views
656
  • Programming and Computer Science
Replies
18
Views
4K
  • STEM Academic Advising
Replies
13
Views
2K
  • STEM Academic Advising
Replies
1
Views
916
  • STEM Academic Advising
Replies
5
Views
2K
  • STEM Academic Advising
Replies
2
Views
2K
  • STEM Academic Advising
Replies
1
Views
1K
  • STEM Academic Advising
Replies
4
Views
2K
  • STEM Academic Advising
Replies
16
Views
2K
Back
Top