Sunday, 15 July 2012

The 3 main Principles of Object Oriented Programming - How to Program with Java


Object Oriented Programming (or OOP) may be classified by three main principles.  

1) Encapsulation
2) Inheritance
3) Polymorphism

All these seem like intimidating words but are really fairly simple concepts to comprehend.  If you wish to learn how to program with java, you will need to know these concepts.  So let's have a look at our first main principle of OOP, encapsulation.  Encapsulation just means that we wish to limit the access that other pieces of code have to our particular object.  So, for example, if you have a Person object, and this Person object has a first and last name as attributes.  In the event that another piece of code tries to change your Person object's first name to be say "Frank3", you could have a look at what the first name is trying to be set to, and strip out any digits so we're just left with "Frank".  Without having encapsulation, we wouldn't be able to stop "silly programmers" from changing the values of our variables to something that wouldn't make sense, or even worse, break the application.  Make sense?

The second principle of OOP, and a very important concept if you wish to learn how to program with Java, is Inheritance.  This principle refers to a super class (or parent class) and a sub-class (or child class) and the idea that a child class gains all the attributes of its parent.  You can think of it in terms of a real world situation, just like a real parent and child.  A child has a tendency to inherit certain characteristics from his or her parents, like say, eye colour or hair colour.  Let's think about another example in terms of programming, say we have super class "Vehicle" and sub-classes "Car" and "Motorcycle".  A "Vehicle" has wheels, and therefore through inheritence so would a "Car" and a "Motorcycle", but a "Car" has doors, and a "Motorcycle" does not.  So it wouldn't be accurate to say that a "Vehicle" has doors, as that statement would be incorrect.  So you can see how we could determine all the aspects that are common between a "Car" and a "Motorcycle" and define them inside of the "Vehicle" super class.

The 3rd principle of OOP is Polymorphism.  This principle sounds the most intimidating, but I can explain it in simple terms.  Polymorphism means that an object (i.e. Animal) can take on several forms while your program is running.  Let's imagine you have created an Animal class and defined the method "Speak".  You then asked three of your friends to make types of animals and have them implement the "Speak" method.  You won't know what type of animals your friends create, nor how their Animals will speak, until you actually hear those animals speak.  This is very much like how Java handles this problem.  It's known as dynamic method binding, which simply means, Java won't know how the specific Animal speaks until runtime.  So perhaps your friends have created a Dog, Cat and Snake.  These are three kinds of Animals, and they each speak differently.  When Java asks the Dog to speak, it says "woof".  Whenever Java asks the Cat to speak, it says "meow".  Whenever Java asks the snake to speak, it hisses.  There's the advantage of polymorphism, all we did was define an Animal interface with a Speak method, and we can create a whole bunch of types of animals that speak in their own distinctive way.

For more information about how you can program with Java, there is a wealth of straightforward java tutorials presented on the howtoprogramwithjava.com web site.

29 comments:

  1. Replies
    1. This comment has been removed by a blog administrator.

      Delete
  2. How will you describe the steps or algorithm in making any general program in an OOP programming language???? And how will you do that for a non-OOP language????
    Highlight the difference in the programming styles of the OOP and non-OOP languages.

    ReplyDelete
  3. I have completely read your post and the content is crisp and clear. Thank you for posting such an informative article, I have decided to follow your blog so that I can myself updated.Android Training in Bangalore

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Thanks a lot very much for the high quality and results-oriented help.
    I won’t think twice to endorse your blog post to anybody who wants
    and needs support about this area.

    java training in chennai


    java training in bangalore

    ReplyDelete
  6. Ciitnoida provides Core and java training institute in

    noida
    . We have a team of experienced Java professionals who help our students learn Java with the help of Live Base Projects. The object-

    oriented, java training in noida , class-based build

    of Java has made it one of most popular programming languages and the demand of professionals with certification in Advance Java training is at an

    all-time high not just in India but foreign countries too.

    By helping our students understand the fundamentals and Advance concepts of Java, we prepare them for a successful programming career. With over 13

    years of sound experience, we have successfully trained hundreds of students in Noida and have been able to turn ourselves into an institute for best

    Java training in Noida.

    java training institute in noida
    java training in noida

    ReplyDelete
  7. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    safety course in chennai

    ReplyDelete
  8. Sap fico training institute in Noida

    Sap fico training institute in Noida - Webtrackker Technology is IT Company which is providing the web designing, development, mobile application, and sap installation, digital marketing service in Noida, India and out of India. Webtrackker is also providing the sap fico training in Noida with working trainers.


    WEBTRACKKER TECHNOLOGY (P) LTD.
    C - 67, sector- 63, Noida, India.
    F -1 Sector 3 (Near Sector 16 metro station) Noida, India.

    +91 - 8802820025
    0120-433-0760
    0120-4204716
    EMAIL: info@webtrackker.com
    Website: www.webtrackker.com

    ReplyDelete
  9. Thanks For sharing Your information The information shared Is Very Valuable Please Keep Updating Us Time Just went On reading Thae article Python Online Training DataScience Online Training AWS Online Training Hadoop Online Training

    ReplyDelete
  10. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
    Best PHP Training Institute in Chennai|PHP Course in chennai

    Best .Net Training Institute in Chennai
    Powerbi Training in Chennai
    R Programming Training in Chennai
    Javascript Training in Chennai

    ReplyDelete
  11. Thanks for sharing article. The way you have stated everything above is quite awesome. Keep blogging like this. Thanks.
    AWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

    ReplyDelete
  12. Marketing is all about analytics. Talentedge offers Marketing Analytics program to sharpen your skills. To know more visit:-

    ReplyDelete
  13. Many companies are pivoting to virtual events and More than 90% of event marketers plan to make virtual a key part of their strategy going forward. biography format and team building invitation email

    ReplyDelete
  14. Visit Bharat Go Digital Academy to learn the digital marketing skills in India.

    ReplyDelete
  15. Are you looking for the best Azure training in Chennai here is the best suggestion for you, Infycle Technologies the best Software training institute to study to also other technical courses like Cyber Security, Graphic Design and Animation, Block Security, Java, Cyber Security, Oracle, Python, Big data, Azure, Python, Manual and Automation Testing, DevOps, Medical Coding etc., with technical trainers with amazing training with a peaceful environment. And we also provide 100+ Live Practical Sessions and Real-Time scenarios which helps you to easily get through the interviews in top MNC’s. for more queries approach us on 7504633633, 7502633633

    ReplyDelete
  16. That's an superb post. Thanks for sharing.
    also, visit Java course in Pune

    ReplyDelete