Demonstrating object oriented concepts in Java
- Abstraction is when only the essential details are shown to the user. Abstract classes can contain both abstract and non-abstract methods. Abstract methods do not show the implementation of the method, or in other words, they do not have a body. The main advantage of using an abstract class is that similar classes can be grouped together. This helps to keep a program organized and improves the readability.
- Inheritance is used to create classes quickly without having to code everything from scratch. Child classes can inherit the attributes, methods, and constructors of the parent while also adding any needed modifications. Inheritance saves time because similar classes can be made quickly and with fewer lines of code. The main disadvantage of inheritance is that the classes are tightly coupled, meaning that if the parent class is changed, the child class must be changed as well.
- Encapsulation is binding the attributes and the methods acting on the attributes together as a single unit. In encapsulation, the data in a class is hidden from other classes, and can only be accessed through public methods that allow the data to be accessed and changed. The main advantage of encapsulation is data-hiding. This means that the implementation of the class is hidden from the user. Encapsulation also allows the attributes of a class to be read-only or write-only.
- Polymorphism is the ability of a class to be take on multiple forms/implementations. In object oriented programming, polymorphism occurs when a parent class static type is used for a child class dynamic type. Polymorphism allows for specific implementations of an object through method overriding. A disadvantage of using polymorphism is that is reduces readibility as it is hard to determine what the runtime type of the object is, which can make debugging harder.