Common Java keywords and their usage

  1. extends
  2. abstract
  3. interface
  4. implement

1.extends keyword

Inheritance: an existing class derives a new class, and a subclass inherits the properties and behaviors of the parent class, and can be extended

Class subclass extends parent class {}
package com.day3;

/**
 * @author SFJ
 * @date 2019/11/9
 * @time 22:15
 **/
public class Test1 {
    public static void main(String[] args) {
        Student student = new Student(); // Subclasses are instantiated
        student.setName("Sang Feng Jiao"); //        Method of Person class (parent class method)
        student.setAge(21); //               Person class definition
        System.out.println("Full name:" + student.getName() + ",Age:" + student.getAge());
        student.setSchool("liaocheng university");
        System.out.println(student.getSchool());
    }
}
class Person {
    private String name;
    private int age;
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return this.name;
    }
    public int getAge() {
        return this.age;
    }
}
class Student extends Person { // The Student class inherits the Person class
    private String school; // Properties of subclasses
    public void setSchool(String school) {
        this.school = school;
    }
    public String getSchool() {
        return this.school;
    }
}

The subclass inherits the parent class and makes the definition of the parent class more specific, that is, the scope of the parent class representation is large and the scope of the subclass representation is small

Restrictions when using inheritance:

1. Single inheritance: a subclass can only inherit one parent class, but a parent class can be inherited by multiple subclasses

1.1 error: (cannot inherit multiple)

class A {}
class B {}
class C extends A,B {}          // A subclass inherits two parents

1.2 correct: (C is actually a (grandchild) subclass, which implements multi-level inheritance)

class A {}
class B extends A{}            // Class B inherits parent class A
class C extends B {}          //  Subclass C inherits class B

2. The private property cannot be accessed directly: the parent class private property and the child class cannot be accessed directly, but it is found that they can operate indirectly through setter and getter methods.

package com.day3;

/**
 * @author SFJ
 * @date 2019/11/9
 * @time 22:38
 **/
public class Test2 {
    public static void main(String[] args) {
        B b = new B();
        b.setM("sangfnegjiao");
        System.out.println(b.getM());
    }
}
class A {
    private String m;
    public void setM(String msg) {
        this.m = msg;
    }
    public String getM() {
        return this.m;
    }
}
class B extends A {
    public void print() {
        System.out.println(m); // Error: m defined as private, not visible
    }
}

3. Transitivity: in the inheritance relationship, when instantiating a subclass object, the parent class structure will be called by default to initialize the properties in the parent class, and then the subclass structure will be called to initialize the properties in the subclass, that is, by default, the subclass will find the nonparametric construction method in the parent class.

package com.day3;

/**
 * @author SFJ
 * @date 2019/11/9
 * @time 22:44
 **/
public class Test3 {
    public static void main(String[] args) {
        B1 b1 = new B1();
    }
}
class A1 {
    public A1() {         // Parent nonparametric construction
        System.out.println("Construction method of parent class without parameters") ;
    }
}
class B1 extends A1 {
    public B1() {         // Subclass construction
        System.out.println("Subclass construction method");
    }
}

The default is to call parameterless construction. If the parent class does not have parameterless construction at this time, the child class must call the construction method of the specified parameter through super():

package com.day3;

/**
 * @author SFJ
 * @date 2019/11/9
 * @time 22:44
 **/
public class Test3 {
    public static void main(String[] args) {
        B1 b1 = new B1();
    }
}
class A1 {
    public A1(String s) {         // Parent nonparametric construction
        System.out.println("Method of constructing parent class with parameters"+s) ;
    }
}
class B1 extends A1 {
    public B1() {
        super("hello");// When super calls the parent class construction, it must be placed on the first line of the construction method
        System.out.println("Subclass construction method");
    }
}

 

2.abstract keyword (abstract method, abstract class)

  1. Abstract class: a class containing abstract methods is an abstract class. The specification is defined by abstract, and then the concrete implementation must be defined to find the subclass. The design of subclass is strictly restricted by abstract class, so that the subclass is more general.

  2. Abstract method: Method decorated with abstract, no method body, only declaration. It defines a "specification", which tells the subclass to provide concrete implementation for abstract methods

matters needing attention:

1. A class in an abstract method can only be defined as an abstract class.
2. Abstract classes cannot be instantiated, that is, objects cannot be instantiated with new
3. Abstract classes can contain properties, methods, and construction methods, but construction methods cannot be new instances, and can only be called by subclasses.
4. Abstract classes can only be used to be inherited
5. Abstract methods must be implemented by subclasses.    

abstract class Bird {// Abstract class: a class with abstract methods must be an abstract class
	
	public abstract void play();// Don't use {} method, just write the method's declaration abstract method function: make a simple rule
	public void run() {// Common method
		System.out.println("run");
	}
}
 abstract class BadBird extends Bird {// You can change a subclass into an abstract class, but if you want to use the current class, you must create another subclass, because an abstract class cannot directly create objects
	public void piay() {
		// Rewriting method, the realization of writing abstract method
	}
}

3. interface&& 4.implements

Interface is a collection of definitions of abstract methods and constant values. It is a special abstract class, which contains only the definitions of constants and methods, but no implementation of variables and methods.

public interface Study{ 
   int id = 1
   public void start()
   public void run()
   public void stop()    
}

An abstract class can inherit an abstract class, but an interface can use extends to inherit multiple interfaces at the same time (but an interface cannot inherit an abstract class, because if it inherits an abstract class, it must implement its abstract methods, and only abstract methods in the interface):

package com.day3;

/**
 * @author SFJ
 * @date 2019/11/9
 * @time 23:20
 **/
public class Test4 {
    public static void main(String[] args) {
        C4 c = new C4();
        c.print();	//Calling methods implemented by subclasses
        c.info();
    }
}
interface B4 {
    public void info();//Define abstract methods
}
interface A4{
    public void print();
}
class C implements A4 {

    @Override
    public void print() {
        System.out.println("Implementation interface A4 Abstract method of");
    }
}
 class C4 implements A4,B4{
    @Override
    public void print() {
        System.out.println("Implementation interface A4 Abstract method of");
    }
    @Override
    public void info() {
        System.out.println("Implementation interface B4 Abstract method of");
    }
}

Tags: Programming

Posted on Sat, 09 Nov 2019 12:49:20 -0800 by azwebdiva