Introduction to Java for beginners 200 examples of 80 Java extensions

Keywords: Java

Introduction to the author

Author name: Ming Shiyin in programming world
Introduction: CSDN blog expert has been engaged in software development for many years and is proficient in Java and JavaScript. Bloggers also learn and grow step by step from scratch, know the importance of learning and accumulation, and like to fight and upgrade with ADC. Welcome to pay attention and look forward to learning, growing and taking off with you!

introduction

Many Java beginners ask me that it's worrying for the novice to turn around and forget the Java knowledge he has studied very hard. How can Xiaobai grow up quickly and become a big cow?
In fact, there is only one skill to become a big God: "learn more and practice more", so brother Ming sorted out typical practice examples. Through practice, you can quickly improve coding skills and proficiency, so that you can never return on the way to become a big man (remember to practice with your own hands)!

Navigation

✪ introduction to Java white 200 case series directory index
◄ previous article    79. Encapsulation of Java classes
► next article to be updated     

summary

Inheritance is one of the three characteristics of object-oriented.
Inheritance is that a subclass inherits the characteristics and behavior of the parent class, so that the subclass object (instance) has the instance domain and method of the parent class, or the subclass inherits the method from the parent class, so that the subclass has the same behavior as the parent class.

Inheritance format of class

Class parent class{
}

Class subclass extends parent class{
}

Why use inheritance

There are two classes, students and teachers. They have many things in common, such as name, age, gender, telephone and other attributes, as well as common methods such as eating and sleeping.
There are also some different things. For example, students have student numbers and teachers have teacher numbers (just write one)
Student class

package demo.demo80;

/*
 * Student class
 */
public class Student {
	//Property is private
	private String name; // full name
	private int age; // Age
	private String sex; // Gender
	private String phone; // contact number
	private String sNo; //Student number
	
	//Construction method
	public Student(String name,int age,String sex,String phone,String sNo){
		this.name=name;
		this.age=age;
		this.sex=sex;
		this.phone=phone;
		this.sNo=sNo;
	}
	//having dinner
	public void eat(){ 
        System.out.println(name+"be at  table"); 
    }
	//sleep
    public void sleep(){
        System.out.println(name+"sleeping");
    }
}

Teacher class

package demo.demo80;

/*
 * Teacher class
 */
public class Teacher {
	// Property is private
	private String name; // full name
	private int age; // Age
	private String sex; // Gender
	private String phone; // contact number
	private String tNo; //Teacher number

	// Construction method
	public Teacher(String name, int age, String sex, String phone,String tNo) {
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.phone = phone;
		this.tNo = tNo;
	}

	// having dinner
	public void eat() {
		System.out.println(name + "be at  table");
	}

	// sleep
	public void sleep() {
		System.out.println(name + "sleeping");
	}
}

It can be seen from these two pieces of code that the code is repeated, resulting in a large amount of code and bloated.

Design People class

Extract the same things of Student class and Teacher class into People

package demo.demo80;

/*
 * Parent class
 */
public class People {
	// Property is private
	private String name; // full name
	private int age; // Age
	private String sex; // Gender
	private String phone; // contact number

	// Construction method
	public People(String name, int age, String sex, String phone) {
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.phone = phone;
	}

	// having dinner
	public void eat() {
		System.out.println(name + "be at  table");
	}

	// sleep
	public void sleep() {
		System.out.println(name + "sleeping");
	}
}

Inherit to People class

Student class inheritance

package demo.demo80;

/*
 * Student class
 */
public class Student extends People{
	//Property is private
	private String sNo; //Student number
	
	//Construction method
	public Student(String name,int age,String sex,String phone,String sNo){
		super(name,age,sex,phone);
		this.sNo=sNo;
	}
}

Teacher class inheritance

package demo.demo80;

/*
 * Teacher class
 */
public class Teacher extends People{
	// Property is private
	private String tNo; //Teacher number
	
	//Construction method
	public Teacher(String name,int age,String sex,String phone,String tNo){
		super(name,age,sex,phone);
		this.tNo=tNo;
	}
}

Test it

package demo.demo80;

public class Test {

	public static void main(String[] args) {
		Student student = new  Student("Zhang San",19,"male","13812345678","001");
		student.eat();
		student.sleep();
	}
}

Operation results:

Zhang San is eating
Zhang San is sleeping

advantage

  1. Realize code sharing, reduce the workload of creating classes, and enable subclasses to have methods and properties of parent classes.
  2. Improve code maintainability and reusability.
  3. Improve the code scalability and better implement the method of parent class.

Summary

This section summarizes "Java extensions", hoping to be helpful to you. Please help [like] + [collection] + [punch in the comment area]. If you are interested in learning java with brother Xiaoming, [pay attention to a wave] won't get lost.

Let me know you by punching in the comment area. Mingge will continue to pay attention to your learning progress!

Navigation

✪ introduction to Java white 200 case series directory index
◄ previous article    79. Encapsulation of Java classes
► next article to be updated     

Popular column recommendation

1.Java game series (Tetris, aircraft war, plant war, zombie, etc.)
2.JavaWeb project practice (library management, online examination, dormitory management system, etc.)
3. Wonderful examples of JavaScript (aircraft war, minesweeping, snake eating, verification code, etc.)
4. Introduction to Java Xiaobai 200 cases
5. Learn Java from zero, learn Java with interest, and learn Java from the perspective of King glory

Posted by JVassie on Thu, 30 Sep 2021 18:24:41 -0700