Tech Off Thread

18 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Help with OOP

Back to Forum: Tech Off
  • User profile image
    Pace

    Hi all,

    Ive been programming for a wee bit now... I can do all sorts with VB and I can manipulate data using ADO... im pretty good with SQL and Stored Procs so to take my programming to the next level as it were I picked up the book "Head First Design Patterns"... now its written in Java, but I know VB so well and other languages that  I can kind of find my way around C#... Java is not too dissimilar from C# so I have some wicked sick conversions going on in my brain. Im struggling getting the terminology of the concepts correctly though so can you help me out?

    Say I have an Abstract Class Duck...
    Duck has setFly setQuack, quackBehavior, flyBehavior // these are will be inherited

    flyBehavior and quackBehavior are encapsulated behaviors of the duck right?

    so my concrete classes (canQuack, cantQuack, canFly, cantFly) are absctracts of my encapsulation that I could use polymorphism to change the two behaviors of...

    my question is "Am I fishing in the right pond?" Expressionless

    edit: "are inherited became will be inherited"

  • User profile image
    Dr Herbie

    Wouldn't worry too much about the terminology, everyone seems to have their own set.  Feel free to invent your own. I've never used the expression 'encapsulated behaviour' before, for example.

    As long as you get the basic concepts, you'll be fine.

    I would say the the setFly, setQuack, quackBehaviour, and flyBehaviours were all virtual, ready to be inherited.  If they are abstract, they must be inherited, if they're just virtual they will have a default implementation.

    Virtualisation is one way to use polymorphism, and interfaces are another.  They both use inheritance.

    Importantly, do you understand the purpose of polymorphism? I once saw it explained as a way to simplify code by reducing the number of if statements.

    Instead of
        if(myitem is type1)
      {
         (type1)myItem.DoMethod1();
      }
      else
      {
         (type2)myItem.DoMethod2();
      }

    You just get:
        myItem.DoPolymorphicMethod();


    Don't ever do it the first way, or the OOP police will swoop down and take you away.

    I'd rat you out in an instant.

    It would be my duty.

    I have recently seen professional developers do it the first way. I left that job soon after.

    Herbie

  • User profile image
    Pace

    AFAIK the purpose of polymorphism is so that should things change down the line, you would only have to change it in one place.

    IE to add extra functionality you could add another class as apposed to having to go through your whole project changing things.

    So say I wanted to add swim to my duck I could add a swim class that is inherited by the duck Correct?

  • User profile image
    littleguru

    There are so much different names for the same thing: That is already with the basic stuff, like the template for an instance. Some people call it class, some type, some prototype, some template, some frame, ...

    Some people call instancing a class (type, ...) also instance inheritance, some call it just instancing.

    I would suggest to read a C# book, if you start with OOP concepts and if you want to use C# in the future. Reading a C# book gives you a few benefits:
    1) learning OOP
    2) learning the language.
    3) you learn the common OOP terminology that is used for the .NET languages. It is different then the one used in JAVA.

    JAVA doesn't support a few things that are supported by C# and vice versa...

  • User profile image
    Pace

    Hey littleguru yes I appreciate that...

    Im a VB.NET guy in a sense that my company apps I inherited are all VB6 so I learn VB.NET to make the transition easier... dabbling with C# I notice though its not initially easier to understand as VB which is very english like, it makes more sense with what you are doing, its more structured...

    I dont want to program Java, its just the book is aimed at learning patterns easily... its structured in such a way that I could pick it up... If I were to get another book im sure I would have very hard work trying to understand... so im using this to get the basic concepts by converting the Java to C# which in turn allows me to grasp it from a VB perspective... Yes I want to learn C#... its which book to get.

    I dont like the Step by Step series I prefer the Core Reference series but I cant seem to find the appropriate MS Press material.

    I just hope im getting the theory right.

  • User profile image
    littleguru

    You could read one of those books: http://tinyurl.com/qek4p
    What I like a lot is the "Inside C#" book: http://tinyurl.com/o7lv6 It gets you inside C# and OOP patterns (in a very very fast way).

  • User profile image
    littleguru

    Pace wrote:
    AFAIK the purpose of polymorphism is so that should things change down the line, you would only have to change it in one place.

    IE to add extra functionality you could add another class as apposed to having to go through your whole project changing things.

    So say I wanted to add swim to my duck I could add a swim class that is inherited by the duck Correct?


    If you want add swim to the duck, you need to extend the class (NOT to add another class!!). Keep always in mind that a class has the responsibility of their own state and functionality. Objects are created to bundle functionality in logic groups (= a logic group is an object).

    Buy adding swim that does not mean you have to change the whole project. You need only to change the base object and the objects that inherit from that class (that is if each object inheriting from the base object should have an implementation of swim. if not, you add swim just to the duck). That is good, because if another type of animal can't swim, you need to implement that explicitely in your code!

    A good way to do stuff, is using interfaces. They give you an abstracter way to work with objects. You should always create an interface if the actual class is not important, but you want just a certain functionality to be fulfilled.

  • User profile image
    mot256

    Pace wrote:
    
    ...
    So say I wanted to add swim to my duck I could add a swim class that is inherited by the duck Correct?

    Don’t you mean "encapsulated" by the duck class? Because your duck-class would typically inherit from a bird-class (that will encapsulate “fly”, “eat”, etc.).

  • User profile image
    mot256

    littleguru wrote:
    
    Pace wrote:AFAIK the purpose of polymorphism is so that should things change down the line, you would only have to change it in one place.

    IE to add extra functionality you could add another class as apposed to having to go through your whole project changing things.

    So say I wanted to add swim to my duck I could add a swim class that is inherited by the duck Correct?


    If you want add swim to the duck, you need to extend the class (NOT to add another class!!). Keep always in mind that a class has the responsibility of their own state and functionality. Objects are created to bundle functionality in logic groups (= a logic group is an object).

    Buy adding swim that does not mean you have to change the whole project. You need only to change the base object and the objects that inherit from that class (that is if each object inheriting from the base object should have an implementation of swim. if not, you add swim just to the duck). That is good, because if another type of animal can't swim, you need to implement that explicitely in your code!

    A good way to do stuff, is using interfaces. They give you an abstracter way to work with objects. You should always create an interface if the actual class is not important, but you want just a certain functionality to be fulfilled.

    Interfaces are a very good idea, but could introduce code duplication across classes (and therefore duplicate bugs). Because if you implement an IFly interface on all classes you will need to implement the same code for all classes (with birds flying the same), that is if it is not implemented on the base class as a virtual method which you could just overwrite (if the bird has a different method of flying)…

  • User profile image
    Sven Groot

    It's the old is-a vs. can-do thing. Use an abstract base class for is-a (for instance, a duck is-a bird) and an interface for can-do (a bird can-do flying). It's also perfectly fine to have an IFly interface and provide a base implementation of that in the Bird class that other derived classes (such as Duck) can use.

    Then for the Chicken class you override the Fly() method and throw an InvalidOperationException. Tongue Out

  • User profile image
    Dr Herbie

    Well, these types of discussion have been going on for decades:

    You have a duck that can both swim and fly.  Duck inherits from Bird, so you could add fly and swim to the Bird class.  But what about Ostriches which can neither fly nor swim* ?

    Here are some options:

    A) Add Fly and Swim to the Bird class as virtual methods and override in Orstrich to do nothing.

    B) Add Fly and Swim to the Bird class as virtual methods and override in ostrich to throw an exception (making developers check types before calling and breaking polymorphism)

    C) Add Fly and Swim to the Bird class and also add  CanFly and CanSwim properties to indicate whether these are valid to call.

    D) Add IFly and ISwim as interfaces that Duck inherits as well as the Bird base class. (requires developers to perform interface type checking, but can be useful if you need collections of flying or swimming things that may or may not be birds).

    E) All of the above. Tongue Out


    Whichever method you choose there are plusses and minises that will only appear when you have started using them.  Learn to refactor.

    Herbie



    * I'm assuming that Ostriches can't swim for the purposes of this argument.  I don't actually know whether Ostriches can swim or not. Looks like the PhD in Zoology was a waste of time.

  • User profile image
    Pace

    woot! Thanks! Big Smile

    this is way more fun!

  • User profile image
    mot256

    Sven Groot wrote:
    
    ...
    Then for the Chicken class you override the Fly() method and throw an InvalidOperationException.


    or just call
    base.Run(InCircles)
    Big Smile

  • User profile image
    mot256

    Pace wrote:
    

    woot! Thanks!

    this is way more fun!



    dumn question... "woot"? see you guys use it allot in this forum that few weeks.... others spel it "w00t"... 1 will not know if you do not ask... Smiley

  • User profile image
    Tensor

    mot256 wrote:
    
    Pace wrote: 

    woot! Thanks!

    this is way more fun!



    dumn question... "woot"? see you guys use it allot in this forum that few weeks.... others spel it "w00t"... 1 will not know if you do not ask...


    http://en.wikipedia.org/wiki/Woot

  • User profile image
    Pace

    lol yes w00t > woot

    w00t is like... well w00t...

    lets say you were working on a really intense program that everytime you built failed, over and over. Then you did something, pressed the button and achieved compilation... thats a w00t!

    winning the lottery is a pretty good w00t too Tongue Out enjoy its useage Wink

  • User profile image
    Pace

    Tensor wrote:
    
    mot256 wrote: 
    Pace wrote: 

    woot! Thanks!

    this is way more fun!



    dumn question... "woot"? see you guys use it allot in this forum that few weeks.... others spel it "w00t"... 1 will not know if you do not ask...


    http://en.wikipedia.org/wiki/Woot


    AWESOME FIND! im impressed Cool

  • User profile image
    mot256

    Cool thanx!
    may you guys have w00t-full day! Tongue Out

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.