Object Oriented programming (OOP) languages are based around the paradigm of objects that interact with each other to produce functionality. There are many programming languages that are designed around object oriented principles, such as Java. A class is a definition of an object and contains data and/or functions. Functions within a class are called "methods".
Object Oriented programming is defined by three things: inheritance, polymorphism, and encapsulation.
Inheritance is the ability for one class to use the data and methods of an ancestor class (or multiple ancestor classes if multiple inheritance is allowed), rather than having to define those again within a new class. If class B inherits from class A, then class A is called an ancestor class of B, and class B is called a descendant class of A.
Polymorphism allows an object to be used in place of an ancestor object without the code that is using it having to know about the descendant. This means that a complex object can appear to be a simple one. The example below shows that you can have a specific model of car, such as the Model T, which looks like and acts like a normal car. Polymorphism can be implemented in different ways. Smalltalk implemented it via messages. Most compilers that support OOP implement a virtual method table, which is an array of method pointers as per the CORBA standard. A descendant class instance can be used in place of any ancestor class instance, but an ancestor class instance cannot be used in place of a descendant class instance.
Encapsulation means that the data used by a class is defined within the class itself, rather than the code having to resort to global data. Further, functions specific to the class, called "methods" are also defined within the class.
A class is a definition. In most languages, a class is a data type. An instance is an instantiation of a class. It can be considered in terms of a real-world analog: the designs for a pickup truck are akin to a class. An actual pickup truck build according to those designs is an instance of that class. The term "object" is often used to refer to either the class, an instance, or the general concept. The data within an instance is referred to as "instance data".
A method is a function that is encapsulated within a class. Methods have some level of scope and some mechanism for inheritance.
Different languages support different types of scoping. Typically, these levels are:
Inheritance mechanisms define how a method interacts with inheritance.
A constructor is a class method that is automatically called when an instance of the class is instantiated. The purpose of the constructor is to set up the new instance. A destructor is a method that is called when an instance of the class is destroyed. The purpose of the destructor is to clean up just prior to the instance being deleted. Typically this includes freeing memory pointed to by instance data. Some programming languages support special constructors called "copy constructors" which are called when an instance is created as a copy of another instance.
public class ModelT extends Car { private Color color = Color.BLACK; private String model = "T"; public void setColor(Color color){ this.color = color; } public Color getColor(){ return this.color; } public String getModel(){ return model; } } public abstract class Car { void setColor(Color color); Color getColor(); String getModel(); }