-[Latest] How to build Java environment on Ubuntu
-[Even beginners can do it! ] How to create a Java environment on Windows 10 (JDK14.0.1)
-[Easy-to-understand explanation! ] How to use Java instance
-[Even beginners can do it! ] How to install Eclipse on Windows 10 (Java environment construction)
As prior knowledge, the contents of the above link are required.
--Inheritance
is to define a class with extensions and changes
based on the class already defined
.
--The class that is the source of inheritance is called " superclass
".
--The class newly defined by inheritance is called " subclass
".
--Define subclass
with" subclass name extends superclass name
".
--In subclass
, describe only the difference
from the superclass.
--Java allows only superclass
to be defined for one class due to language specifications.
--You can create a subclass
by furtherinheriting the
subclass. --All classes are [java.lang.Object class](https://docs.oracle.com/javase/jp/7/api/java/lang) defined in the
Java Standard Class Library (Java API). /Object.html) is inherited. --Classes with the
final modifier cannot
inherit. --Subclasses cannot
directly access the
private variableof the superclass. --In subclass instantiation, the
superclass constructor is processed and then the
subclass constructoris processed. --You can arbitrarily call the
superclass constructor by writing
super () at the beginning of the subclass constructor. --You can
redefine a method defined in a superclass in a subclass by using
override`.
--Override
means to redefine
a method defined in a superclass in a subclass.
--Override
is used when you want to perform different processing
with the same method name / argument as the superclass method.
--A basic description example of override
is as follows.
Super class
class superclass name{
//Superclass method
Method name(){
//processing
}
}
Subclass
class subclass name extends superclass name{
//override
@Override
Method name(){
//processing
}
}
Test class
package package name;
public class main class name{
public static void main(String[] args) {
//Instance generation
Subclass name variable name=new subclass name();
//Put a value in setter
Variable name.set instance variable name(Actual argument);
//Get the value entered by getter
System.out.println(Variable name.getインスタンスVariable name());
}
}
Super class
package package name;
class superclass name{
//Definition of instance variables
private type name variable name;
//Constructor (executed when instantiating)
Super class name(Type name argument){
Initialization process, etc.
}
// setter
void set instance variable name(Type name Argument name){
this.Variable name=Argument name;
}
// getter
Type name get instance variable name(){
return variable name;
}
}
Subclass
package package name;
class subclass name extends superclass name{
//Definition of instance variables
private type name variable name;
//Constructor (executed when instantiating)
Subclass name(Type name argument){
super(argument);
Initialization process, etc.
}
// setter
void set instance variable name(Type name Argument name){
this.Variable name=Argument name;
}
// getter
Type name get instance variable name(){
return variable name;
}
}
--Basic encapsulation is described as above.
[File (F)]-> [New (N)]-> [Java Project]
.
Test1
for the project name and click the Done
button.
[File (F)] → [New (N)] → [Class]
.
Test1
for the package and name and click the Done
button.
Test1.java
has been created.
Enter Test1
in the package, enter TestSuper
in the name, and click the Finish
button in the same way as in 6.3.
TestSuper.java
has been created.
Enter Test1
in the package and TestSub
in the name in the same procedure as in 8.3, and click the Finish
button.
Test1.java
, TestSuper.java
, and TestSub.java
are created.Test1.java
package Test1;
public class Test1 {
public static void main(String[] args) {
//Instance generation
TestSuper ts1 = new TestSuper("A");
TestSub ts2 = new TestSub("B","Gyoza");
//View instance
ts1.showName();
System.out.println();
ts2.showName();
}
}
TestSuper.java
package Test1;
public class TestSuper {
//Definition of instance variables
private String name;
//Constructor (executed when instantiating)
public TestSuper(String name) {
this.name = name;
}
// setter
public void setName(String name) {
this.name = name;
}
// getter
public String getName() {
return name;
}
public void showName() {
System.out.println("name:"+name);
}
}
TestSub.java
package Test1;
public class TestSub extends TestSuper {
//Definition of instance variables
private String food;
//Constructor (executed when instantiating)
public TestSub(String name,String food) {
super(name);
this.food = food;
}
// setter
public void setFood(String food) {
this.food = food;
}
// getter
public String getFood() {
return food;
}
@Override
public void showName() {
super.showName();
showFood();
}
public void showFood() {
System.out.println("Favorite food:"+food);
}
}
--Copy the above sentence, specify S-JIS
as the character code, save the file name as Test1.java
, TestSuper.java
, TestSuper.java
, and execute it. .. ↓ ↓
-[Useful to remember !!!] Easy creation of constructor and getter / setter in Eclipse -[Even beginners can do it! ] How to write Javadoc -[Easy-to-understand explanation! ] How to use Java encapsulation -[Easy-to-understand explanation! ] How to use Java overload
Recommended Posts