[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

OOP constructors.

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 26
Thread images: 1

File: IMG_3898.png (163KB, 500x591px) Image search: [Google]
IMG_3898.png
163KB, 500x591px
Can someone redpill me on Constructors ?

Why do I need them?
My class has methods, I can just instantiate an object then call methods....where do constructors fall in this simple usage pattern?
>>
A constructor is a method that is called on object instantiation
>>
They're useful for initialising object state and passing in parameters
Example:
File file = new File("blah.txt");
>>
>>59154889
If you fill your class with useful default values upon creation, you're using a constructor.

So thats only one call.

your_class instance;

has then everything you need to make it work right away. Which is one call, not two, hence cleaner
 your_class instance;
instance.set_some_values(a,b,c,d,e);


You can also pass them arguments (if you write the constructor eccordingly) to give them different default values. That comes in pretty handy


your_class instance(a,b,c,d,e);


Also it enables you to finetune visibilty, if it is for instance not meant that the user can set values after creation themselves, you can directly only set the constructor public, not the set functions
>>
>>59155970
>>59155970
>>
wait so is the "init" method in python a constructor then?
>>
>>59156028
what would the code for the File class look like here?
>>
>>59157287
Yes
>>
>>59157296
so does every class just have a single init constructor? can there be multiple constructors?
>>
>>59157302
Depending on the language, you can have constructors that take different parameters, yes. However, typically, it's not done too often.
>>
>>59157302
You can have multiple "overloaded" constructors with different parameters. For example one constructor might have no inputs. One might take an integer. One might take a string. One might take an int and a string. Anything you want, as long as there aren't duplicates.
>>
>>59157327
well based on a quick google search, Python doesn't support constructor overloading. What do I do in that case?

I assume overloading is needed to instantiate an object with different parameters
>>
>>59154889
When you instantiate an object you call the default constructor fgt.
>>
>>59157327
Exactly this. If two constructors for the same class took the same parameters, it'd be considered an error as the 'signature' of the constructors conflict. That is to say, the language would have no way of distinguishing which constructor you intend to use because they are called in exactly the same ways.
>>
As
>>59156028
said, they help to avoid unitialized variables. Anyone who has ever programmed in C probably has faced some heisenbug which in the end was some unitialized int somewhere.

In C++ when a struct's variable is "const" you need to set its value at instantiation, you can't (or better yet, shouldn't) set it afterwards by setters.
>>
>>59154889
>Can someone redpill me on
this phrase makes you look like you're not very good at getting laid.
>>
>>59157398
>Anyone who has ever programmed in C probably has faced some heisenbug which in the end was some unitialized int somewhere.

True. Just debugged my own shell for 2 hours today only to find out that I forgot to dup a file descriptor.
>>
>>59157340
Have optional parameters
>>
>>59157559
what is the point in a language using overloading rather than optional parameters?
>>
>>59157569
You get to use the word overloading.
>>
>>59157595
o-oh..
>>
>>59154889
Look up the concepts of composition, inversion of control, and dependency injection.

Constructors let you generate objects with configurable capabilities without leaking interface details to callers. This is useful when building systems with loose coupling, as it helps improve maintainability.

Constructors are also integral to the concept of RAII, where your class lifetime completely controls the lifetime of all of its dependencies. This allows you to build memory-safe programs without needing a stop-the-world garbage collection.
>>
>>59157342

Jeez, you're an assh*le
>>
>>59158028

Very helpful. I can see these being important to enterprise software / large projects where you integrate with internal and external teams.

Thanks.
>>
>>59157569
>>59157595
Depending on how the overloading is implemented, you get static dispatch which gets resolved at compile time rather than run time. It will run slightly faster in exchange for being less flexible.
>>
>>59157569
Less typing. Optionals are a little cumbersome in Java. Optional.of(value) or Optional.empty() and they don't always serialize nicely.

Say if you have a record class for a DynamoDb table you could have a constructor that creates an instance with only the hash and range key (primary key in a relational db) fields set for lookups, and one which accepts all the fields for your application to call when you're creating new records.
Thread posts: 26
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.