Java encapsulation, inheritance and polymorphism

Posted by sunil.23413 on Thu, 17 Feb 2022 10:42:21 +0100



//student class
public class Student {
	//Property private get/set
	private String name;
	private int age;
	private char sex;	
	//Provide some public get and set methods	
	//get this data
	public String getName() {
	//set assigns a value to this property
	public void setName(String name) { = name;
	public int getAge() {
		return age;
	public void setAge(int age) {
		if(age<120 && age>0) {
			this.age = age;
		}else {
			System.out.println("The age entered is illegal!");
	public char getSex() {
		return sex;
	public void setSex(char sex) { = sex;

//Implementation class
 1, Improve program security and protect data
 2,Hidden code implementation details
 3,Unified interface
 4,System maintainability increased 
//There should be only one main method in a project
public class Application {
	public static void main(String[] args) { 
		Student stu = new Student();		


1. All classes inherit the object class directly or indirectly by default.
2. There is only single inheritance in Java, not multiple inheritance.
A subclass can inherit only one parent class, but a parent class can be inherited by multiple subclasses.
3. Extensions means "extension", and subclasses are extensions of parent classes.

1.super calls the constructor of the parent class, which must be in the first line of the constructor.
2.super must only appear in subclass methods or construction methods!
3.super and this cannot call construction methods at the same time.

The difference between super and this:
1. Different representative objects:
this: the object of the caller itself
super: represents the parent class object reference
2. Premise:
this: it can be used without inheritance
super: can only be used under inheritance conditions
3. Construction method:
this(): the construction of this class!
super(): Construction of parent class!

Inheritance relationship is required, and the subclass overrides the parent method.
1. The method name must be the same
2. The parameter list must be the same
3. Modifier: the scope can be expanded but not reduced:
4. Throw an exception. The range can be reduced, but not expanded:
Classnotfoundexception -- > exception (large)
5. When overridden, the method of the subclass must be consistent with that of the parent class: the method body is different!
6. Why rewrite: the function of the parent class and the subclass are not necessarily required or satisfied!


//Application class 
import com.oop.demo05.A;
import com.oop.demo05.B;
public class Application {
	//Static methods differ greatly from static methods
	//Static method: the method call is only related to the data type defined on the left
	public static void main(String[] args) { 
		A a = new A();
		//A reference to a parent class points to a child class
		B b = new A();//The subclass overrides the method of the parent class
 //Subclass A
 public class A extends B{
	public static void test() {
//Parent class
 public class B {
	public static void test() {


matters needing attention:
1. Polymorphism is the polymorphism of methods, and there is no polymorphism of attributes.
2. The parent class is related to the child class, and the type conversion exception is ClassCastException!.
3. Existence conditions: inheritance relationship, method needs to be rewritten, and the reference of the parent class points to the subclass object!
4.instanceof (type conversion) determines what type an object is
Methods that cannot be overridden:
1.static belongs to class, not method constant;
3.private method, private method, can't be rewritten!

instanceof example:

public class Application {
	public static void main(String[] args) { 
		Object object = new Student();		
		System.out.println(object instanceof Student);//true
		System.out.println(object instanceof Person);//true
		System.out.println(object instanceof Object);//true
		System.out.println(object instanceof Teacher);//false
		System.out.println(object instanceof String);//false
		Person person = new Student();
		System.out.println(person instanceof Student);//true
		System.out.println(person instanceof Person);//true
		System.out.println(person instanceof Object);//true
		System.out.println(person instanceof Teacher);//false
		//System.out.println(person instanceof String);// Compilation error!
		Student student = new Student();
		System.out.println(student instanceof Student);//true
		System.out.println(student instanceof Person);//true
		System.out.println(student instanceof Object);//true
		//System.out.println(student instanceof Teacher);// Compilation error!
		//System.out.println(student instanceof String);// Compile error!
		//System.out.println(x instanceof y);// Can you compile it to see if there is a parent-child relationship		

Type conversion:
1. The parent class reference points to the child class object.
2. The subclass is converted to the parent class and converted upward.
3. Conversion from parent class to child class, downward conversion and forced conversion.
4. Facilitate method calls and reduce duplicate code.

static keyword:

public class Person {
	//Second execution
		System.out.println("Anonymous code block");
	//The first execution, no matter how many times main executes, it only executes once at the first time
	static {
		System.out.println("Static code block");
	//Third execution
	public Person() {
		System.out.println("Construction method");
	public static void main(String[] args) {
		Person person = new Person();
		Person person2 = new Person();

Static import package:

//Static import package
import  static java.lang.Math.random;
import  static java.lang.Math.PI;
public class Test {
	public static void main(String[] args) {

abstract class
1. new is not an abstract class, but can only be implemented by subclasses: constraints
2. Ordinary methods can be written in abstract classes
3. Abstract methods must be in abstract classes

//Single class interfaces can inherit from multiple classes
public abstract class Action {
	//Abstract methods have only method names and no method bodies
	public abstract void doSomething();	

1. Constraints
2. Define some methods for different people to realize
3. Method public abstract
4. Constant public static final
5. The interface cannot be instantiated. There is no construction method in the interface
6.implements can implement multiple interfaces
7. When implementing the interface, you must rewrite the methods in the interface

//Interface UserService 
//The defined keyword interface and interface need to have implementation classes
public interface UserService {
	//All definitions in the interface are abstract public
    void add(); 

//Interface TimeService 
public interface TimeService {}

//Implementation class UserServiceImpl 
//Class can implement the interface through implementation
//If you implement the classes in the interface, you need to rewrite the methods in the interface
//Using interfaces to realize multi inheritance,
public class UserServiceImpl implements UserService,TimeService{
	public void add() {

Inner class
1. A java class can have multiple class classes, but only one public class

//Class Outer defines the Inner class
public class Outer {
	private int id=10;
	public void out() {
		System.out.println("This is the method of an external class");
	public class Inner{
		public void in() {
			System.out.println("This is the method of the outer inner class");
		//Get the private properties of the external class
		public void getID() {

//Application class
import com.oop.demo10.Outer;
public class Application {
	public static void main(String[] args) { 
		Outer outer = new Outer();
		//Instantiate the inner class through the outer class
		Outer.Inner inner = Inner();

Local internal class:

public class Outer {
	public void method() {
		//Local inner class
		class Inner{
			public void in() {				

There can only be one public class

public class Outer {	
//There can be multiple class classes in a java class, but there can only be one public class
class A{	

Topics: Java Back-end