Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Object oriented programming

  1. Sep 7, 2016 #1
    I have a problem with understanding the concepts of "object oriented programming"
    I do not know what is an instance, object, method,interface...
    I have read many examples from the real world
    but I need a sample code with labeled instance, object, method, interface
    I wish the idea is clear
     
  2. jcsd
  3. Sep 7, 2016 #2

    Stephen Tashi

    User Avatar
    Science Advisor

    If you want sample code, you should tell what computer language you want it in.
     
  4. Sep 7, 2016 #3
    sorry, i want java program
     
  5. Sep 7, 2016 #4

    Mark44

    Staff: Mentor

    You don't need sample code with these things labelled - what you need is to understand how these terms are defined.

    You didn't ask about the term class, which is a user-defined type in Java, C++, C#, python, and other languages. A class definition is a template the the compiler or interpreter uses to set up memory when a variable of this new type is declared. The compiler doesn't actually allocate any memory for a class.

    An object or instance (pretty much the same thing) is a variable whose type is whatever class the object is declared as. The compiler allocates memory for the data members of the class.

    A method is object-oriented terminology for a function member for a class, as opposed to data members (sometimes called class properties).

    An interface is a template for a set of functions that can be used by some class. The interface does not contain function definitions -- only declarations (or prototypes) of the functions, namely the names of the functions, the number and types of their arguments, and their return types. A programmer who wishes to use an interface has to write definitions (or implementations) for all of the functions listed in an interface.
     
  6. Sep 7, 2016 #5
    Why do we use the interface?
     
  7. Sep 7, 2016 #6

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    You can write a program that uses the functions in the interface and someone else can write the implementation of the functions without more coordination. The development environment can easily check that you are using the functions with the correct interface and tell you when you have made a mistake. In the editor, you can put the function name in your code and it will give you a list of the valid calls available with that function name. It's a good thing.
     
  8. Sep 7, 2016 #7

    Mark44

    Staff: Mentor

    I did a quick web search for "OOP interface" and here's one that was listed.
    http://www.cs.utah.edu/~germain/PPS/Topics/interfaces.html
     
  9. Sep 7, 2016 #8

    jedishrfu

    Staff: Mentor

    An interface is a kind of software contract. If a given class implements an interface then it means that every instance of the class will have defined the methods listed in the interface which means you can refer to the object by its interface as along as you only need to call the methods of the interface.

    A common example right from Java, is the the Runnable interface. If a class implements Runnable then it must provide the run() method. Programmers define classes using Runnable so the class can be used in a multiple threaded environment i.e. you can run instances of the class in different threads at the same time.

    https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

    Notice in the above Java tutorial that the programmer has two choices either to implement Runnable or to subclass from Thread. Implementing Runnable is far more flexible and considered a best practice over sub classing Thread. Subclassing from Thread means you can't subclass from any other class in Java so it limits your options in the long term.
     
  10. Sep 7, 2016 #9

    Stephen Tashi

    User Avatar
    Science Advisor

  11. Sep 7, 2016 #10

    jedishrfu

    Staff: Mentor

  12. Sep 7, 2016 #11

    rcgldr

    User Avatar
    Homework Helper

    Wiki articles:

    http://en.wikipedia.org/wiki/Class_(computer_programming)

    http://en.wikipedia.org/wiki/Object_(computer_science)

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

    http://en.wikipedia.org/wiki/Method_(computer_programming)

    interface for oop:
    http://en.wikipedia.org/wiki/Protocol_(object-oriented_programming)

    interface for computing in general:
    http://en.wikipedia.org/wiki/Interface_(computing)

    http://en.wikipedia.org/wiki/Instance_(computer_science)

    Instance - in the case of C++, template functions can be defined, including the code, using generic types for it's parameters, but don't generate any actual code. When C++ code includes a call to a template function with specific types, an "instance" of that function for those specific types is created within the code space of a program. Multiple calls to a template function with the same set of specific types will only generate one "instance" of a template function. In the case of Java, generic functions create code at define time, and parameters are handled as objects where the object type is handled at runtime.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted