In my previous post, I started discussion on Object Oriented Programming click here if you missed it where I explained Encapsulation and Inheritance as OOP concepts. This post will be wrapping up this lesson with Abstraction and Polymorphism concepts.
Abstraction:This is a process whereby important details of a method are hidden and only the functionality is shown to the user. For instance, Abstraction will only tell the user things like a class can cook, wash, and fly by listing out the functions but not how the actual cooking, washing and flying are done. In Java, the ‘what a class can do’ is known as method while the ‘how the method does them’ is called Implementation. Therefore we can simply say abstraction list methods and hides implementation.
Abstraction can be achieved using either of the two ways below:
- Abstract Class – A class declared with an abstract keyword is called an abstract class. It can have both abstract (non-implemented) and non-abstract methods (implemented) as shown in the snippet below.
The abstract methods of an abstract class can be implemented using inheritance i.e. in other to use an abstract class; an inheriting class must extend the abstract class. The class inheriting an abstract class must implement all the abstract method within the Abstract class. See example below as we try to implement the abstract methods in our class above.
- Interfaces- An interface is a collection of abstract methods. It similar to a class but can only have an abstract methods.
To make use of an Interface, a class uses the implements keyword as shown below. All the methods within an interface must be implemented.
- We need Abstraction mainly to hide unnecessary complexity
- In Java youcannot create instance of an abstract class, it’s a compiler error. You can only implement an abstract class.
- If a class extends an abstract class or implements an Interface, it must provide implementation to all its abstract method to be a concrete class. Alternatively this class can also be abstract.
The word polymorphism is a biological term that describes the ability of an organism or species to have many different form or stages. Also in Java, a similar principle can be applied whereby a method can act differently depending on the object that is acting over it. This principle can be achieved using either by using Method Overloading or Method Overriding. We discussed method in my earlier post; click here in case you missed it.
Method Overloading: This is a situation whereby two or more methods have the same name but different argument list. For methods to be said to have been overloaded, the argument list must differ in
- Number of parameter OR
- Parameter Data Type OR
- Sequence of Parameter Data Type
See example below
Method Overriding: This is a situation whereby a subclass of a class has the same method as its super class but different implementation. In this case, the subclass is said to have overridden the functionality of its super class. We use method overriding in other to tweak or customize the functionality of a super class method to suit the subclass. Remember our inheritance example, see how the subclass Nigeria can override Country method.
Remember to play around with these concepts and have fun learning!!!