Java: Using the concept of inheritance to make programming code more structured – Desiliciouskitchen

Java: Using the concept of inheritance to make programming code more structured

Desiliciouskitchen-In the world of programming, programmers usually make a very detailed program code so that every line of program code runs smoothly. The detailed code is usually very little (shorter in code generation) and easy to understand every line. Sometimes for beginners who have just touched the world of programming, usually in code generation it will be very much in making a line of code.

There’s nothing wrong with very many lines of code, but it can make it more difficult for a programmer to fix an error in the code and take a very long time to fix. Therefore, Java created a concept where programming code can be more structured and easier during code generation.

In this article, we will explain the concept of inheritance which is one of the concepts to make code more structured and easier during code generation. The following is an explanation and examples to make it easier to understand.

Read This: Know the difference between a web developer and a web designer

Inheritance

Inheritance is a concept of inheritance in the form of variables and functions owned by a class to be passed on to other classes.

The concept of inheritance itself is to create a class structure in programming where the structure has a Parentclass or Superclass as the parent class and a Subclass as a child class. The concept is a branching of a class (Superclass) which has general properties into a class (Subclass) which has more specific properties.

In inheritance, the superclass can pass the properties it has to each subclass, but not all the properties possessed by the superclass can be inherited by the subclass.

✔️ READ  Comparison Of Java, Kotlin And Dart (Flutter) Programming Languages From An Android Development Perspective - Desiliciouskitchen

Types of access / access rights that can be inherited from superclass to subclass are protected and public, while private will not be passed down to subclasses. an example of the concept of inheritance: A geometric shape is a triangle. Triangle is a parentclass that has a base and height as well as the formula 1/2 x base x height. Right triangles, equilateral triangles, isosceles triangles and arbitrary triangles are a subclass of triangles. Then the subclass has the base, height and formula values ​​from the parentclass.

In making code based on the concept of inheritance, programmers usually also use the overriding method or the overloading method for subclasses so that the program can run smoothly. For the discussion of the ineritance concept here, we will only use the overriding method, here is an explanation of the overriding method on the inheritance concept.

Read This: Recommended 5 Web Design Software for Designing Websites Without Coding

Overriding Method

Method overriding is a re-creation of an existing method in a superclass in a subclass or child class. The overriding method is used so that the subclass has a more specific function. Like the example in the inheritance concept above, Subclasses only have base, height and formula values. So by using the overriding method, the subclass can create its own formula which is more specific than the parentclass.

To better understand the concept of inheritance and overriding methods, let’s understand the following cases and their programming:

Examples of Inheritance Cases and Overriding Methods

A food franchise consisting of several employees such as managers, waiters, chefs, cashiers and security guards. Each employee has a name, employee id and salary. For employees with employee ID 01, they have the responsibility as a manager with a salary of 7 million. Employees with employee id 02 have the task of conducting transactions with buyers and a salary of 1 million.

✔️ READ  Difference between Java and PHP - Desiliciouskitchen

An employee with employee id 03 has the task of cooking food with a salary of 2 million. An employee with employee id 4 has the task of serving and serving every food ordered by the buyer with a salary of 1 million. Employees with employee ID 05 have the duty to maintain security inside or outside the franchise with a salary of 1 million.

From the above case, we can see that:

  1. Employee has a name String; int id_employee; salary string; void display();
  2. Manager has void task(); void display();
  3. The cashier has void task(); void display();
  4. The chef has void task(); void display();
  5. The server has void task(); void display();
  6. Security guard has void task(); void display();

Leave a Reply

Your email address will not be published.