[RUBY] How to create a class that inherits class information

Contents

If you want to define the same method in multiple classes, and if you define that method in all of each class, you will end up repeating the same description over and over again. (Example 1) Class 1 Class 2 Class 3 Method A Method A Method A Method B Method C Method D When multiple classes have the same method, the more classes you have, the more code you have and the more difficult it is to manage. Let's learn about class inheritance to prevent this.

What is class inheritance?

Defining a class after making a method defined in one class available in another new class is called inheritance.

Class inheritance has a parent-child relationship. The original class is called the parent class, and the newly created class that inherits the methods of the parent class is called the child class. image.png Create a class of "cars", which is a superordinate concept of police cars and trucks, and define common features there. By inheriting the characteristics of the car (parent class), it is not necessary to describe the characteristics of the car in the police car (child class) and truck (child class). As a result, you only have to write the characteristics of the police car and truck, and each characteristic is easy to understand.

When inheriting a class, use "<" when declaring the class and write as follows.


class child class name<Parent class name
end

Now, let's write the example of the car above with the code that actually uses the inheritance of the class. First, define the parent class.

Car class (parent class)

class Car
  def speed_up
    puts "Accelerate"
  end

  def speed_down
    puts "Decelerate"
  end

  def horn
    puts "Pupoo"
  end
end

The parent class defines the common behavior of the car. Next, define each child class. This time, police cars and trucks are defined as Patrol Car and Truck Car, respectively.

PatrolCar class (child class)

class PatrolCar < Car  #Class inheritance
  def siren
    puts "Pea Po Pea Po"
  end
end

TruckCar class (child class)

class TruckCar < Car  #Class inheritance
  def carry
    puts "I will carry my luggage"
  end
end

By defining a method common to the parent class, the amount of code in the child class is reduced and it is easier to see. Also, this is a big advantage of using inheritance, but you can see from the above code that you can easily reflect the change to the child class by simply changing the common method defined in the parent class.

Finally

By using class inheritance, you can write neat code without repeatedly writing common methods, so please write it!

Recommended Posts

How to create a class that inherits class information
How to create a method
[Java] How to create a folder
How to create a Maven repository for 2020
[Swift5] How to create a splash screen
[rails] How to create a partial template
How to create a database for H2 Database anywhere
[Android] Inherit ImageView to create a new class
How to quickly create a reverse proxy that supports HTTPS with Docker
[Rails] How to create a graph using lazy_high_charts
How to create pagination for a "kaminari" array
How to convert a solidity contract to a Java contract class
How to create a theme in Liferay 7 / DXP
How to create a validator that allows only input to any one field
[1st] How to create a Spring-MVC framework project
How to create docker-compose
How to easily create a pull-down in Rails
[Rails] How to create a Twitter share button
How to create a convenient method that utilizes generics and functional interfaces
How to test a class that handles application.properties with SpringBoot (request: pointed out)
[Rails] How to create a signed URL for CloudFront
How to update devise user information without a password
How to create a JDBC URL (Oracle Database, Thin)
How to create a Spring Boot project in IntelliJ
[Spring Boot] How to create a project (for beginners)
How to create a data URI (base64) in Java
How to create and execute method, Proc, Method class objects
[Apple Subscription Offer] How to create a promotional offer signature
How to create an application
How to use java class
How to insert a video
How to create a lightweight container image for Java apps
How to create a form to select a date from the calendar
How to move another class with a button action of another class.
Mapping to a class with a value object in How to MyBatis
How to create a service builder portlet in Liferay 7 / DXP
How to create and launch a Dockerfile for Payara Micro
[rails] How to display db information
Preparing to create a Rails application
How to make a Java container
How to disassemble Java class files
How to use the wrapper class
How to sign a Minecraft MOD
How to make a JDBC driver
How to write a ternary operator
How to decompile java class files
[Java] How to use LinkedHashMap class
[Swift] How to send a notification
How to make a splash screen
How to make a Jenkins plugin
How to use class methods [Java]
How to make a Maven project
Try to create a server-client app
How to make a Java array
[Java] How to use Math class
How to create a jar file or war file using the jar command
How to create a registration / update function where the table crosses
[Rails 6] How to create a dynamic form input screen using cocoon
Create a static file that expands variables using the ERB class
How to deploy an app that references a local jar to heroku
Easy way to create a mapping class when using the API