In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "Java inheritance case Analysis". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this "Java inheritance case Analysis" article can help you solve the problem.
I. brief introduction
What is inheritance? General perception, such as inheriting craftsmanship, inheritance, etc., to acquire skill or wealth from another person.
In Java, inheritance is one of the three major features, a mechanism that is represented as getting all the properties and behaviors of a parent object from a child object. In other words, the subclass inherits everything from the parent class, and it can have something that the parent class doesn't have.
In Java, to use inheritance, you need to use the keyword extends, with the following syntax:
Class Subclass-name extends Superclass-name {/ / methods and fields}
In the figure above, Employee is the parent class and Programmer is the subclass. Indicates that Programmer is a subtype of Employee. Specific code:
Class Employee {float salary = 48880;} class Programmer extends Employee {int bonus = 10000; public static void main (string args []) {Programmer p = new Programmer (); System.out. Println ("Programmer salary is:" + p.salary); system.out. Println ("Bonus of Programmer is:" + p.bonus);}}
Execution result:
Programmer salary is: 40000.0Bonus of programmer is: 10000
From the result, we can see that the subclass inherits the salary of the parent class in addition to its own defined bonus.
II. Java inheritance type
In Java, there are mainly three types of inheritance: single, multi-level and hierarchical. In Java programming, multiple and mixed inheritance can only be supported through interfaces. To learn about this section, you can refer to the Java Basics interface.
[note] in Java, only interfaces accept multiple inheritance, while classes have and can only inherit one.
(1) single inheritance
Class Animal {void eat () {system.out.println ("eating...");}} class Dog extends Animal {void bark () {system.out println ("barking...")}} class TestInheritance {public static void main (string args []) {Dog d = new Dog (); d.bark (); d.eat ();}} barking...eating...
(2) Multi-level inheritance
Class Animal {void eat () {System.out.printin ("eating..."):}} class Dog extends Animal {void bark () {System.out.printIn ("barking...");}} class BabyDog extends Dog {void weep () {System.out.printin ("weeping...");}} class Testinheritance2 {public static void rain (String args []) {BabyDog d = new BabyDog () D.weep (); d.bark (); d.eat ();}} weeping...barking...eating...
(3) hierarchical inheritance
Class Animal {void eat () {System.out.println ("eating...";}} class Dog extends Animal {void bark () {system.out.println ("barking...")}} class Cat extends Animal {void meow () {System.out-println ("meowing...");}} class TestInheritance3 {public static void main (string args []) {cat c = new Cat () C.meaw (); c.eat ();}} meowing...eating... This is the end of the introduction to "Java inheritance instance Analysis". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.