Object-Oriented Programming Concepts
- Summary
-
Discussion
- What are objects and classes in OOP?
- What are attributes and methods of objects in OOP?
- Which are the essential concepts of OOP?
- What is meant by abstraction in OOP?
- How is composition different from inheritance in OOP?
- What are access modifiers in the context of OOP?
- What are static classes and methods?
- What are some additional concepts in OOP?
- Milestones
- Sample Code
- References
- Further Reading
- Article Stats
- Cite As
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.
Discussion
-
What are objects and classes in OOP? 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 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 belength
andbreadth
. It's methods could begetArea()
androtate()
.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 whichSquare
andCircle
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 thegetArea()
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? 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 memberscolor
andgetArea()
. SubclassesRectangle
andTriangle
implement how area is calculated. SinceShape
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 ofgetArea()
can be instantiated. Shape'sgetArea()
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
andEngine
. 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 ofVehicle
. The relationship between engine and car is best modelled as composition: anEngine
instance is an attribute of eitherVehicle
orCar
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 aDegreeProgram
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? 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. Modifierprotected
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? 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 allBicycle
objects. Java'sstatic
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 orCar
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.
Milestones
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.
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.
Sample Code
References
- Alex. 2008. "Inheritance and access specifiers." Sec. 11.5, In: CPP Tutorial, LearnCpp.com, January 14. Updated 2020-03-15. Accessed 2020-03-25.
- CoreJavaGuru. 2020. "Inheritence in Java with Examples." CoreJavaGuru. Accessed 2020-03-26.
- Forget Code. 2020. "Access Specifiers(Public,Protected,Private) ,Static and Non Stating Member Functions and Variables in Java." Forget Code. Accessed 2020-03-25.
- 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.
- Guglani, Jitin. 2010. "do objects have seprate copies of the instance methods as they have separate copies." CodeRanch, June 30. Accessed 2020-03-26.
- Gökalp, Gökhan. 2019. "Understanding SOLID Principles, keep calm! For Cloud-Native Applications." July 17. Accessed 2020-03-26.
- Hock-Chuan, Chua. 2020. "Java Programming Tutorial: OOP - Composition, Inheritance & Polymorphism." Nanyang Technological University, February. Accessed 2020-03-26.
- Khillar, Sagar. 2019. "Difference Between Composition and Inheritance." DifferenceBetween.net, February 7. Updated 2019-06-24. Accessed 2020-03-26.
- Klempfner, David. 2019. "The Best Programmers Are Lazy." Medium, December 14. Accessed 2020-03-25.
- KnowledgeHills. 2016. "Python Classes, Objects and OOP concepts." KnowledgeHills, June 28. Updated 2016-06-30. Accessed 2020-03-26.
- Lowe, Steven. 2015. "Composition vs. Inheritance: How to Choose?" Blog, ThoughtWorks, May 11. Accessed 2020-03-26.
- MDN Web Docs. 2020. "Inheritance in JavaScript." MDN Web Docs, February 21. Accessed 2020-03-26.
- Microsoft Docs. 2015. "Static Classes and Static Class Members (C# Programming Guide)." C# Guide, Microsoft Docs, July 20. Accessed 2020-03-26.
- Microsoft Docs. 2018. "Generics and Templates (C++/CLI)." Visual Studio 2019, Microsoft Docs, October 12. Accessed 2020-03-27.
- Microsoft Docs. 2020. "Object-Oriented programming (C#)." C# Guide, Microsoft Docs, February 8. Accessed 2020-03-25.
- Milea, Andrei. 2019. "Constructors and Destructors in C++." Cprogramming.com. Accessed 2020-03-26.
- Millington, Sam. 2019. "A Solid Guide to SOLID Principles." Baeldung, February 10. Accessed 2019-05-23.
- MyPearsonStore. 2019. "Agile Software Development: Principles, Patterns, and Practices by Robert C. Martin." Pearson. Accessed 2019-05-23.
- Nookala, Siva. 2016. "Java Program To Find Rectangle Area & Perimeter Using Classes." meritcampus, March 14. Accessed 2020-03-26.
- OMG. 2020. "About the Unified Modeling Language Specification Version 2.5.1." Object Management Group. Accessed 2020-03-26.
- Oracle Docs. 2020a. "What Is an Object?" The Java™ Tutorials, Oracle Docs. Accessed 2020-03-25.
- Oracle Docs. 2020b. "What Is a Class?" The Java™ Tutorials, Oracle Docs. Accessed 2020-03-25.
- Oracle Docs. 2020c. "Examining Class Modifiers and Types." The Java™ Tutorials, Oracle Docs. Accessed 2020-03-26.
- Oracle Docs. 2020d. "Multiple Inheritance of State, Implementation, and Type." The Java™ Tutorials, Oracle Docs. Accessed 2020-03-26.
- Oracle Docs. 2020e. "Abstract Methods and Classes." The Java™ Tutorials, Oracle Docs. Accessed 2020-03-26.
- Oracle Docs. 2020f. "Understanding Class Members." The Java™ Tutorials, Oracle Docs. Accessed 2020-03-26.
- Paul, Javin. 2017. "Difference between Abstraction and Encapsulation in Java - OOP." Java Revisited, April 17. Accessed 2020-03-26.
- Ruhil, Praveen. 2019. "Kotlin Visibility Modifiers." GeeksforGeeks, June 20. Accessed 2020-03-25.
- Sakpal, Tanmay. 2018. "Java – Introduction to Object Oriented Programming [OOP]." Simple Snippets, March 31. Accessed 2020-03-26.
- Sedgewick, Robert, and Kevin Wayne. 2020. "Creating Data Types." Sec. 3.2 in: Computer Science: An Interdisciplinary Approach, February 28. Accessed 2020-03-26.
- Shute, Gary. 2020. "Object-Oriented Terminology." Software Engineering, University of Minnesota. Accessed 2020-03-26.
- 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.
- Trachtenberg, Adam. 2004. "Upgrading to PHP 5." O'Reilly Media, Inc., July. Accessed 2020-03-26.
- UML-Diagrams. 2020. "Object-Oriented Design Concepts in UML." UML-Diagrams. Accessed 2020-03-25.
- Umair, Muhammad. 2018. "A Systematic Approach to Write Better Code With OOP Concepts." DZone, May 1. Accessed 2020-03-25.
- Wagner, Gerd. 2015. "Really Understanding Association, Aggregation, and Composition." Code Project, April 6. Accessed 2020-03-25.
- Wikipedia. 2019a. "Object-oriented design." Wikipedia, September 8. Accessed 2020-03-25.
- Wikipedia. 2019b. "Reflection (computer programming)." Wikipedia, December 30. Accessed 2020-03-26.
- Wikipedia. 2020a. "Object-oriented programming." Wikipedia, March 17. Accessed 2020-03-25.
- Wikipedia. 2020b. "Procedural programming." Wikipedia, February 26. Accessed 2020-03-25.
- Wikipedia. 2020c. "Unified Modeling Language." Wikipedia, March 2. Accessed 2020-03-26.
- Wikipedia. 2020d. "Multiple inheritance." Wikipedia, March 11. Accessed 2020-03-26.
- Wikipedia. 2020e. "Mixin." Wikipedia, January 24. Accessed 2020-03-26.
- Wikipedia. 2020f. "Design Patterns." Wikipedia, January 11. Accessed 2020-03-26.
- Wikipedia. 2020g. "Design Patterns." Wikipedia, March 7. Accessed 2020-03-27.
- freeCodeCamp. 2020. "Getters & Setters." Java Guide, freeCodeCamp. Accessed 2020-03-25.
Further Reading
- Umair, Muhammad. 2018. "A Systematic Approach to Write Better Code With OOP Concepts." DZone, May 1. Accessed 2020-03-25.
- Mulonda, Yann. 2018. "Object-Oriented Programming Concepts “In Simple English”." Noteworthy - The Journal Blog, on Medium, June 10. Accessed 2020-03-25.
- 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.
- Paul, Javin. 2019. "10 OOP Design Principles Every Programmer Should Know." Hackernoon, May 5. Accessed 2020-03-25.
- UML-Diagrams. 2020. "Object-Oriented Design Concepts in UML." UML-Diagrams. Accessed 2020-03-25.
Article Stats
Cite As
See Also
- SOLID Design Principles
- Design Patterns
- Design by Contract
- Object-Oriented Programming
- Unified Modelling Language
- Functional Programming