• Comparing procedural vs object-oriented programming. Source: Sakpal 2018.
    Comparing procedural vs object-oriented programming. Source: Sakpal 2018.
  • SOLID design principles. Source: Gökalp 2019.
    SOLID design principles. Source: Gökalp 2019.
  • An overview of OOP concepts. Source: Devopedia 2020.
    An overview of OOP concepts. Source: Devopedia 2020.
  • Attributes and methods of a bicycle object. Source: Oracle Docs 2020a.
    Attributes and methods of a bicycle object. Source: Oracle Docs 2020a.
  • Illustrating the use of abstraction. Source: Hock-Chuan 2020.
    Illustrating the use of abstraction. Source: Hock-Chuan 2020.
  • Access modifiers in C#. Source: Steiger 2014.
    Access modifiers in C#. Source: Steiger 2014.
  • Static method used for testing the class. Source: Sedgewick and Wayne 2020.
    Static method used for testing the class. Source: Sedgewick and Wayne 2020.

Object-Oriented Programming Concepts

Avatar of user arvindpdmn
arvindpdmn
1402 DevCoins
User avatar
kkris
30 DevCoins
2 authors have contributed to this article
Last updated by kkris
on 2020-03-27 04:14:41
Created by arvindpdmn
on 2020-03-25 07:12:27

Summary

Comparing procedural vs object-oriented programming. Source: Sakpal 2018.
Comparing procedural vs object-oriented programming. Source: Sakpal 2018.

Developers who are familiar with procedural languages such as C and Pascal will understand variables, functions, and scope. When learning an object-oriented programming (OOP) language such as Java or C++, the same developers might have difficulty. This article presents an overview of OOP concepts.

Whereas the building block of procedural languages is a function or procedure, the building block of OOP languages is an object. Procedural languages have variables (data) and functions (code). In OOP, objects contain both data and code. With procedural languages, functions operate on data. With OOP, objects operate on their own data and selectively expose this data to other objects.

Different OOP languages sometimes differ in terminology. Some OOP languages may lack advanced OOP features. In general, OOP concepts are mostly similar across languages.

Milestones

1967

Simula 67 is born, officially the first object-oriented programming language. It's initially used in computer simulations. Classes in this language are called processes since object lifecycle depends on the process executing the statements.

1985

In the mid-1980s, many systems and application programmers adopt C++ for OOP. It takes another decade (1990s) for OOP to become more widely adopted.

Oct
1994

Object-oriented programming (OOP) and object-oriented design (OOD) are quite complex. To give software architects and developers guidelines to get these right, principles and design patterns have evolved over the years. At ACM's annual OOPSLA conference, four authors announce their book titled Design Patterns: Elements of Reusable Object-Oriented Software. This book describes 23 software design patterns. In time, this book becomes an essential read for OOP and OOD.

Dec
1997

Object Management Group (OMG) standardizes Unified Modelling Language (UML), v1.1. It's roots are in OOP. It's evolution can be traced to the early 1990s. UML establishes a common language among stakeholders involved in the design of object-oriented software and systems. Subsequently, UML undergoes many revisions, with v2.5.1 coming out in December 2017.

2002
SOLID design principles. Source: Gökalp 2019.

Robert Martin publishes a book on Agile where he describes five principles of OOD. Sometime later, Michael Feathers coins the term SOLID as a useful way to remember the principles.

Discussion

  • What are objects and classes in OOP?
    An overview of OOP concepts. Source: Devopedia 2020.
    An overview of OOP concepts. Source: Devopedia 2020.

    In the real world, just about anything can be seen as an object: car, dog, person, department, city, etc. These have state and behaviour. For example, a dog's state is its colour, breed and name; its behaviour is the way it barks, runs or wags its tail. Objects in OOP are quite similar.

    In OOP, an object is a software construct that includes both data and code to process that data. Unlike procedural languages in which functions are independent of variables, OOP objects retain control of their state and how others can affect that state via executable code. Data specifies the state of the object whereas executable code specifies behaviour. A real-world dog could be represented in OOP as a software object.

    Let's create many dogs of the same or different breeds. It's inefficient to write code for each dog. This is where the concept of class becomes important. A class is sort of a blueprint from which an object can be created. Thus, each dog is an instance of the same Dog class. Instantiation is the process of creating objects or instances from classes.

  • What are attributes and methods of objects in OOP?
    Attributes and methods of a bicycle object. Source: Oracle Docs 2020a.
    Attributes and methods of a bicycle object. Source: Oracle Docs 2020a.

    Attributes contain an object's state. Methods define an object's behaviour. Both are bound to the object and therefore accessed via the object. Attributes are also called properties, fields or instance variables. Each object has its own copy of attributes in memory. As for methods, only their addresses are stored within an object. Attributes and methods are also called members.

    Consider the class Rectangle. It's attributes could be length and breadth. It's methods could be getArea() and rotate().

    Two special methods are worth noting: constructor and destructor. When an object is instantiated from its class, the constructor method is called to initialize the object state. When the object is destroyed, its destructor method is called so that necessary clean up is done.

    Consider a database connector class. The constructor method might initialize the database server IP address, port, username and password for access. It might even open the DB connection. The destructor on the other hand might abort pending queries and close the connection. Other methods of the class might manage the connection, process DB queries and responses.

  • Which are the essential concepts of OOP?

    Among the many concepts of OOP, three stand out:

    • Encapsulation: A class and its instances bring together attributes and methods that are closely related. Encapsulation hides internal implementation and exposes public interfaces. Thus, it achieves data hiding.
    • Inheritance: Using an existing class, a more refined class can be created. The former is called a base class or super class. The latter is called a derived class or subclass. For example, Shape is a base class from which Square and Circle are derived classes. Base class might define common attributes such as colour and line width. Derived classes might define more specific attributes such as length or radius, and specific methods to compute area and perimeter.
    • Polymorphism: Different classes having the same attributes and methods, though implemented differently, can be interchanged at runtime. For example, a Canvas object might wish to compute the area of a shape object. It doesn't care if it's a square or a circle since both have the getArea() method. Canvas object calls this method on a shape object and the correct method of the current derived object is called.
  • What is meant by abstraction in OOP?
    Illustrating the use of abstraction. Source: Hock-Chuan 2020.
    Illustrating the use of abstraction. Source: Hock-Chuan 2020.

    Abstraction is about describing something at a conceptual level while leaving out the details. For example, we may talk about a vehicle without being explicit if it's a ship or a car. Abstraction a useful design tool since considering too many details upfront can be distracting and confusing. We start with an abstract concept and refine it later by adding more details to it. It's been said that,

    Abstraction hides details at the design level, while encapsulation hides details at the implementation level.

    Consider Shape class with members color and getArea(). Subclasses Rectangle and Triangle implement how area is calculated. Since Shape doesn't know how to calculate area, we call it an abstract class. It can't be instantiated. Only its subclasses that provide an implementation of getArea() can be instantiated. Shape's getArea() is an abstract method.

    Other than inheritance, an alternative to achieve abstraction is to use interfaces. Interfaces provide method signatures without implementing them. Classes that adopt these interfaces are required to implement them. Mixins are similar to interfaces except that they implement the methods themselves.

  • How is composition different from inheritance in OOP?

    Consider the classes Vehicle, Car and Engine. Car is a type of vehicle whereas engine is part of a car. The relationship between vehicle and car is best modelled as inheritance: Car is a subclass of Vehicle. The relationship between engine and car is best modelled as composition: an Engine instance is an attribute of either Vehicle or Car class.

    While both inheritance and composition are relevant to OOP, it's important for software architects and designers to look at the relationships among classes to select the right one. Specifically, we should ask, "Is it a is-a or a has-a relation?" Inheritance is a pillar of OOP but composition is essential to every language.

    Two other concepts closely related to composition are association and aggregation. Aggregation describes a part-whole relation. A Course object is part of a DegreeProgram object. It's also a many-to-many relation since the same course can belong to different programs and a program has many courses.

    Some languages such as JavaScript, allow prototypal inheritance. Object prototypes form the basis of other objects. Classes don't exist.

  • What are access modifiers in the context of OOP?
    Access modifiers in C#. Source: Steiger 2014.
    Access modifiers in C#. Source: Steiger 2014.

    It's possible to restrict access to classes, methods and attributes. By the principle of least privilege, limit access only to classes that need it.

    In Java, when no access modifier is specified, all classes within the package get access. Otherwise, we can use the following access modifiers:

    • public: Access is allowed for all classes.
    • protected: Access is allowed for subclasses (in any package) and classes within the same package. Thus, access is allowed only to closely related classes.
    • private: Access is allowed only within the class to which the members belong. This is typically used to hide data within the class. Any access to private data is provided via methods. Read and write access are via getters and setters respectively. These are also called accessors and mutators.

    In Kotlin, the term visibility modifier is used. Modifier internal gives access to all classes within the same module. Modifier protected gives access to subclasses.

    In C++, access modifiers can also be applied to inheritance. For example, private inheritance would make all methods and attributes of the base class private and therefore inaccessible to the subclass.

  • What are static classes and methods?
    Static method used for testing the class. Source: Sedgewick and Wayne 2020.
    Static method used for testing the class. Source: Sedgewick and Wayne 2020.

    We know that objects are at the centre of OOP. However, there are use cases that don't need to store state. Class methods operate only on the input arguments they receive. In this case, we use a static class. In .NET, System.Math is an example of a static class.

    A static class contains only static members. It can't be instantiated into objects. It's accessed by its class name, and not by instance variable name as is customary with objects.

    Sometimes we wish to share an attribute across all objects of a class. For example, numberOfBicycles is an attribute that should be shared across all Bicycle objects. Java's static modifier can be used to make this a static variable. Likewise, a method to access this static attribute is called a static method. Static methods can access only static members. The class itself need not be static.

  • What are some additional concepts in OOP?

    Some OOP concepts are not common to all languages or were introduced later as languages evolved. We mention a few of these:

    • Multiple Inheritance: This is when a subclass inherits from multiple base classes. C++ and Python support this. In Java and C#, a class can implement multiple interfaces but not inherit from multiple classes.
    • Generics: Classes, methods and interfaces that can be parameterized are called generics. For example, a class might implement a set of objects with parameterized type. Thus, the same class definition can be used to instantiate a set of Shape objects or Car objects.
    • Templates: Whereas types in generics are substituted at runtime, types in templates are specialized at compile time. Each template specialization has its own compiled code. C++ and D support templates.
    • Delegates: We can call a method via its delegate that has a compatible method signature. With delegates, methods can be passed as arguments to other methods.
    • Reflection: Not exclusive to OOP, reflection allows a class to introspect and modify its own definition. In Java, reflection is possible on classes, interfaces, fields and methods at runtime. This is often useful for testing, data serialization and metaprogramming.

Sample Code

  • // Source: https://docs.oracle.com/javase/tutorial/java/concepts/class.html
    // Accessed: 2020-03-26
    // Java code showing a class and some instances/objects of that class
     
    class Bicycle {
     
        int cadence = 0;
        int speed = 0;
        int gear = 1;
     
        void changeCadence(int newValue) {
             cadence = newValue;
        }
     
        void changeGear(int newValue) {
             gear = newValue;
        }
     
        void speedUp(int increment) {
             speed = speed + increment;
        }
     
        void applyBrakes(int decrement) {
             speed = speed - decrement;
        }
     
        void printStates() {
             System.out.println("cadence:" +
                 cadence + " speed:" +
                 speed + " gear:" + gear);
        }
    }
     
    // This is another class that creates Bicycle objects and calls their instance methods
    class BicycleDemo {
        public static void main(String[] args) {
     
            // Create two different
            // Bicycle objects
            Bicycle bike1 = new Bicycle();
            Bicycle bike2 = new Bicycle();
     
            // Invoke methods on
            // those objects
            bike1.changeCadence(50);
            bike1.speedUp(10);
            bike1.changeGear(2);
            bike1.printStates();
     
            bike2.changeCadence(50);
            bike2.speedUp(10);
            bike2.changeGear(2);
            bike2.changeCadence(40);
            bike2.speedUp(10);
            bike2.changeGear(3);
            bike2.printStates();
        }
    }

References

  1. Alex. 2008. "Inheritance and access specifiers." Sec. 11.5, In: CPP Tutorial, LearnCpp.com, January 14. Updated 2020-03-15. Accessed 2020-03-25.
  2. CoreJavaGuru. 2020. "Inheritence in Java with Examples." CoreJavaGuru. Accessed 2020-03-26.
  3. Forget Code. 2020. "Access Specifiers(Public,Protected,Private) ,Static and Non Stating Member Functions and Variables in Java." Forget Code. Accessed 2020-03-25.
  4. Gegick, Michael, and Sean Barnum. 2005. "Least Privilege." CISA, Department of Homeland Security, © Cigital Inc., September 14. Updated 2013-05-10. Accessed 2020-03-25.
  5. Guglani, Jitin. 2010. "do objects have seprate copies of the instance methods as they have separate copies." CodeRanch, June 30. Accessed 2020-03-26.
  6. Gökalp, Gökhan. 2019. "Understanding SOLID Principles, keep calm! For Cloud-Native Applications." July 17. Accessed 2020-03-26.
  7. Hock-Chuan, Chua. 2020. "Java Programming Tutorial: OOP - Composition, Inheritance & Polymorphism." Nanyang Technological University, February. Accessed 2020-03-26.
  8. Khillar, Sagar. 2019. "Difference Between Composition and Inheritance." DifferenceBetween.net, February 7. Updated 2019-06-24. Accessed 2020-03-26.
  9. Klempfner, David. 2019. "The Best Programmers Are Lazy." Medium, December 14. Accessed 2020-03-25.
  10. KnowledgeHills. 2016. "Python Classes, Objects and OOP concepts." KnowledgeHills, June 28. Updated 2016-06-30. Accessed 2020-03-26.
  11. Lowe, Steven. 2015. "Composition vs. Inheritance: How to Choose?" Blog, ThoughtWorks, May 11. Accessed 2020-03-26.
  12. MDN Web Docs. 2020. "Inheritance in JavaScript." MDN Web Docs, February 21. Accessed 2020-03-26.
  13. Microsoft Docs. 2015. "Static Classes and Static Class Members (C# Programming Guide)." C# Guide, Microsoft Docs, July 20. Accessed 2020-03-26.
  14. Microsoft Docs. 2018. "Generics and Templates (C++/CLI)." Visual Studio 2019, Microsoft Docs, October 12. Accessed 2020-03-27.
  15. Microsoft Docs. 2020. "Object-Oriented programming (C#)." C# Guide, Microsoft Docs, February 8. Accessed 2020-03-25.
  16. Milea, Andrei. 2019. "Constructors and Destructors in C++." Cprogramming.com. Accessed 2020-03-26.
  17. Millington, Sam. 2019. "A Solid Guide to SOLID Principles." Baeldung, February 10. Accessed 2019-05-23.
  18. MyPearsonStore. 2019. "Agile Software Development: Principles, Patterns, and Practices by Robert C. Martin." Pearson. Accessed 2019-05-23.
  19. Nookala, Siva. 2016. "Java Program To Find Rectangle Area & Perimeter Using Classes." meritcampus, March 14. Accessed 2020-03-26.
  20. OMG. 2020. "About the Unified Modeling Language Specification Version 2.5.1." Object Management Group. Accessed 2020-03-26.
  21. Oracle Docs. 2020a. "What Is an Object?" The Java™ Tutorials, Oracle Docs. Accessed 2020-03-25.
  22. Oracle Docs. 2020b. "What Is a Class?" The Java™ Tutorials, Oracle Docs. Accessed 2020-03-25.
  23. Oracle Docs. 2020c. "Examining Class Modifiers and Types." The Java™ Tutorials, Oracle Docs. Accessed 2020-03-26.
  24. Oracle Docs. 2020d. "Multiple Inheritance of State, Implementation, and Type." The Java™ Tutorials, Oracle Docs. Accessed 2020-03-26.
  25. Oracle Docs. 2020e. "Abstract Methods and Classes." The Java™ Tutorials, Oracle Docs. Accessed 2020-03-26.
  26. Oracle Docs. 2020f. "Understanding Class Members." The Java™ Tutorials, Oracle Docs. Accessed 2020-03-26.
  27. Paul, Javin. 2017. "Difference between Abstraction and Encapsulation in Java - OOP." Java Revisited, April 17. Accessed 2020-03-26.
  28. Ruhil, Praveen. 2019. "Kotlin Visibility Modifiers." GeeksforGeeks, June 20. Accessed 2020-03-25.
  29. Sakpal, Tanmay. 2018. "Java – Introduction to Object Oriented Programming [OOP]." Simple Snippets, March 31. Accessed 2020-03-26.
  30. Sedgewick, Robert, and Kevin Wayne. 2020. "Creating Data Types." Sec. 3.2 in: Computer Science: An Interdisciplinary Approach, February 28. Accessed 2020-03-26.
  31. Shute, Gary. 2020. "Object-Oriented Terminology." Software Engineering, University of Minnesota. Accessed 2020-03-26.
  32. Steiger, Stefan. 2014. "In C#, what is the difference between public, private, protected, and having no access modifier?" StackOverflow, April 11. Asked 2009-03-05. Accessed 2020-03-25.
  33. Trachtenberg, Adam. 2004. "Upgrading to PHP 5." O'Reilly Media, Inc., July. Accessed 2020-03-26.
  34. UML-Diagrams. 2020. "Object-Oriented Design Concepts in UML." UML-Diagrams. Accessed 2020-03-25.
  35. Umair, Muhammad. 2018. "A Systematic Approach to Write Better Code With OOP Concepts." DZone, May 1. Accessed 2020-03-25.
  36. Wagner, Gerd. 2015. "Really Understanding Association, Aggregation, and Composition." Code Project, April 6. Accessed 2020-03-25.
  37. Wikipedia. 2019a. "Object-oriented design." Wikipedia, September 8. Accessed 2020-03-25.
  38. Wikipedia. 2019b. "Reflection (computer programming)." Wikipedia, December 30. Accessed 2020-03-26.
  39. Wikipedia. 2020a. "Object-oriented programming." Wikipedia, March 17. Accessed 2020-03-25.
  40. Wikipedia. 2020b. "Procedural programming." Wikipedia, February 26. Accessed 2020-03-25.
  41. Wikipedia. 2020c. "Unified Modeling Language." Wikipedia, March 2. Accessed 2020-03-26.
  42. Wikipedia. 2020d. "Multiple inheritance." Wikipedia, March 11. Accessed 2020-03-26.
  43. Wikipedia. 2020e. "Mixin." Wikipedia, January 24. Accessed 2020-03-26.
  44. Wikipedia. 2020f. "Design Patterns." Wikipedia, January 11. Accessed 2020-03-26.
  45. Wikipedia. 2020g. "Design Patterns." Wikipedia, March 7. Accessed 2020-03-27.
  46. freeCodeCamp. 2020. "Getters & Setters." Java Guide, freeCodeCamp. Accessed 2020-03-25.

Milestones

1967

Simula 67 is born, officially the first object-oriented programming language. It's initially used in computer simulations. Classes in this language are called processes since object lifecycle depends on the process executing the statements.

1985

In the mid-1980s, many systems and application programmers adopt C++ for OOP. It takes another decade (1990s) for OOP to become more widely adopted.

Oct
1994

Object-oriented programming (OOP) and object-oriented design (OOD) are quite complex. To give software architects and developers guidelines to get these right, principles and design patterns have evolved over the years. At ACM's annual OOPSLA conference, four authors announce their book titled Design Patterns: Elements of Reusable Object-Oriented Software. This book describes 23 software design patterns. In time, this book becomes an essential read for OOP and OOD.

Dec
1997

Object Management Group (OMG) standardizes Unified Modelling Language (UML), v1.1. It's roots are in OOP. It's evolution can be traced to the early 1990s. UML establishes a common language among stakeholders involved in the design of object-oriented software and systems. Subsequently, UML undergoes many revisions, with v2.5.1 coming out in December 2017.

2002
SOLID design principles. Source: Gökalp 2019.

Robert Martin publishes a book on Agile where he describes five principles of OOD. Sometime later, Michael Feathers coins the term SOLID as a useful way to remember the principles.

Tags

See Also

Further Reading

  1. Umair, Muhammad. 2018. "A Systematic Approach to Write Better Code With OOP Concepts." DZone, May 1. Accessed 2020-03-25.
  2. Mulonda, Yann. 2018. "Object-Oriented Programming Concepts “In Simple English”." Noteworthy - The Journal Blog, on Medium, June 10. Accessed 2020-03-25.
  3. Stackify. 2017. "What Are OOP Concepts in Java? The Four Main OOP Concepts in Java, How They Work, Examples, and More." Stackify, April 5. Accessed 2020-03-25.
  4. Paul, Javin. 2019. "10 OOP Design Principles Every Programmer Should Know." Hackernoon, May 5. Accessed 2020-03-25.
  5. UML-Diagrams. 2020. "Object-Oriented Design Concepts in UML." UML-Diagrams. Accessed 2020-03-25.

Article Stats

Author-wise Stats for Article Edits

Author
No. of Edits
No. of Chats
DevCoins
3
0
1402
1
0
30
1881
Words
0
Chats
4
Edits
3
Likes
1277
Hits

Cite As

Devopedia. 2020. "Object-Oriented Programming Concepts." Version 4, March 27. Accessed 2020-09-19. https://devopedia.org/object-oriented-programming-concepts