[Java] What is class inheritance?

What is class inheritance?

Inheritance is a function to describe the process unique to the child class while inheriting the common process by creating a child class (subclass) based on the parent class (super class). With inheritance, you don't have to write similar programs over and over again to streamline your work and make the entire source code easier to read.

Java class inheritance method

When inheriting, add "extends" after the class declaration and specify the parent class name as the inheritance source. However, there is only one class that inherits from Java. It is not possible to inherit two classes at the same time (multiple inheritance prohibited).

class class name extends the original class name{
"Difference" member with parent class

When using the method or constructor of the inherited class, use "super" to access and refer to it.

class class name extends the original class name(argument) {
  //Call the constructor of the parent class
  //Call the method of the parent instance part
  super.Method name(argument);

Method override

Overriding is overriding the contents of the method defined in the parent class. It is used when you want to change the contents of the method defined in the parent class a little. The condition for overriding is that the method name, return value, and arguments are the same.

[Parent class]
    public void output() {
[Child class]
    public void output() {

It's a convenient override because you can reuse the parent class (superclass) and change the method you want to change, but there are some rules you must follow when using it. First, if a method on the parent class (superclass) side has the final modifier, the child class (subclass) cannot override that method. You should also be aware that variables and methods with the private modifier in the superclass are not available to subclasses.

Difference from overload

Overload is a term similar to override. It's often mistaken, so I'll explain it briefly. Overloading is the declaration of multiple "methods with the same name" in a class. The condition of overload is that ➀ method names are the same ➁ argument types, numbers, and order are different.

  public void output() {}
  public void output(String str) {}
  public void output(String str, int i) {}
  public void output(int i, String str) {}


By using class inheritance, the child class does not need to rewrite the member variables and member methods of the parent class. Doing so will also make the code easier to read and will be easier to modify and maintain later.

Recommended Posts

[Java] What is class inheritance?
[Java basics] What is Class?
[Java] Class inheritance
What is java
What is Java <>?
What is Java
What is a class in Java language (3 /?)
What is Java Encapsulation?
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
What is Java API-java
[Java] What is flatMap?
[Java] What is ArrayList?
What is a wrapper class?
What is a Java collection?
[Java] What is jaee j2ee?
What is java escape analysis?
[Java] Inheritance
Java inheritance
Java inheritance
java (inheritance)
What is JVM (Java Virtual Machine)?
What is thread safe (with Java)
[Java] What is Concurrent Modification Exception?
What is a lambda expression (Java)
What is the LocalDateTime class? [Java beginner] -Date and time class-
What is Cubby
What is null? ]
java Scanner class
What is Keycloak
What is maven?
What is Jackson?
java (abstract class)
What is self
What is Jenkins
What is IM-Juggling?
What is the best file reading (Java)
What is params
What is SLF4J?
[Java] Nested class
Java anonymous class
What is Facade? ??
What is Gradle?
What is POJO
What is the main method in Java?
About Java class
What is centOS
What is RubyGem?
About java inheritance
What is before_action?
What is Docker
[java] abstract class
[Java] Object class
What is Byte?
Java local class
What is Tomcat
Java Basic Learning Content 6 (Inheritance / Abstract Class / Interface)
What is the Java Servlet / JSP MVC model?
What is the volatile modifier for Java variables?
Java and Swift comparison (3) Class implementation / Class inheritance / Class design
[Java] Overload / Override / Inheritance