Java Inheritance and overwrite

Source: Internet
Author: User


The purpose of inheritance?
: Reuse of code    
What is inheritance?
extends
Syntax:
Class subclass extends parent class {

        }
inherit object By default if no inheritance is declared (ancestor class for all classes)
static variables: cannot be inherited, but can be called directly from the class name
static methods: cannot be inherited, can be called
member Variable: can be inherited, provided that the subclass does not have the same variables as the parent class (data type and variable name) if there is the same variable, it will not inherit
member methods: will be overwritten by inheritance
Local variables: in the method does not matter is inherited follows the method to walk
Constructor method: not because the constructor method is used to instantiate the inheritance of the past, the instantiation of the object of whom? So definitely not .

If there is no inheritance, how could it be written?
The overwrite can be said to be for the member method, also can say that except the member method, nothing else is inherited


The purpose of the overwrite?
: Reuse of code
What is overwrite?
: The return value is the same, the method name is the same , the parameter list is the same (the number and data type are the same) (for the member method, because the other cannot be inherited, there is no overwrite that said)
    
This represents the object of this class
Super represents a feature of the parent type (and there is no instantiation of the object)
This (); represents a non-parametric construct that invokes this class
super (); represents the non-parametric construction of the calling parent class, even if it is not written, the default will be
This and super must all be written in the first line so the same method, this and super cannot appear simultaneously

Code:
public class ji_cheng_fu_xie{
public static void Main (string[] args) {
Zi_lei z = new Zi_lei ();//instantiation of the current class
Z.m_1 ();//Call my own member method with the object name, it should be OK (actually calling the Member method to overwrite the parent class)
Z.m_2 ();//Invoke its own static method with the object name, and the underlying default conversion is called by the class name (no overwrite is present because no inheritance)
Z.m_3 ();///There is no M_3 method in my sub-class, why does it output? Because the inheritance of the parent class to me, actually has, above m_2 (); it's because I've covered it, so I'm going to do it after I've written it, and I've inherited it from the parent class.
System.out.println (Z.I);//Call member variable
System.out.println (Z.A);//Call static variable

System.out.println (z.h);//Why this code can not be executed, error? Because when I declare in the parent class, it is not inherited because it is used private.

Fu_lei f = new Zi_lei ();//polymorphism: A reference to the parent class, which points to the object of the child class
F.m_1 ();//Call the M_1 () method of the parent class
F.m_2 ();//Call the parent class M_2 () method (which will output the subclass of the overridden one this design to polymorphic, first to find the parent class of this method, to see if it is inherited, if not inherited, the output of the parent class, if it is inherited, go to the subclass, see if this method is overwritten, The output of the original member method, if overwritten, will output the Overwrite method, so here is the output is "I am a subclass of the member method, I overwrite the parent class m_2" because I overwrite)
F.m_3 ();//But this is not the same, this will output the "member method of the parent class M_3" because it is inherited, but my sub-class did not overwrite
}
}

Class fu_lei{//declares that a parent class is inherited
private int h = 100;//Member variable can this be inherited?
int i = 100;//member variable
static int a = 10;//statically variable
public static void M_1 () {///static method (do I inherit from subclass? No,,, so there is no overwrite)
System.out.println ("static method M_1 of the parent class");
}
public void M_2 () {//member method in subclass overwrite
System.out.println ("Member Method M_2 of the parent class");
}
public void M_3 () {//member method not overridden in subclass
System.out.println ("Member Method M_3 of the parent class");
}
}
Class Zi_lei extends fu_lei{//declares a subclass and inherits the parent class inheritance syntax: Class Subclass extends (inherited) parent class
public static void M_1 () {//static method (not present overwrite, because not inherited)
System.out.println ("I am the static method of the subclass, I did not overwrite the static method of the parent class, because it did not inherit to me");
}
public void M_2 () {//Overwrite the member method of the parent class m_2 ();
System.out.println ("I am a member of the subclass method, I overwrite the m_2 of the parent class");
}

}

Java Inheritance and overwrite

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.