In programming, there are a lot of different terms that are used to describe the same exact thing and there are also some terms that appear to refer to the same thing but actually don’t. To make it even more confusing. Some terms are used differently depending on what kind of language you are talking about or even when going from one language to another language that is of the same type. Thus, some terms like, “method”, “function”, “interface”, or “abstract class” can be very confusing to some people As they can mean one thing in one context but a completely different thing in another.
A method is essentially a step by step procedure that is enacted upon a designated object. Methods are only used in object oriented programming languages like C++. Essentially, a method either can alter the characteristics of the object that is passed through the method, or the method can do a certain procedure based on the pre-existing characteristics of the object passed through it. Sometimes, a method can even run through code that neither affects the object passed through it or is changed by the already existing characteristics of the object. But this is much less common and is not a promoted way of handling methods.
A function, on the other hand, is a much more generic term that refers to a procedure that takes in a number of inputs and returns some number of values. Confusingly, a function and a method sound like they would do the same thing or refer to the same kind of procedure when programming, but they don’t. Functions are not associated with objects and are used in basically every high-level language that is not object-oriented. Methods on the other hand are only associated with objects and the term is only used when dealing with objects or when a procedure is within a class. So, functions return values based on the inputs given, whereas methods either alter the characteristics of an object or return values based on the object’s pre-existing characteristics.
Similar to the confusion between a function and a method. There is a lot of confusion between interfaces and abstract classes. However, unlike functions and methods, interfaces and abstract classes have a lot in common and their characteristics are easy to mix up. The easiest way to differentiate the two is to understand that interfaces are only present in object oriented programming languages. Whereas abstract classes are present in nearly all high-level languages.
An interface is essentially a base that you create that accounts for all of the similarities between classes. So, say you want to create different objects but that they are all animals. So, humans, apes, and chimpanzees. Obviously, these three animals have a lot in common, and it would be smart to define all of these similarities, so you can ensure that they are all implemented throughout each class. For example, each has a head, four limbs, a mouth, nose, tongue, etc.
So, in the interface, you define these characteristics and because you defined them in the interface. When each class implements the interface they must define and initialize these characteristics through the defined methods in the interface. Otherwise, the code won’t run. The main reason why you would use an interface instead of an abstract class is if each animal has the same characteristics. But each animal implements them differently. For example, all chimpanzees. Humans, and apes have noses, but the width, length, and use for them may all be slightly different. Therefore, these differences are defined in the individual classes. But the fact that they have noses is defined in the interface.
An abstract class is exactly like an interface. However, unlike an interface, methods and functions can actually be defined and initialized, but they don’t have to be. On top of that, instead of classes implementing abstract classes, they make the abstract class the superclass. This is actually a major disadvantage for abstract classes when compared to interfaces, as a class can implement as many interfaces as they want, but they can only have one superclass, meaning a class can only utilize one abstract class but multiple interfaces.
Abstract classes are great. However if there are some identical characteristics that have identical ways of being implemented. But if there are also some that have different implementations across objects. For example, if you had two classes, men and women, and wanted to define similarities between the two, it may be best to use an abstract class. Why? Because certain characteristics between the genders are identical and are implemented in the exact same way. For example, the mouths are about the same size and serve the same purpose; the eyes are about the same size and function the same between the genders, etc.
So, for these characteristics, the functions or methods defining them can be initialized in the abstract class. However, certain characteristics, like the hips or eyebrows are present on both genders. But these characteristics are much different between the two. Therefore, the abstract class will not initialize the methods or functions defining these characteristics. And will leave that to the subclasses.
Hopefully these explanations were not too confusing or wordy. If you are new to programming, don’t worry if you are still confused. There are plenty of accessible resources that you can use to understand the fundamentals of programming and the subtle differences between some terms.
It is very important mentioning that if you like to become a professional programmer. Leaning both procedural and Object-Oriented Programming (OOP) concepts are a must. Indeed. Many software engineers use Unified Model Language to map project or application requirements to objects and diagrams before they do OOP coding. There are lots of online resources for learning software engineering. For teenagers and high school students, High School Technology Services offers variety of hands-on training. For adults and professionals, Coding Bootcamps institute offers many basic to advance programming classes with focuses on both procedural and OOP coding.
Matt Zand is a programmer, businessman, IT Consultant, and writer. He is the founder and owner of WEG2G Group. He is also the founder of DC Web Makers. His hobbies are hiking, biking, outdoor activities, traveling and mountain climbing.