java - What are Abstract data types in algorithms and why do we need them? -
i quote source read , not understand.
we know default, primitive datatypes (int, float etc) support basic operations such addition , subraction. system provides implementation primitive datatypes. userdefined types need define operations. implementation of these algorithms done when use them. means, user defined datatypes defined along operations.
to simplify process of solving problems, combine datastructures along operations , call abstractdatatype.
can explain example of , context of real world too? why u need them?
source narasimha karumanchi book datastructures , algorithms
good question!
in terms of application infrastructure , design, abstract data types useful in order have clean , understandable hierarchy of classes , data models. asked practical example, take application uses database: online eshop. primitive data types useful, of course, use doubles store prices, ints store number of items , strings display , store item names / descriptions, how deal basket/cart? easiest , recommended way create abstract representation of cart via separate class, shoppingcart, contain properties:
class shoppingcart{ int numofitems; double totalprice; list<product> products; etc. }
also, need classes represent products , categories such as:
class product{ double price; string name; etc. }
think of abstract data type create own objects, own methods , on.
we use abstract classes define general behavior of subclasses extend them, such as: abstract class vehicle , classes car, bike extend vehicle. structure be:
abstract class vehicle{ public void drive(); public int getnumberofwheels(); }
this way make sure instances of car , bike implement 2 methods listed in abstract class vehicle:
class car extends vehicle{ //we have implement 2 methods , provide implementation }
another developer create new type of vehicle (say "plane"), see has implement 2 methods no matter what.
i hope you.
Comments
Post a Comment