[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]

Polymorphism

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: 20
Thread images: 3

File: 1468875094238.gif (822KB, 600x366px) Image search: [Google]
1468875094238.gif
822KB, 600x366px
Hey /g/,

I'm new to polymorphism and I have a question regarding abstract classes, specifically in Java.

Let's say I have three different classes for different shapes (circle, rectangle, triangle) that all have a getArea method. Why is it necessary to have an abstract class with a getArea method defined for the compiler to decide which method from which class needs to be called. Why is the data type not enough?
>>
>>59107443
http://laufer.cs.luc.edu/teaching/313/handouts/practice-midterm
>>
>>59107443
the interface is useful when you want to add a new shape and have all the methods that you must implement already defined so you can just use the interface and know it will work with what ever implements the shape interface
>>
>>59107443
The advantage of polymorphism is that you can have a pointer to a base class but the when you call the function it resolves to the derived class.

Java is really shit for learning programming. The concepts come easier with c++.
>>
>>59108017
using pointers goes against the c++ recommendations
>>
>>59108017
idk, I had a hard time grasping pointers when it was introduced to me (at the time I was learning C++), but I didn't really grasp pointers until I was introduced to objects in Java.
>>
>>59108030
What are you babbling about?
>>
>>59108180
Raw pointers he means. Use smart pointers.
>>
>>59108208
There are still times where it makes sense to use raw pointers. Like as parameters to functions or in any large application where you have long lived objects that are used throughout and have a clear owner of the objects.
>>
>>59108344
No, you should never use raw pointers unless you want to hyper optimized specific functions that bottlenck. Smart pointers should always be used and you should do things OOP for all high level objects, which means doing things the java way.
>>
Think pokemon specifically evee
>>
>>59107972
this

when you start making bigger classes you'll love interfaces
>>
Also why not just have an interface with generic methods at the end of the day only some shapes will share similar get area methods that you'll have to redefine in the first place so any abstract will basically be an overloaded method
>>
>>59108559
Yup. It's the RAIItâ„¢ thing to do
>>
>>59107443
TFW seeing a classmate on 4chan. Hola.
>>
>>59107443
Don't bother, OOP is absolute shit and I immediately forgot everything about after my exam in my OOP course.

If you have to write 50 lines of code and need 3 files to write a very simple hash-function you should abandon whatever you are doing.
>>
>>59107443
In case you are retarded and forget to implement it
>>
File: Ar.K.png (831KB, 843x843px) Image search: [Google]
Ar.K.png
831KB, 843x843px
>>59107443
Use (((LISP))), problem solved.
>>
>>59108702
surprisingly helpful
>>
File: 1451434814905.png (267B, 400x500px) Image search: [Google]
1451434814905.png
267B, 400x500px
>>59108559
wrong.
you should never use OWNING raw pointers. non-owning raw pointers are still the preferable way to pass optional function parameters.
Thread posts: 20
Thread images: 3


[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.