You will learn HTML CSS by following this video tutorial. Let’s enjoy the tutorial and subscribe to our channel.
Making the switch to dark mode is a really easy way to give your website a more visually appealing look while also making your website more user-friendly. With this dark-mode tutorial, you’ll learn how to create a dark-mode variant for a website. This dark-mode variant can be styled as a night-time atmosphere or a dark-day-light atmosphere.
Object Oriented Programming: Interfaces and Polymorphism
Object oriented programming topic is not very easy, in this series I will try to make the topic a little easier for you. After learning algorithms and data structures, perhaps the most important thing to learn is object oriented programming.
Anyone can write code that “works”, but if you want to write code where you have to add new features every few days, behind which many people are working together, then it’s not enough to write code like that, you need to know how to design properly, that’s where object oriented programming comes in. It becomes important to know. After many years of writing about algorithms, I felt that now it is necessary to write about these too.
Learning object oriented programming can be difficult at first. The main reason for this is that the basic concepts are not well cleared. If you don’t understand the difference between classes and objects, and the difference between interfaces and abstract classes, you will find OOP texts confusing and difficult when trying to learn design patterns.
By the time you read this article I hope you at least know about classes and objects. You know how to define methods or variables inside a class, how to create an instance of a class, what is a constructor. If you don’t know or forget them, do a little research. I will be using Java in this series.
Many people try to teach OOP using classes like Animal, Food. I’m not advocating that, I think it’s hard to get the point across without using a more practical example. I won’t just give definitions, I’ll try to take you a little deeper.
Today we will first learn about the interface. Many start early with inheritance, no problem. But the way I want to explain it is important to talk about the interface first. There are 4 main foundations of object oriented programming, two of which are polymorphism and abstraction. By reading today’s article, you will also know about them. If I had gone directly to the definition of these without teaching the interface, you would have understood nothing.
What is the interface?
Forget about programming in the beginning, we use the word interface very often in real life too. Think of the bus-truck. A person who can drive a bus can also drive a truck. The work while driving a bus-truck or a car is the same, first you have to start the engine, then you have to step on the accelerator to move it forward, you have to take it left and right with the steering wheel.
Now Volvo bus steering and Toyota car steering don’t work exactly the same, the mechanism inside the hood is different. But we don’t go inside the hood to see how the steering is “implemented”, we are given an interface called steering, it has two features “go left”, “go right”, you use them and drive, how it works inside the hood is up to you. No headache.
So an interface is a layer to communicate with a system. You are pressing different buttons on the mobile, that is also an interface, you don’t know how it works inside, you only know what the function of the button is. So an interface has two parts:
Definition of interfaces. It says what the interface can do. Actions are “abstract” to you, you don’t know what’s going on inside.
Implementation of an interface, which tells exactly how the interface works.
Interface in the world of programming
An interface is a class that contains some methods or variables but does not define them.
For example, suppose I’m building a library whose task is to find the sum of some numbers. I have two methods in my library. Using one method you can add new numbers, using another method you can find the sum of all the numbers found so far.
I would define an interface as follows:
With this one interface I just define what kind of service SummationService will provide. I have declared the signature of two methods but not defined how the method works. This type of method is called abstract method. Abstraction is to hide the implementation details and give the bare features to the user. So the method we know what to do but don’t know how to do it is Abstract Method.
Now we can define the interface a little harder, an interface is a class that only contains some abstract methods.
Now I need a class that implements the interface methods. Only then we can do something using the interface.
Notice the word implements here. This means that the SimpleSum class will implement the methods declared in the SummationService. But now there is nothing inside our class so if you try to compile this class you will get an error message:
Forget the “either be declared abstract” part for now. The error message says that SimpleSum must implement a method called addNew(int). An interface is often called a contract because if you implement an interface, you must implement all methods according to the contract.
We then implement the methods:
This time the SimpleSum class implements all the methods. Using the @Override annotation is optional here, it means that these methods implement the interface class, I will not discuss the annotation in detail here.
Now I want another kind of implementation which only sums the unique numbers instead of adding all the numbers. In that case our algorithm will be slightly different, we’ll store the numbers in a set so that every time we get a new number we can check to see if we got the number before. We create a class called UniqueSum:
We’ve got two classes that implement the interface. Now we see how to use the interface:
Here’s how I created two SummationServices. Once I used SimpleSum’s constructor, another time UniqueSum’s. But both times I assigned same type to variable. That means the SummationService interface has two different implementations.
You must Join our Facebook Group and Subscribe YouTube Channel
All Links in Below:
Join Our FreeWebsiteCreate Facebook Group to get an instant update for projects, templates, design resources, and solutions.
Join Our YouTube Channel & Subscribe with Bell Icon for New Video:
Join Our Official Facebook Page For the Latest updates All Code Projects are Free:
Visit our service page to get premium services.
You must Join our Facebook Group and Subscribe YouTube Channel