ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have tried to get my head around OOP as 'the world' told me it was the way to go. However in the simple examples that usually go with tutorials and books, I failed to see the difference between a class and a struct; anything in those simple examples could be done in C so what is all the fuzz about. The idea of a private member appealed to me till the example showed the setter method that still allowed access by any piece of the application that knows about the class; so much for protection. So there I was, not seeing advantage in using OOP, and falling back to the languages that I was familiar with.
However, there was that nagging feeling that 'the world' could not be that stupid so there had to be something. And after a while I tried again. I think I've tried about 5 times (from the days of TurboC++ through Borland C++ for windows 3.x till recently MS C#).
Now, due to a job change, the time has come and I have to use OOP. One of my first projects is JAVA related.
Wish me luck and maybe, one day, I will be able to explain advantages of OOP over normal languages and vice versa.
PS I bought 'java for dummies' and the impression is that it indeed seems to be able to clear some things for me.
First, all the best.You do raise a few important points about the OO approach.
The first is what is the different between a class and a struct. I see this essentially as a convenient way of grouping your code. With a class there is (built into the language) a mechanism of placing the data and the functionality together. This helps when it comes to designing the code and maintaining the code.
The other is with the concept of private member variables and a setter methods. First not all member variables need to have setter methods, or the setter methods could themselves be private, restricting access to member variable to the class itself. The other important point is that for setter methods this is where you put the validation. For example if you have a member variable that holds the ISBN for a book the setter method should validate that the checksum is correct.
I failed to see the difference between a class and a struct
Several important things have already been said on this thread, but since nobody else has said the following so far, please let me say it:
In C++, the only difference between a class and a struct is that in a class, members are private by default, and in a struct, members are public by default.
Inheritance, member functions, member functions as the only way to set private data, virtual functions, all the rest are available for both classes and structs in C++.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
OOP is a lot of hype, but it got stuck and some people can only think in OOP.
In procedural programming, you call a function and pass objects (variables, structs) as parameters on which you operate. In OOP you take an object and call the function to perform an operation on that object. There is not that much difference.
Problem is that once you master OOP, my experience is that it is difficult to change your mind and write again a procedural program. Procedural programming appears as programming inside out.
The big advantage of OOP? Polymorphism and Inheritance? Well, sometimes. If you have to program GUI environments (I mean writing the libraries, not using QT or so) it might be beneficial.
What I have against OOP is that it might be totally unreadable. If you have a pointer in your source code, do you know to what object it points? What method in the inheritance chain will be called? After all, source code is source code as we know it because we want it to be readable for humans. If that weren't true, we could happily program away in Brainfuck.
I have seen (unfortunately) program being written in PHP where the common ancester consisted of 50 lines of code, 4 different derived objects overloading every method, and consisting of 1000-2000 lines of PHP code. The classes as such were not in one inheritance chain, but all derived of one base object. So what is the benefit of OOP? There is no inheritance, no polymorphism and no code re-use, just confusion (with me)
OTOH, you say you have to program in Java. This is an extremely powerful OOP framework, providing an wealthy library of routines and objects you can use. It is just another tool, OOP or not.
OOP is a lot of hype, but it got stuck and some people can only think in OOP.
In procedural programming, you call a function and pass objects (variables, structs) as parameters on which you operate. In OOP you take an object and call the function to perform an operation on that object. There is not that much difference.
Problem is that once you master OOP, my experience is that it is difficult to change your mind and write again a procedural program. Procedural programming appears as programming inside out.
The big advantage of OOP? Polymorphism and Inheritance? Well, sometimes. If you have to program GUI environments (I mean writing the libraries, not using QT or so) it might be beneficial.
What I have against OOP is that it might be totally unreadable. If you have a pointer in your source code, do you know to what object it points? What method in the inheritance chain will be called? After all, source code is source code as we know it because we want it to be readable for humans. If that weren't true, we could happily program away in Brainfuck.
I have seen (unfortunately) program being written in PHP where the common ancester consisted of 50 lines of code, 4 different derived objects overloading every method, and consisting of 1000-2000 lines of PHP code. The classes as such were not in one inheritance chain, but all derived of one base object. So what is the benefit of OOP? There is no inheritance, no polymorphism and no code re-use, just confusion (with me)
OTOH, you say you have to program in Java. This is an extremely powerful OOP framework, providing an wealthy library of routines and objects you can use. It is just another tool, OOP or not.
jlinkels
Not only the item in bold, but it can be totally counterintuitive (my favorite ellipse <-> circle example).
Simplistically, OOP makes sense when we have "small deltas", i.e. there is a powerful base class and small modifications need to be made through inheritance to enhance functionality.
At the moment the modifications become big, the code becomes unreadable - as you've correctly pointed out.
Another problem - when there are too many deltas, it's difficult to see the whole picture.
...
To OP - yes, the whole world may be that crazy. OOP is not the only paradigm, and not at all always a convenient one.
There are more basic things: visibility/scope, data protection, making code modification easy, etc. OOP is just a way, and not always a good way.
Oh, on public setters on private data members - this is my favorite OOP nonsense. The only advantage it may have is simultaneous modification of all setting, i.e. instead of
Code:
void set_a(double b)
{
a = b;
}
one can make it
Code:
void set_a(double b)
{
a = 1.1 * b + c;
}
.
Likewise, traceability - instead of
Code:
void set_a(double b)
{
a = b;
}
Code:
void set_a(double b)
{
fprintf(stderr, "about to set a to %g\n", b);
a = b;
}
.
But the above features have nothing to do with OOP.
What I have against OOP is that it might be totally unreadable.
Is there a paradigm for which this is not the case?
The OOP approach can help to design large programs and make them maintainable, but by breaking them down into smaller more manageable chunks it is necessary to have a good design, but again that is true of any paradigm.
Is there a paradigm for which this is not the case?
The OOP approach can help to design large programs and make them maintainable, but by breaking them down into smaller more manageable chunks it is necessary to have a good design, but again that is true of any paradigm.
Every modular approach helps to make large programs maintainable.
I.e. the basic values are compartmentalization/encapsulation/protection/reuse.
Oh, on public setters on private data members - this is my favorite OOP nonsense. The only advantage it may have is simultaneous modification of all setting
...
Quote:
Likewise, traceability
There is a third advantage: checking the validity of a proposed new value of a data member.
The usual example is to make sure that the proposed new value is within a certain constant range. But this checking can be as complex as you like. For example, it may be valid to set a data member to a certain value until a certain date, but not after that date.
There is a third advantage: checking the validity of a proposed new value of a data member.
The usual example is to make sure that the proposed new value is within a certain constant range. But this checking can be as complex as you like. For example, it may be valid to set a data member to a certain value until a certain date, but not after that date.
I agree. But, again, it has nothing to do with OOP. AFAIK, C# "allows" '=', and default setter is called. Once you change your mind, you just override the default setter not changing your code.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.