In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "what are the characteristics of Java inheritance and rewriting". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Concept: inheritance is one of the three major features of object-oriented grammar. Inheritance can reduce the redundancy of code and improve the efficiency of programming. Some properties and methods in the parent class can be called at will by inheriting the subclass. a subclass can only inherit one parent class, and a parent class can be inherited by multiple subclasses. It's like showing that children inherit their father's property in life. The advantage of rewriting is that subclasses can define their own specific behavior as needed. That is to say, subclasses can implement the methods of their parents according to their needs, just like Golden hair and huskies, whose characteristics come from dogs, hamsters and squirrels, and their characteristics come from mice, and their differences belong to genetic mutations equivalent to rewriting.
Characteristics of inheritance:
1): only single root inheritance is supported in java, that is, a class can have only one direct parent class, and a parent class can have multiple subclasses.
2): extract the attributes common to the subclass to generate the parent class attribute for inheritance
3): subclasses cannot inherit or override attributes and resources decorated by the parent class private
Rewritten features:
1)。 The method overridden by the subclass must have the same method name and parameter list as the method overridden by the parent class
2)。 The return value type of the method overridden by the subclass cannot be greater than the return value type of the parent class overridden.
3)。 The access permissions used by methods overridden by subclasses cannot be less than the access rights of methods overridden by the parent class. Methods declared as private (that is, private properties) permissions in the parent class cannot be overridden by the subclass.
4)。 The exception thrown by the subclass method cannot be greater than that of the overridden method of the parent class
The method or keyword to be used:
Extends: we can inherit through this keyword, and write the class or method to be inherited after the keyword to achieve inheritance.
Super (): we can use this method to call the properties or methods of our inherited parent class
Test class:
Package com.ytzl.total;/* inherits * / public class inherit {public static void main (String [] args) {son son = new son (); / / calls the subclass System.out.println (son.a); / / calls the attribute son.a=2;// in the subclass to change the attribute System.out.println ("changed parent attribute" + son.a) in the parent class through the subclass. Son.myfather (); / son.money () with calling parent class; / / calling overridden method w son.jng (); / / calling statically decorated method son.jing (); System.out.println ("subclass invokes attributes in parent class by inheriting methods = >" + son.b); / / calls static attributes in parent class son.two ();}}
Parent class:
Package com.ytzl.total;public class father {int adepo; static int bread2; private int one=4;// cannot be called or inherited by other subclasses after being modified by private attributes. Protected int two=0; protected void two () {/ / this method can only be inherited by subclasses and cannot be called System.out.println ("I am the protected method");} public void setOne (int one) {this.one = one } public void money () {System.out.println ("I am the method overridden by the subclass"); System.out.println ("attributes of the parent class = >" + a);} public void myfather () {System.out.println ("I am the method inherited by the subclass");} public static void jing () {System.out.println ("Hello") } public static void jng () {System.out.println ("hello");} private void dong () {System.out.println ("private method");}}
Subcategory:
Override of package com.ytzl.total;public class son extends father {/ / method public void money () {super.money (); / / by inheriting the contents of the parent class's money method System.out.println ("overridden method"); System.out.println ("inherited" + super.a) / inherit the property System.out.println of the parent class through super ("inherited protected attribute = >" + super.two); / / inherit the protected method super.two () through super;} public static void jing () {/ / cannot override the static method static System.out.println ("bad") } private void dong () {/ / cannot override or inherit the parent's private method System.out.println ("private method");} protected void two () {/ / overridden protected method System.out.println ("I am the overridden protected method");}}
The result after running:
This is the end of the content of "what are the characteristics of Java inheritance and rewriting". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.