Polymorphism gives us a way to use a class exactly like its parent so there is no confusion with mixing types. ![]() Apple the sub-class acquires these properties from Fruit and has some unique properties, which are different from other sub-classes of Fruit such as red, round, depression at the top. Fruit defines a class of foods that are mature ovaries of a plant, fleshy, contains a large seed within or numerous tiny seeds. Other classifications could be grape, pear, and mango, etc. Our Apple acquires the properties of the Fruit class. For example, Apple is a fruit so assume that we have a class Fruit and a subclass of it named Apple. In Java, there are various types of inheritances: single, multiple, multilevel, hierarchical, and hybrid. You can reuse the fields and methods of the existing class. With inheritance, reusability is a major advantage. For example, a child inherits the traits of his/her parents. Inheritance is the ability of one object to acquire some/all properties of another object. One of the advantages of Abstraction is being able to apply the same information you used for the dating application to other applications with little or no modification. The process of fetching/removing/selecting the user information from a larger pool is referred to as Abstraction. Data like Full name, favorite food, favorite movie, and hobbies make sense for a dating application. You only need to select the information that is pertinent to your dating application from that pool. This amount of data is great however not all of it is required to create a dating profile. You might receive the following data from your user: Full name, address, phone number, favorite food, favorite movie, hobbies, tax information, social security number, credit score. Suppose you want to create a dating application and you are asked to collect all the information about your users. It is the process of selecting data from a larger pool to show only the relevant details to the object. ![]() AbstractionĪbstraction is an extension of encapsulation. Each of these functions modifies the internal state of the Dog class and may invoke bark(), when this happens the private state and public methods are bonded. There are also public methods such as sleep(), play() and eat() that are available to other classes. The dog class can call this whenever it wants, and the other classes can not tell the dog when to bark. The “state” of the dog is in the private variables playful, hungry and energy and each of these variables has their respective fields. If we want to apply encapsulation, we do so by encapsulating all “dog” logic into a Dog class. One way I like to think of encapsulation is by using the example of people and their dogs. In order to communicate with the object, you will need to utilize the methods provided. The object manages its own state via these functions and no other class can alter it unless explicitly allowed. Other objects can not access this state directly, instead, they can only invoke a list of public functions. The Four Principles of Object-Oriented-Programming (OOP): EncapsulationĮncapsulation is accomplished when each object maintains a private state, inside a class. ![]() Now that we have covered these keywords, let’s jump into the four principles of object-oriented-programming: Encapsulation, Abstraction, Inheritance, and Polymorphism. Your car that has been made from that blueprint is an instance of that class. Instance: These are like Objects, however, let’s think about it in these terms: A blueprint for a car design is the class description, all the cars manufactured from that blueprint are objects of that class. ![]() Method: Can modify a class state that would apply across all the instances of the class Object: The instance of a class / it’s the working entity of a classĬlass: This is the model or standard about the capability of what an object can do In OOP, objects have fields to store knowledge/state/data and can-do various methods.īefore diving into the four pillars of OOP, I would like to go over some of the basic terminologies. In your everyday life, people have the knowledge and can-do various works/tasks. Object-Oriented-Programing allows programmers to think of software development as if they are working with real-life entities. In this article, I will try to explain the main four principles of Object-Oriented-Programming (OOP).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |