Introduction to java - object oriented

Posted by PatriotXCountry on Sun, 02 Jan 2022 17:39:08 +0100

object-oriented

inherit

Usage scenario: if multiple similar classes have the same attributes and methods, you can extract the same attributes and methods from the parent class

Benefits: reduced code redundancy

Deep inheritance:

When creating a subclass object, will the parent class constructor be called?
meeting
If you create a subclass object, will you create a parent object?
can't
When you create a subclass object, why do you call the parent class constructor?
The purpose is to store the properties of the parent class in the child class object

To create a subclass object, first call the parent class constructor or subclass constructor?
Call the subclass constructor first

To create a subclass object, first complete the parent class construction method or subclass construction method?
Complete the parent class construction method first

Can a subclass inherit the properties and methods privatized by the parent class?

Yes, but only indirectly

super - parent class

Meaning: represents the parent class

Acting in subclasses:

  1. super. Property: call the non privatized member variable of the parent class
  2. super. Method: call the non privatized member method of the parent class
  3. super(): call the non privatized constructor of the parent class

rewrite

Meaning: rewriting is also called replication, which rewrites the methods in the parent class in the child class

Application scenario: when the parent method does not meet the needs of the child, the child can repeat the parent method

Conditions:

  1. Override the method of the parent class in a subclass
  2. The return value, method name and parameter list must be consistent with the method overridden by the parent class
  3. The access modifier cannot be more restrictive than the method overridden by the parent class
package Test02;

public class Test {
	public static void main(String[] args) {
		Chinese c = new Chinese();
		c.setName("Zhang Sanfeng");
		c.setChinaId("123456789");//China ID Card
		c.eat();

		Japanese j = new Japanese();
		j.setName("Asso");
		j.setYearNumber("Reiwa");//Japanese year
		j.eat();
	}
}

Zhang Sanfeng ate Manchu and Han banquet
 Asso eats sashimi
package Test02;

public class Chinese extends Preson {//Chinese, inherit human attributes
	private String ChinaId;//Add a property that the parent class does not have
	public String getChinaId() {
		return ChinaId;
	}
	public void setChinaId(String chinaId) {
		ChinaId = chinaId;
	}
	public void eat() {
		System.out.println(super.getName()+"Eat man Han banquet");//rewrite; super calls the getName of the parent class
	}
}

package Test02;

public class Japanese extends Preson {//Japanese
	private String  yearNumber;//No parent class added
	public String getYearNumber() {
		return yearNumber;
	}
	public void setYearNumber(String yearNumber) {
		this.yearNumber = yearNumber;
	}
	public void eat() {
		System.out.println(super.getName()+"Eat sashimi");//rewrite; super calls the getN of the parent class
	}
}
package Test02;

public class Preson {//human beings
	private String name;
	public Preson() {}
	public Preson(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void eat() {
		System.out.println(this.name+"Have a meal");
	}
}

Access modifier

Meaning: modifies classes, methods and attributes, and defines the scope of use

Learning: doing experiments

Access modifier This categoryThis packageOther steamed stuffed bun subclassesOther packages
privateOK
defaultOKOK
protectedOKOKOK
publicOkOKOKOk

Object

Meaning: a base class is also called a superclass. Object is the ancestor of all classes

Note: if a class has no explicitly inherited parent class, it inherits Object by default

equals: compares whether the memory addresses of two objects are the same

hashCode: get the hash value of the object

getClass: gets the bytecode file object of the class

toString: gets the string representation of the object

package com.Xu.Test;

public class Test01 {
	@SuppressWarnings("unlikely-arg-type")
	public static void main(String[] args) {
		Animal animal = new Animal();
		Person person = new Person();
		//Gets the string representation of the object
		System.out.println(animal.toString());
		//Compare whether the memory addresses of two objects are the same
		System.out.println(person.equals(animal));
		//Gets the hash value of the object
		System.out.println(person.hashCode());
		//Gets the bytecode file object of the class
		System.out.println(person.getClass());
	}
}

package com.Xu.Test;

public class Animal {
	private String name;
	private String varieties;//varieties
	private int age;//Age
	public Animal() {
		// TODO Auto-generated constructor stub
	}
	public Animal(String name, String varieties, int age) {
		super();
		this.name = name;
		this.varieties = varieties;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getVarieties() {
		return varieties;
	}
	public void setVarieties(String varieties) {
		this.varieties = varieties;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public void eat() {
		System.out.println(name+"Eat something ");
	}
	public void sleep() {
		System.out.println(name+"sleep");
	}
}
package com.Xu.Test;

public class Person {
	private String name;
	private char sex;//Gender
	private int age;//Age
	public Person() {
		// TODO Auto-generated constructor stub
	}
	public Person(String name, char sex, int age) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public char getSex() {
		return sex;
	}
	public void setSex(char sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public void eat() {
		System.out.println(name+"Eat something ");
	}
	public void sleep() {
		System.out.println(name+"sleep");
	}
}

Benchmarking: as the parent class of all classes, Object defines several methods to facilitate child classes to override

Benchmark function of equals: compare whether two objects are the same, and the comparison rules of different objects are different, so subclass can be overridden

Benchmarking function of toString: each subclass has different properties. Rewriting toString directly prints all properties in the object to facilitate data observation

final

Meaning: Final

effect:

  1. Decorated class: this class cannot be inherited

  2. Modifier method: this method cannot be overridden

  3. Modified variable: becomes a constant and cannot be re assigned

    Naming rules of constants: all uppercase, words separated by underscores

    Declaration cycle of constant: it exists in the constant pool and will not be destroyed until the end of the project

Abstract classes and abstract methods

//abstract class
public abstract class Class name{
    //Abstract method
    public abstract void method();
}

Abstract methods are implemented (overridden) by non Abstract subclasses

Application scenario: when a method must appear in the parent class, but the method is difficult to implement, turn the method into an abstract method and hand it over to a non Abstract subclass for implementation

package com.Xu.work;

public abstract class Person {//Define an abstract class and modify it with abstract
	String name;
	public abstract void eat();//Abstract method, no method body, decorated with abstract keyword
	public abstract void sleep();
}
package com.Xu.work;

public class A extends Person {
	@Override
	public void eat() {//Abstract methods must be overridden
		System.out.println(super.name+"Eat in a basin");
	}
	@Override
	public void sleep() {
		System.out.println(super.name+"Sleep standing");
	}

}

package com.Xu.work;

public class Test {
	public static void main(String[] args) {
		A a = new A();
		a.name="Xiao Ming";
		a.sleep();
		a.eat();
	}
}
Xiao Ming sleeps standing
 Xiao Ming eats in a basin

Interface

Meaning: special abstract class

be careful:

  1. JDK1.7, the interface can only have static constants and abstract methods
  2. JDK1. Starting from 8, there are static constants, abstract methods and default methods in the interface

Application scenario: the interface is more like a specification

Abstract class vs interface

Abstract classes: member variables, static variables, static constants, member methods, and static methods

Interface: static constant, static method, default method (JDK1.8)

package com.Xu.Test02;

public interface A {
	final int a=1;
	public abstract void eat();
}
package com.Xu.Test02;

public interface B {
	final int b=2;
	public abstract void sleep();
}
package com.Xu.Test02;

public class AB implements A,B{
	//All abstract methods of the interface must be overridden 
	@Override
	public void sleep() {
		System.out.println("Sleep sleep");
	}
	@Override
	public void eat() {
		System.out.println("Have a meal");
	}
}
package com.Xu.Test02;

public class Test {
	public static void main(String[] args) {
		AB ab = new AB();
		ab.eat();
		ab.sleep();
		System.out.println(ab.a);
		System.out.println(ab.b);
	}
}
Have a meal
 Sleep sleep
1
2

Class interface relationship:
Class - class: single inheritance
Class interface: multiple implementations
Interfaces - Interfaces: multi inheritance

It's not easy to create. Update high-quality content every day. Pay attention to it when you pass by...

Topics: Programming OOP interface abstract class inheritance