Object oriented programming

Main Question or Discussion Point

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
 

Answers and Replies

Stephen Tashi
Science Advisor
6,851
1,149
If you want sample code, you should tell what computer language you want it in.
 
sorry, i want java program
 
32,930
4,637
I have a problem with understanding the concepts of "object oriented programming"
I do not know what is an instance, object, method,interface...
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.
Passionate Eng said:
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
 
Why do we use the interface?
 
FactChecker
Science Advisor
Gold Member
2018 Award
5,163
1,819
Why do we use the interface?
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.
 
32,930
4,637
Why do we use the interface?
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
An interface is a programming structure/syntax that allows the computer to enforce certain properties on an object (class). For example, say we have a car class and a scooter class and a truck class. Each of these three classes should have a start_engine() action. How the "engine is started" for each vehicle is left to each particular class, but the fact that they must have a start_engine action is the domain of the interface.
 
11,095
4,603
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.
 
rcgldr
Homework Helper
8,604
493
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.
 

Related Threads for: Object oriented programming

  • Last Post
Replies
11
Views
3K
Replies
31
Views
2K
  • Last Post
Replies
2
Views
3K
Replies
8
Views
1K
Top