[Java] Implicit inheritance memo

Object class

The Object class is the ancestor of every class. It has been inherited. It has the following methods.

--ʻEquals () : Check if an instance is the same as your own self. --toString ()`: Returns a string representation of its own content.

By having an Object class, you can use polymorphism to create "methods that can pass instances because they can be of any type", and you can define "methods that all classes want to have at least".

Something like System.out.println can accept an object as an argument. So

Sub.java


public class Sub{
    //Assuming that something is written
}

Main.java


public class Main
    public static void main(String[] args){
        Sub s = new Sub();

        System.out.println(s);            // (1)
        System.out.println(s.toString()); // (2)
}

The output results of (1) and (2) are the same.

Execution result.


test.Sub@5c8da962
test.Sub@5c8da962

toString method

Override and use. In the previous example,

Sub.java


public class Sub {
	String name = "Honda Mio";

	public String toString(){
		return this.name;
	}
}

Main.java


public class Main
    public static void main(String[] args){
        Sub s = new Sub();
        System.out.println(s);            // (1)
        System.out.println(s.toString()); // (2)
}

Execution result.


Honda Mio// (1)
Honda Mio// (2)

What kind of program is this? (Too rough)

The toString method itself of the String class is

String.java


public class toString(){
    return this;
}

This seems to be the only one. I pressed F3 in Eclipse and read it, but it was on line 2806.

equals method

It may be used for equivalence judgment and equality judgment.

-** Equal value : The referenced address is the same. Determined by the == operator. - Equivalent **: The referenced addresses are different, but the values are the same. Determined by the ʻequals` method.

Rough recognition. The plain ʻequals` method itself is

String.java


    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String aString = (String)anObject;
            if (coder() == aString.coder()) {
                return isLatin1() ? StringLatin1.equals(value, aString.value)
                                  : StringUTF16.equals(value, aString.value);
            }
        }
        return false;

(It was on line 1002) If you take a closer look at this now, it's night and it looks like you'll stay up all night. But, very roughly speaking, it means that they are actually doing equality judgment. So, when you actually use it, decide ** what is equivalent **.

Here, if the name field is the same, Yoshi! So ...

Sub.java


public class Sub {
	String name = "Honda Mio";

	public boolean equals(Object o){
		//If they are equal(Definitely equivalent)
		if (this == o) {
			return true;
		}
		
		//If they are equivalent
		if (o instanceof Sub) {
			Sub s = (Sub) o;
			if (this.name == s.name) {
				return true;
			}
		}
		
		//If neither equal nor equivalent
		return false;
	}
}

Main.java


public class Main {
	public static void main(String[] args) {
		//Create 3 instances
		Sub s1 = new Sub();
		Sub s2 = new Sub();
		Sub s3 = new Sub();

		//Enter a value in each name field
		s1.name = "Honda Mio";
		s2.name = "Honda Mio";
		s3.name = "Keisuke Honda";

		//Output the result of equivalence judgment
		System.out.pritln( s1.equals(s2) );
		System.out.pritln( s1.equals(s3) );
	}
}

Output result.


true
false

It's a really crude program Well, it's a reminder of how to use it, so anything is fine ...

Reference book

[Introduction to Java 2nd Edition] (https://www.amazon.co.jp/%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA%E3%82%8F%E3%81%8B%E3%82%8BJava%E5%85%A5%E9%96%80-%E7%AC%AC2%E7%89%88-%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA%E3%82%B7%E3%83%AA%E3%83%BC%E3%82%BA-%E4%B8%AD%E5%B1%B1-%E6%B8%85%E5%96%AC/dp/484433638X) Pp.541-551

Recommended Posts

[Java] Implicit inheritance memo
Java learning memo (inheritance)
[Java] Inheritance
Java memo
Java inheritance
Java inheritance
java (inheritance)
java anything memo
[Java] Class inheritance
Java Silver memo
Java SE 7 memo
java anything memo 2
About java inheritance
Java specification memo
Java pattern memo
Java development environment memo
java basic knowledge memo
Java learning memo (method)
Java Kuche Day memo
Summarize Java inheritance (Java Silver 8)
[Java ~ Method ~] Study memo (5)
java se 8 programmer Ⅰ memo
Java paid private memo
About inheritance (Java Silver)
[Java ~ Array ~] Study memo 4
Java learning memo (basic)
(Memo) Java for statement
Java lambda expression [memo]
Java learning memo (interface)
java competitive programming memo
[Memo] Java Linked List
Java (WebSphere Application Server) memo [1]
Advanced inheritance abstract, interface -java
[Java] Variable name naming memo
Java memo (standard class) substring
JAVA learning history interface inheritance
Java learning memo (data type)
Java memo (standard class) length
Java Silver Study Method Memo
[Java ~ Boolean value ~] Study memo (2)
Create a java method [Memo] [java11]
Java Silver exam preparation memo
Java learning memo (logical operator)
Java learning memo (abstract class)
[Java] Date Related Term Memo
Java study memo 2 with Progate
[Java] What is class inheritance?
java (inheritance of is-a principle)
What are Java metrics? _Memo_20200818
Java HashMap, entrySet [Personal memo]
Muscle Java Object Oriented Day 2 ~ Inheritance ~
[Eclipse Java] Development environment setting memo
Java learning memo (creating an array)
JCA (Java Cryptography Architecture) Usage Memo
Inheritance
[Java] Memo for naming class names
Inheritance
[Study session memo] Java Day Tokyo 2017
Java learning memo (while statement, do-while statement)
Java
From Java to VB.NET-Writing Contrast Memo-