[Java] Precautions when referencing a reference type in a parent class and instantiating it in a child class

parent p = new child()Whenchild c = new child()A memorandum of what is different in

important point

Assumed case

class Parent{
	int a = 1;
	static int b = 2;
	
	String medhod1() {
		return "parent method1";
	}
	
	static String medhod2() {
		return "parent method2";
	}
}


class Child extends Parent{
	int a = 10;
	int b = 20;
	
	@Override
	String medhod1() {
		return "child method1";
	}
	
	static String medhod2() {
		return "child method2";
	}
}

Execution result

		Parent parent = new Child();

		System.out.println(parent.a); // =>1 ・ ・ ・ ★
		System.out.println(parent.b); // =>2 ・ ・ ・ ★
		System.out.println(parent.medhod1());  // =>child method1 ・ ・ ・ ★
		System.out.println(parent.medhod2());  // => parent method2
		
		Child child = new Child();
		System.out.println(child.a);  // => 10
		System.out.println(child.b);  // => 20
		System.out.println(child.medhod1());  // => child method1
		System.out.println(child.medhod2());  // => child method1

Recommended Posts

[Java] Precautions when referencing a reference type in a parent class and instantiating it in a child class
Write a class in Kotlin and call it in Java
[Java] Difference between equals and == in a character string that is a reference type
When deploying to Heroku, OpenApp causes an error
[Ruby On Rails] Causes and remedies for ActionView :: Template :: Error (ActiveStorage :: InvariableError) (hypothesis / verification)
Error logging and exception handling that you can't often see in the Java area
[Beginner] Commands and knowledge that may be useful for error resolution when deploying AWS
[Java] Precautions when referencing a reference type in a parent class and instantiating it in a child class
Implement Java Interface in JRuby class and call it from Java
StringBuffer and StringBuilder Class in Java
I wrote a Lambda function in Java and deployed it with SAM
About returning a reference in a Java Getter
What is a class in Java language (3 /?)
[Easy-to-understand explanation! ] Reference type type conversion in Java
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Creating a matrix class in Java Part 1
How to ZIP a JAVA CSV file and manage it in a Byte array
Cause of is not visible when calling a method of another class in java
When using a list in Java, java.awt.List comes out and an error occurs
GetInstance () from a @Singleton class in Groovy from Java
A note when you want Tuple in Java
A quick review of Java learned in class
A memo when I tried "Talking about writing a Java application in Eclipse and publishing it in Kubernetes with a Liberty container (Part 1)"
A quick review of Java learned in class part4
Write a class that can be ordered in Java
[Java] When var should be used and when it should not be used
About Java basic data types and reference type memory
What I learned when building a server in Java
How to convert A to a and a to A using AND and OR in Java
A quick review of Java learned in class part3
A quick review of Java learned in class part2
Use of Abstract Class and Interface properly in Java
I was trapped when I generated my own class equals and hashCode in Java using IDE