Java Learning notes
1. Inheritance
Key words:
Extends
in Java , the inheritance of a class is a single inheritance, that is, a subclass can only have one parent class, so extends can inherit only one class .
eg
public class Penguin extends Animal
{
}
Implements
The use of the Implements keyword can be disguised to make java has multiple inheritance, using the scope of the class to inherit the interface, you can inherit multiple interfaces (interfaces and interfaces separated by commas).
Eg:
Public interface A {
public void eat ();
public void sleep ();
}
Public interface B {
public void Show ();
}
public class C implements A, B {
}
2.
Super and this keyword
Super keyword : We can use the Super keyword to implement access to a parent class member to refer to the parent class of the current object.
this keyword : point to your own reference.
3.
Final keyword
The final keyword declares that a class can define a class as not inheritable, that is, a final class, or for a decorated method that cannot be overridden by a quilt class:
Declaring a class:
Final class class Name {//class body}
Declaration method:
modifier (public/private/default/protected) Final return value type method name () {//Method body}
4.
Constructors
A subclass cannot inherit the constructor (constructor or constructor) of the parent class, but the constructor of the parent class has parameters, and the constructor of the parent class must be explicitly called through the Super keyword in the constructor of the child class with the appropriate argument list.
if the parent class has a parameterless constructor, it is not necessary to call the parent class constructor with super in the constructor of the subclass, and if the Super keyword is not used , the system automatically calls the parent class's parameterless constructor.
Example
Class Superclass {
private int n;
Superclass () {
System.out.println ("Superclass ()");
}
Superclass (int n) {
System.out.println ("superclass (int n)");
THIS.N = n;
}
}
Class Subclass extends Superclass{
private int n;
Subclass () {
Super (300);
System.out.println ("subclass");
}
public subclass (int n) {
System.out.println ("Subclass (int N):" +n);
THIS.N = n;
}
}
public class testsupersub{
public static void Main (String args[]) {
Subclass SC = new subclass ();
Subclass SC2 = new Subclass (200);
}
}
The output is:
Superclass (int n)
Subclass
Superclass ()
Subclass (int n): 200
modifier
default ( that is, by default, nothing is written): It is visible within the same package, and no modifiers are used. Use objects: classes, interfaces, variables, methods.
Private : visible within the same class. Working with objects: variables, methods. Note: Classes cannot be decorated (external classes)
Public: visible to all classes. Working with objects: classes, interfaces, variables, methods
protected : visible to classes and all subclasses within the same package. Working with objects: variables, methods. Note: Classes (external classes) cannot be decorated.
1. A variable declared as a private access type can only be accessed by an external class through a public getter method in the class. The use of the 2.Private access modifier is primarily used to hide the implementation details of the class and to protect the class's data.
3. if several of the public classes that are accessed are distributed in different packages, you need to import the package that contains the appropriate public class. Because of the inheritance of classes, all public methods and variables of a class can be inherited by their subclasses.
4. variables, methods, and constructors declared as protected can be accessed by any other class in the same package, or by subclasses in different packages.
The protected access modifier cannot decorate classes and interfaces, and methods and member variables can be declared as protected, but the member variables and member methods of an interface cannot be declared as protected .
subclasses can access the methods and variables declared by the protected modifier, which protects unrelated classes from using these methods and variables.
Methods declared as public in the parent class must also be public in the subclass.
A method declared as protected in a parent class is either declared as protectedin a subclass,or declared as public, and cannot be declared as Private .
The method declared as private in the parent class cannot be inherited.
non-access modifiers
in order to implement some other functions, Java also provides a number of non-access modifiers.
static modifier that are used to modify class methods and class variables.
final modifier, to modify classes, methods, and variables,the final decorated class cannot be inherited, the decorated method cannot be redefined by the inheriting class, the modified variable is constant, and is not modifiable.
abstract modifier , used to create abstract classes and abstract methods.
synchronized and volatile modifiers , which is used primarily for threading programming.
Summary of Java Inheritance (II.)