I thought that the material I made for the study session was "well, I was able to make a decent amount for myself," so I posted this for the first time. However, it would be very embarrassing if I wrote the wrong content with this.
In object-oriented programming, a unit of 1 with behavior and attribute is called object, and this object type is called class. To break the story a little more,
Is.
The standard syntax of the class is as follows.
class sandbox {
public int field;
public void method() {
// Write anything process.
}
}
__ Creating a class object __. Also known as " new
". Instantiation is performed as follows. By instantiating, you can use the members and methods of the instantiated class.
class sandbox {
public static void main(String[] args) {
Class cls = new Class(); // Create instance.
cls.method(); // You can action method() of Class.
}
}
class Class {
public int field;
public void method() {
// Write anything process.
}
}
A constructor is a method that is executed only once when it is instantiated. When creating the constructor, the method name should be the same as the class name.
class sandbox {
public static void main(String[] args) {
Class cls = new Class();
// Create instance and action constructor.
}
}
class Class {
public int field;
public Class() {
System.out.println("Action constructor.");
}
public void method() {
// Write anything process.
}
}
Execution result
Action constructor.
Access from the outside can be restricted by adding an access modifier.
Access modifier | Access range |
---|---|
public |
all |
protected |
In the same package, own class, subclass |
None | In the same package / own class |
private |
Own class |
A simple access example is as follows.
class sandbox {
public static void main(String[] args) {
Class cls = new Class(); // Create instance.
System.out.println("You can access " + cls.public_fld + " field.");
System.out.println("You can access " + cls.protected_fld + " field.");
System.out.println("You can access " + cls.nothing_fld + " filed.");
// Compile err.
// System.out.println("You can " + cls.private_fld + " field.");
}
}
class Class {
public String public_fld = "public";
protected String protected_fld = "protected";
String nothing_fld = "nothing";
private String private_fld = "private";
}
Execution result
You can access public field.
You can access protected field.
You can access nothing field.
When I try to uncomment and compile the last System.out.println ("You can "+ cls.private_mbr +" member ");
, I get a compile error due to access restrictions.
static
modifierThe static
qualifier allows it to be expanded in memory at runtime and accessed without instantiation.
class sandbox {
public static void main(String[] args) {
System.out.println("You can access " + Class.static_fld + " field.");
}
}
class Class {
static String static_fld = "static";
}
Execution result
You can access static field.
final
modifierBy using the final
modifier, it becomes a constant and its value cannot be changed.
class sandbox {
public static void main(String[] args) {
System.out.println("You can access " + Class.static_fld + "member.");
// Compile err.
// Class.static_fld = "final";
}
}
class Class {
static final String static_fld = "static";
}
Put fields and methods together and add private
to the members you want to protect so they cannot be accessed. Encapsulation is basic,
getter
setter
Is often used. These two methods are called __accessor method __.
class sandbox {
public static void main(String[] args) {
Class cls = new Class();
cls.setField("hoge");
System.out.println(cls.getField());
}
}
class Class {
private String field = "";
/**
* Accessor method.
* setField(String param) -> Setter
* getField() -> Getter
**/
void setField(String param) {
field = param;
}
String getField() {
return field;
}
}
Execution result
hoge
However, there is a story that it is meaningless to make getter
and setter
and access the field with private
, so pure getter
and setter
realize encapsulation. Otherwise, I wrote in this article.
By the way, encapsulation also means that you can achieve the purpose without worrying about data and processing (please tell me in the comment, article _reference-867e1b32cdfedfcc02e2) also did it.).
To give a class a __parent-child relationship __. A child class that inherits the parent class will inherit the functionality of the __parent class __.
class sandbox {
public static void main(String[] args) {
Child cld = new Child();
cld.sayCommentByPerson();
cld.sayCommentByChild();
}
}
// Super class.
class Person {
void sayCommentByPerson() {
System.out.println("I am Person.");
}
}
// Sub class.
class Child extends Person {
void sayCommentByChild() {
System.out.println("I am Child.");
}
}
Execution result
I am Person.
I am Child.
If your parent has a constructor and you want to call it, you can do super ();
.
class sandbox {
public static void main(String[] args) {
Child cld = new Child();
cld.sayCommentByChild();
}
}
// Super class.
class Person {
Person() {
System.out.println("I am Person.");
}
}
// Sub class.
class Child extends Person {
//Call constructor of super class.
Child() {
super();
}
void sayCommentByChild() {
System.out.println("I am Child.");
}
}
Execution result
I am Person.
I am Child.
Overload is __ to generate multiple methods with the same name with different arguments __.
class sandbox {
public static void main(String[] args) {
Class strClass = new Class("hoge");
Class intClass = new Class(1);
}
}
class Class {
Class(String param) {
System.out.println("String: " + param);
}
Class(int param) {
System.out.println("Integer: " + param);
}
}
Execution result
String: hoge
Integer: 1
Override is to overwrite the method inherited from the parent class with the child class.
class sandbox {
public static void main(String[] args) {
Child cld = new Child();
cld.sayComment();
}
}
// Super class.
class Person {
void sayComment() {
System.out.println("I am Person.");
}
}
// Sub class.
class Child extends Person {
@Override
void sayComment() {
System.out.println("I am Child.");
}
}
Being able to change the behavior depending on the application by overloading, overriding, and interface (described later).
__ A class that considers only the purpose of the parent class __. Therefore, it cannot be instantiated directly.
class sandbox {
public static void main(String[] args) {
// Compile err.
// Person prson = new Person();
Child chld = new Child();
chld.sayCommentByPerson();
chld.sayCommentByChild();
}
}
abstract class Person {
void sayCommentByPerson() {
System.out.println("I am Person.");
}
}
class Child extends Person {
void sayCommentByChild() {
System.out.println("I am Child.");
}
}
Execution result
I am Person.
I am Child.
__ A further abstraction of the abstract class __. Methods can be declared in the interface, but processing is not written in principle. I get a compile error when I try to write __ processing normally.
class sandbox {
public static void main(String[] args) {
Class cls = new Class();
cls.sayComment();
}
}
class Class implements Interface {
public void sayComment() {
System.out.println("I am Class.");
}
}
interface Interface {
void sayComment();
}
Also, the methods in ʻinterface are treated as
public, so when using
sayComment () of ʻinterface
in Class
, you must add public
. Also, the members in ʻinterface are treated as
public static final`.
Add the default
qualifier.
class sandbox {
public static void main(String[] args) {
Class cls = new Class();
cls.sayComment();
}
}
class Class implements Interface {
}
interface Interface {
default void sayComment() {
System.out.println("I am Interface.");
}
}
Execution result
I am Interface.
class sandbox {
public static void main(String[] args) {
Class cls = new Class();
cls.sayComment1();
cls.sayComment2();
}
}
class Class implements Interface1, Interface2 {
}
interface Interface1 {
default void sayComment1() {
System.out.println("I am Interface1.");
}
}
interface Interface2 {
default void sayComment2() {
System.out.println("I am Interface2.");
}
}
Execution result
I am Interface1.
I am Interface2.
Recommended Posts