What are the Advantages of Object Oriented Programming?

Discussion in 'Computer Science & Culture' started by Bowser, Apr 14, 2013.

  1. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    I've been studying OOP with Java this past week and have yet to realize the advantages of classes and objects over the simple function calls of traditional programming methods. I studied C programming years back, and I don't see a whole lot of difference between calling a function in C or creating an object in Java. Granted, an object can hold more methods that can be accessed, but isn't creating an object much like invoking an include file?

    I would like to hear from someone who knows this subject well.
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. rethu Registered Member

    Messages:
    8
    see in google you will get lot
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. johnstephen Registered Member

    Messages:
    10
    This is the basic language for any programming language and very essential for any IT professional. If anyone unable to understand this language, can't understand any programming language.
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    Seems to be more of an evolution from other languages. I'm still trying to understand the advantage over say, C language.

    So, I can create a class that contains a number of functions...

    public class myClass
    {
    private int myVariable;

    public myClass(int variable)
    {
    myVariable = variable;
    }

    //Add whatever functions you want
    }
     
  8. przyk squishy Valued Senior Member

    Messages:
    3,203
    ...
     
    Last edited: Apr 28, 2013
  9. przyk squishy Valued Senior Member

    Messages:
    3,203
    In a nutshell, the point of OOP is that it lets you extend the programming language by letting you define new types of variables and the allowed operations on them. There are a few situations where this lets you write your program in a nicer way that's closer to the way you might want to think about the problem that the program is solving.

    For instance, one of the areas where OOP is heavily used is in GUI programming. You have certain types of objects (like windows, buttons, etc.), information characterising them (e.g. the size and position and contents of a window), and certain operations you might want to perform on them (e.g. moving or resizing a window). In an object-oriented language you would define classes representing all of these types of objects. This lets you simply think of windows, buttons, etc. as objects that you just do things with and keeps track of all the details specific to them in one place. For example, in an OO language with the right class definitions, you might be able to display a window with a button in it with code like (pseudo C++/Java-like code):

    Code:
    my_window = new Window();              // Create a window variable.
    my_window.setTitle("I am a window");   // Set window title.
    my_window.setPosition(100,100);        // Upper left corner should be located 100 pixels down and right from the top left corner of the screen.
    my_window.setSize(100,300);            // Window should have a size of 100x300 pixels.
    
    my_button = new Button();              // Create a button object.
    my_button.setCaption("Click me!");     // Button caption should read "Click me!".
    
    my_window.addWidget(my_button);        // Display the button I just defined in the window I created.
    my_window.show();                      // Show the window.
    
    So you end up with code that looks quite a lot like how you'd explain what the program is doing in English.

    Note that you can get quite far doing OO-style programming even in many not officially OO languages. In C you can do object-based programming by defining structures and functions acting on those structures for instance. There are even tricks for implementing data hiding and single inheritance in C.
     
  10. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    Thanks for your help. It is becoming a little more clear. I appreciate your input.
     
  11. przyk squishy Valued Senior Member

    Messages:
    3,203
    In case this is confusing you, it's also worth keeping in mind that Java is a bit overzealous with its imposition of object-oriented language features. For instance it has (or at least had, this might have changed), a rule that all functions have to be class members, so you end up with oddities like e.g. all the math functions being defined in a Math class just because the language requires it rather than because it's a problem where OOP is genuinely useful or makes the expression more elegant.
     
  12. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    So why does Eclipse use xml files in the creation of android apps? Why not approach it through an API that's built on classes? I'm new to the IDE, android, and Java, so I have a ton of questions.
     
  13. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    Beyond the the operators, there is a Math class, which is fine with me.
     
  14. MeNotYou Registered Member

    Messages:
    15
    In procedural languages like C, you have functions and procedures that allow you to break your code into units/modules.
    e.g. function GetCustomerName(int customerID), will return a customer name for you.

    Object oriented languages let you take that a step farther and break your code into units/modules of procedures/functions. It's a way to easily build common frameworks or libraries.
    E.g. Class CustomerInfo could include the GUI for displaying the customer info, the function GetCustomerName, and any number of other procedures that involve handling customer info.

    Another example:
    I built my own framework for performing SQL queries in C#...I was able to compile this class into a dll library and use it in a whole bunch of different projects.
     
  15. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    I'm certain its value will become more apparent as I use it. I want to try building Android apps using Java and am just starting to get a taste of it. When you create a project in Eclipse, it generates a ton of files. I'm still learning what all those files are for. Looking back on C, this is like learning a whole new language...kind of. I explored Java Sing last night and built a window with buttons--a small step.

    View attachment 6240
     
  16. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    Using NetBeans, here's what I was doing last night...

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package simpleframe;
    import javax.swing.*;
    
    
    public class SimpleFrame extends JFrame{
        public SimpleFrame()
        {
            super("Frame Title");
            setSize(300, 200);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLookAndFeel();
            setVisible(true);
        }
        private static void setLookAndFeel()
        {
            try
            {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            }
            catch(Exception exc)
            {
            }
        }
        
            
        public static void main(String[] args) 
        {
         SimpleFrame sf = new SimpleFrame();   // TODO code application logic here
        }
    }
    
    The source came from a book I've been reading: "Teach Yourself Java in 21 Days." I think the guy's code is a little confusing.
     
  17. firdroirich A friend of The Friends Registered Senior Member

    Messages:
    565
    Avoid any book which claims to teach you a programming language in 21 days, 24 hours or any such absurdity.
    True the syntax of Java has only 50 or so keywords, but the API and libraries are year's worth of learning.

    I'd recommend you get Deitel's "Java, how to program" - I think its at 9th edition at the moment.
    The advantages of OOP in Java is inheritance and polymorphism
     
  18. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    I did discover several tutorials online after buying the book and found myself referencing them when the book left me asking questions. I'll give your book a look on amazon. I'm a little sticker shocked by the prices of some of these books; nonetheless, I find the best buys are on Amazon.
     
  19. firdroirich A friend of The Friends Registered Senior Member

    Messages:
    565
    Sometimes these books are in a library if the cost is too much - I learnt c# from 1 book borrowed from a library.
    The time limit imposed by libraries is also a good motivator to get through it as quick as possible. Maybe not applicable for yourself,
    but for absolute beginners this works well. Take a look there before splashing out.

    Also - make use of free videos online from good universities!
    Stanford University now videos all year-1 computer science lectures and are freely available online on youtube. The entire 'Programming Methodology' course is there as are links to notes, handouts etc
    so you can get a Stanford quality education from a legendary lecturer for nothing. You can skip through the easy stuff and concentrate on what you want.
    MIT open courseware also have CS and other lectures online with course notes and labs included.

    There are a lot of OOP related or general programming lectures from reputable institutions such as UNSW. These are some of the best out there for more advanced stuff like data structures, algorithms etc.

    Then ofcourse Oracle website has a walkthrough -with code, of almost every aspect of Java, from basics right up to fully coded Java EE apps.

    Good luck
     
    Last edited: May 4, 2013
  20. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    Thank you for your input. So far it has been enjoyable, and I want to keep it that way.
     

Share This Page