Inheritance in Java: Concepts and Usage
The concept of inheritance
Inheritance means that the subclass inherits the characteristics and behaviors of the parent class, so that the subclass object (instance) has the instance domain and instance domain of the parent class. Method, or a subclass inherits a method from a parent class, so that the subclass has the same behavior as the parent class.
Through inheritance, you can quickly create new classes, realize code reuse, improve program maintainability, save a lot of time in creating new classes, and improve development efficiency and quality.
Benefits of inheritance:
Reduce code duplication
Improve code reusability
Facilitates function expansion
class A extends B{}
class teacher{ //声明一个teacher类为父类 String name; //定义父类的成员变量name、age int age; void show(){ //定义父类成员方法,将成员变量输出 System.out.println(name); System.out.println(age); } } class Student extends teacher { //声明一个Student类为子类并继承父类 } public class myfirst { public static void main(String[] args) { System.out.println("学生"); Student student=new Student(); //声明一个Student类的实例对象student student.name="Tom"; //子类调用父类的成员变量name并赋值 student.age=19; //子类调用父类的成员变量age并赋值 student.show(); //子类调用父类的成员方法show } }
Permission modifier return value type method name (formal parameter list)
About return value type:Return value type of the overridden method in the parent class is void, then the return value type of the method overridden by the subclass can only be void. The return value type of the overridden method of the parent class is type A, then the return value type of the method overridden by the subclass can be class A or Subclasses of class AThe return value type of the overridden method of the parent class is a basic data type (for example: double), then the return value type of the method overridden by the subclass must be the sameThe method in the subclass is called the overridden method, and the method in the parent class is called the overridden method The method written, the method name and formal parameter list of the method overridden by the subclass are the same as the method name and formal parameter list of the overridden method of the parent class, and the permission modifier of the method overridden by the subclass is not less than that of the overridden method of the parent class. Special case of method permission modifier: Subclasses cannot override methods declared as private permissions in the parent class
Methods with parameters of the same name in subclasses and parent classes must be declared as non-static before they can be overridden. If they are static, they cannot be overridden. For example,
class A{ public void sayHello() { //输出英文欢迎 System.out.println("Hello,Welcome to Java!!!"); } public void sayBye() { System.out.println("GoodBye,everyone"); } } class B extends A { public void sayHello() { //输出中文欢迎 System.out.println("大家好,欢迎学习Java!!!"); } } public class myfirst { public static void main(String[] args) { B b=new B(); //创建子类B的一个实例对象,使用默认构造方法 b.sayHello(); //调用子类中重写的方法 b.sayBye(); //调用父类中的方法 } }
package first; class A{ public String name="张飞"; //添加成员变量 public void say() { //添加成员方法say System.out.println("我是父类A成员方法say"); } } class B extends A{ public String name="关羽"; //与父类中同名的字段,隐藏父类 public void say(){ //重写方法say super.say(); //使用super关键字调用父类中的方法 System.out.println("我是子类B成员方法say"); System.out.println("父类的name名字:"+super.name); //使用super关键字访问父类中的变量 } } public class myfirst { public static void main(String[] args) { B b=new B(); //创建子类的一个实例对象 b.say(); //调用子类中重写的方法 System.out.println("子类的name名字:"+b.name); //调用子类中的name } }
The above is the detailed content of Inheritance in Java: Concepts and Usage. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
