search
HomeJavajavaTutorialJava: Classes and Inheritance
Java: Classes and InheritanceFeb 08, 2017 am 11:05 AM
javainherit

For object-oriented programming languages, classes are undoubtedly the most important foundation. The four major features of abstraction, encapsulation, inheritance, and polymorphism are inseparable from classes. Only the existence of classes can reflect the characteristics of object-oriented programming. Today we will learn some knowledge about classes and inheritance. First, let's talk about things related to the initialization of classes, and then we will explain the great feature of inheritance from several aspects. The following is the table of contents outline of this article:

1. Do you understand classes?

2. Do you understand inheritance?

3. Common written interview questions

1. Do you understand categories?

In Java, a class file is a code file with the suffix .java. Only one public class is allowed to appear in each class file. When there is a public class, the name of the class file must be the same as public. The names of the classes are the same. If there is no public, the name of the class file can be any name (of course, names starting with numbers are not allowed).

Within the class, for member variables, if there is no explicit assignment initialization when defining, Java will ensure that each member variable of the class is properly initialized:

 1) For variables of basic data types such as char, short, byte, int, long, float, double, etc., they will be initialized to 0 by default (boolean variables will be initialized to false by default);

 2) For reference types Variables will be initialized to null by default.

If the constructor is not explicitly defined, the compiler will automatically create a no-argument constructor, but remember that if the constructor is explicitly defined, the compiler will not automatically add the constructor. Note that all constructors are static by default.

Next we focus on the initialization sequence:

When the program is executed, an object of a certain class needs to be generated. The Java execution engine will first check whether the class is loaded. If not, then The class is loaded first and then the object is generated. If it has been loaded, the object is generated directly.

During the loading process of the class, the static member variables of the class will be initialized. In addition, if there is a static statement block in the class, the static statement block will be executed. The execution order of static member variables and static statement blocks is consistent with the order in the code. Remember, in Java, classes are loaded on demand. This class will only be loaded when it is needed, and only once. Look at the following example to understand:

public class Test {
    public static void main(String[] args) throws ClassNotFoundException {
         
        Bread bread1 = new Bread();
        Bread bread2 = new Bread();
    }
}
 
 
class Bread {
    static{
        System.out.println("Bread is loaded");
    }
    public Bread() {
        System.out.println("bread");
    }
}

When you run this code, you will find that "Bread is loaded" will only be printed once.

During the process of generating an object, the member variables of the object will be initialized first, and then the constructor will be executed. This means that the variables in the class will be initialized before any method (including the constructor) is called, even if the variables are interspersed between method definitions.

public class Test {
    public static void main(String[] args)  {
        new Meal();
    }
}
 
 
class Meal {
     
    public Meal() {
        System.out.println("meal");
    }
     
    Bread bread = new Bread();
}
 
class Bread {
     
    public Bread() {
        System.out.println("bread");
    }
}

The output result is:

bread
meal

2. Do you understand inheritance?

Inheritance is an indispensable part of all OOP languages. The extends keyword is used in Java to express the inheritance relationship. When a class is created, it is always inherited. If the class to be inherited is not explicitly stated, it is always implicitly inherited from the root class Object. For example, the following code:

class Person {
    public Person() {
         
    }
}
 
class Man extends Person {
    public Man() {
         
    }
}

The class Man inherits from the Person class. In this case, the Person class is called the parent class (base class), and the Man class is called the subclass (derived class). If there is an inheritance relationship between two classes, the subclass will automatically inherit the methods and variables of the parent class, and the methods and variables of the parent class can be called in the subclass. In Java, only single inheritance is allowed, which means that a class can only explicitly inherit from at most one parent class. But a class can be inherited by multiple classes, which means that a class can have multiple subclasses.

 1. The subclass inherits the member variables of the parent class

When the subclass inherits a certain class, it can use the member variables in the parent class, but it does not completely inherit the parent class. All member variables. The specific principles are as follows:

1) The public and protected member variables of the parent class can be inherited; the private member variables of the parent class cannot be inherited;

2) The package access permission members of the parent class Variables, if the subclass and the parent class are in the same package, the subclass can inherit; otherwise, the subclass cannot inherit;

 3) For the parent class member variables that the subclass can inherit, if they are in the subclass If a member variable with the same name appears in the class, the hidden phenomenon will occur, that is, the member variable of the subclass will block the member variable of the parent class with the same name. If you want to access a member variable with the same name in the parent class in a subclass, you need to use the super keyword for reference.

 2. The subclass inherits the methods of the parent class

Similarly, the subclass does not completely inherit all the methods of the parent class.

1) Can inherit the public and protected member methods of the parent class; cannot inherit the private member methods of the parent class;

2) For the package access member methods of the parent class, if the subclass If it is in the same package as the parent class, the subclass can inherit; otherwise, the subclass cannot inherit;

  3)对于子类可以继承的父类成员方法,如果在子类中出现了同名称的成员方法,则称为覆盖,即子类的成员方法会覆盖掉父类的同名成员方法。如果要在子类中访问父类中同名成员方法,需要使用super关键字来进行引用。

  注意:隐藏和覆盖是不同的。隐藏是针对成员变量和静态方法的,而覆盖是针对普通方法的。(后面会讲到)

  3.构造器

  子类是不能够继承父类的构造器,但是要注意的是,如果父类的构造器都是带有参数的,则必须在子类的构造器中显示地通过super关键字调用父类的构造器并配以适当的参数列表。如果父类有无参构造器,则在子类的构造器中用super关键字调用父类构造器不是必须的,如果没有使用super关键字,系统会自动调用父类的无参构造器。看下面这个例子就清楚了:

class Shape {
     
    protected String name;
     
    public Shape(){
        name = "shape";
    }
     
    public Shape(String name) {
        this.name = name;
    }
}
 
class Circle extends Shape {
     
    private double radius;
     
    public Circle() {
        radius = 0;
    }
     
    public Circle(double radius) {
        this.radius = radius;
    }
     
    public Circle(double radius,String name) {
        this.radius = radius;
        this.name = name;
    }
}

  这样的代码是没有问题的,如果把父类的无参构造器去掉,则下面的代码必然会出错:

Java: Classes and Inheritance

  改成下面这样就行了:

Java: Classes and Inheritance

  4.super

  super主要有两种用法:

  1)super.成员变量/super.成员方法;

  2)super(parameter1,parameter2....)

  第一种用法主要用来在子类中调用父类的同名成员变量或者方法;第二种主要用在子类的构造器中显示地调用父类的构造器,要注意的是,如果是用在子类构造器中,则必须是子类构造器的第一个语句。

三.常见的面试笔试题

1.下面这段代码的输出结果是什么?

public class Test {
    public static void main(String[] args)  {
        new Circle();
    }
}
 
class Draw {
     
    public Draw(String type) {
        System.out.println(type+" draw constructor");
    }
}
 
class Shape {
    private Draw draw = new Draw("shape");
     
    public Shape(){
        System.out.println("shape constructor");
    }
}
 
class Circle extends Shape {
    private Draw draw = new Draw("circle");
    public Circle() {
        System.out.println("circle constructor");
    }
}

shape draw constructor
shape constructor
circle draw constructor
circle constructor

  这道题目主要考察的是类继承时构造器的调用顺序和初始化顺序。要记住一点:父类的构造器调用以及初始化过程一定在子类的前面。由于Circle类的父类是Shape类,所以Shape类先进行初始化,然后再执行Shape类的构造器。接着才是对子类Circle进行初始化,最后执行Circle的构造器。

2.下面这段代码的输出结果是什么?

public class Test {
    public static void main(String[] args)  {
        Shape shape = new Circle();
        System.out.println(shape.name);
        shape.printType();
        shape.printName();
    }
}
 
class Shape {
    public String name = "shape";
     
    public Shape(){
        System.out.println("shape constructor");
    }
     
    public void printType() {
        System.out.println("this is shape");
    }
     
    public static void printName() {
        System.out.println("shape");
    }
}
 
class Circle extends Shape {
    public String name = "circle";
     
    public Circle() {
        System.out.println("circle constructor");
    }
     
    public void printType() {
        System.out.println("this is circle");
    }
     
    public static void printName() {
        System.out.println("circle");
    }
}

shape constructor
circle constructor
shapethis is circle
shape

  这道题主要考察了隐藏和覆盖的区别(当然也和多态相关,在后续博文中会继续讲到)。

  覆盖只针对非静态方法(终态方法不能被继承,所以就存在覆盖一说了),而隐藏是针对成员变量和静态方法的。这2者之间的区别是:覆盖受RTTI(Runtime type  identification)约束的,而隐藏却不受该约束。也就是说只有覆盖方法才会进行动态绑定,而隐藏是不会发生动态绑定的。在Java中,除了static方法和final方法,其他所有的方法都是动态绑定。因此,就会出现上面的输出结果。

更多Java: Classes and Inheritance相关文章请关注PHP中文网!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
解决PHP报错:继承父类时遇到的问题解决PHP报错:继承父类时遇到的问题Aug 17, 2023 pm 01:33 PM

解决PHP报错:继承父类时遇到的问题在PHP中,继承是一种重要的面向对象编程的特性。通过继承,我们能够重用已有的代码,并且能够在不修改原有代码的情况下,对其进行扩展和改进。尽管继承在开发中应用广泛,但有时候在继承父类时可能会遇到一些报错问题,本文将围绕解决继承父类时遇到的常见问题进行讨论,并提供相应的代码示例。问题一:未找到父类在继承父类的过程中,如果系统无

使用继承的Java程序来计算定期存款(FDs)和定期存款(RDs)的利息使用继承的Java程序来计算定期存款(FDs)和定期存款(RDs)的利息Aug 20, 2023 pm 10:49 PM

继承是一个概念,它允许我们从一个类访问另一个类的属性和行为。被继承方法和成员变量的类被称为超类或父类,而继承这些方法和成员变量的类被称为子类或子类。在Java中,我们使用“extends”关键字来继承一个类。在本文中,我们将讨论使用继承来计算定期存款和定期存款的利息的Java程序。首先,在您的本地机器IDE中创建这四个Java文件-Acnt.java−这个文件将包含一个抽象类‘Acnt’,用于存储账户详情,如利率和金额。它还将具有一个带有参数‘amnt’的抽象方法‘calcIntrst’,用于计

如何在PHP中使用多态和继承来处理数据类型如何在PHP中使用多态和继承来处理数据类型Jul 15, 2023 pm 07:41 PM

如何在PHP中使用多态和继承来处理数据类型引言:在PHP中,多态和继承是两个重要的面向对象编程(OOP)概念。通过使用多态和继承,我们可以更加灵活地处理不同的数据类型。本文将介绍如何在PHP中使用多态和继承来处理数据类型,并通过代码示例展示它们的实际应用。一、继承的基本概念继承是面向对象编程中的一种重要概念,它允许我们创建一个类,该类可以继承父类的属性和方法

PHP中的封装技术及应用PHP中的封装技术及应用Oct 12, 2023 pm 01:43 PM

PHP中的封装技术及应用封装是面向对象编程中的一个重要概念,它指的是将数据和对数据的操作封装在一起,以便提供对外部程序的统一访问接口。在PHP中,封装可以通过访问控制修饰符和类的定义来实现。本文将介绍PHP中的封装技术及其应用场景,并提供一些具体的代码示例。一、封装的访问控制修饰符在PHP中,封装主要通过访问控制修饰符来实现。PHP提供了三个访问控制修饰符,

PHP中的多重继承PHP中的多重继承Aug 23, 2023 pm 05:53 PM

继承:继承是面向对象编程(OOP)中的一个基本概念,它允许类从其他类继承属性和行为。它是一种基于现有类创建新类的机制,促进代码重用并建立类之间的层次关系。继承基于"父子"或"超类-子类"关系的概念。从中继承的类被称为超类或基类,而继承超类的类被称为子类或派生类。子类继承其超类的所有属性(变量)和方法(函数),还可以添加自己独特的属性和方法或覆盖继承的属性和方法继承的类型在面向对象编程(OOP)中,继承是一个基本概念,它允许类从其他类中继承属性和行为。它促进

如何使用Java强制继承代理final类?如何使用Java强制继承代理final类?Sep 06, 2023 pm 01:27 PM

如何使用Java强制继承代理final类?在Java中,final关键字用于修饰类、方法和变量,表示它们不可被继承、重写和修改。然而,在某些情况下,我们可能需要强制继承一个final类,以实现特定的需求。本文将讨论如何使用代理模式来实现这样的功能。代理模式是一种结构型设计模式,它允许我们创建一个中间对象(代理对象),该对象可以控制对另一个对象(被代理对象)的

继承、多态与接口:PHP面向对象的三大特性继承、多态与接口:PHP面向对象的三大特性May 11, 2023 pm 03:45 PM

PHP是一种服务器端编程语言,自PHP5之后开始支持面向对象编程(OOP)。OOP的核心思想是将数据和行为封装在对象中,以提高程序的可维护性和可扩展性。在PHP中,面向对象编程具有三大特性:继承、多态与接口。一、继承继承是指一个类可以从另一个类中继承属性和方法。被继承的类称为父类或基类,继承的类称为子类或派生类。子类可以通过继承获得父类中的属性和方法,并且可

如何在Go语言中实现封装和继承如何在Go语言中实现封装和继承Jul 23, 2023 pm 08:17 PM

如何在Go语言中实现封装和继承封装和继承是面向对象编程中的两个重要概念,它们可以使代码更加模块化和可维护,同时也为代码的复用提供了便利。本文将介绍在Go语言中如何实现封装和继承,并提供相应的代码示例。封装封装是将数据和功能进行封装,隐藏实现的细节,只暴露必要的接口给外部使用。在Go语言中,封装是通过导出和非导出标识符来实现的。首字母大写的标识符可以被其他包访

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version