Inheritance of Java Classes

Source: Internet
Author: User

Inheritance is the creation of a new type based on an existing type, either by adding new methods or by redefining the existing methods (overrides). Inheritance is one of the three basic features of object-oriented-encapsulation, inheritance, polymorphism, and every class we write when we use Java is inherited, because in the Java language, the Java.lang.object class is the fundamental base class (or parent class, Superclass) of all Classes. If one of our newly defined classes does not explicitly specify which base class to inherit from, then Java defaults to that it inherits from the object class.

1. Why do I need to use inheritance? Improve the reusability of your Code. Improve the extensibility of the Program.

2.Java inherited syntax format Java inherited keyword: extends Format: public class class name (subclass, derived class, Superclass) extends class name (parent class, base Class) {}? note: Inheritance for Java classes is single-inheritance.

3. When the subclass inherits from the parent class which properties and method subclasses inherit the parent class, the subclass inherits all the properties and common methods of the parent class. The constructor method does not inherit the quilt class. Whether a method or property can be invoked is determined by the relationship of the access Modifier. Public all the places can be called protected under the current package and have inherited the relationship between the different packages of the Subclass. The default can only be called under the current package private only in the current class. Can subclasses define properties and methods themselves? Can and subclasses can have more properties and methods than the parent class

4. Method Override   Method override condition   must have inheritance relationship   Method's return value type, method name, number of arguments, parameter type, parameter order must be exactly the same as the parent class.   When a subclass overrides a method, the access modifier for the method can be greater than or equal to the access modifier of the parent class Method.   Will I call the wrong method when I call the method?   whether the object created is a subclass (new constructor method ())   If the object is created by the constructor method of the subclass, it takes precedence over the method of the subclass, and the subclass does not execute the method of the parent class.   The difference between overloading and overriding:  overloads and overrides are all about the concept of methods, and before we figure out these two concepts, let's first look at what is called a Method's construct. A type structure refers to the composition of a method, including the name and parameters of the method, the number of parameters, the type, and the order in which they appear, but does not include the return value type of the method, the access permission modifier, and the abstract, static, final, and so on Modifiers. For example, The following two is a method with the same type of structure:  public void method (int I string S) { //do something } public string method (int i S Tring S) { //do something }  and these two are methods with different types:  public void method (int I string S) {//do something  ;}  public void method (string s int i) { //do something }  overload:  English name is overload, means that more than one method with the same name is defined in the same class, but with different types of Configuration. In the same class, it is not allowed to define more than one method with the same type of structure. The   method name is the same, but the parameter type, quantity, or order must be different.   can be overloaded in the same class or in a class that has an inheritance Relationship.   rewrite:  English name is override, that is, in the case of inheritance, a subclass defines a new method with the same type of method in its base class, which is called a subclass that overloads the method of the base class. This is the necessary step to achieve Polymorphism. The   method name, parameter type, quantity, order must be identical, the access modifier can be different, but the subclass must be greater than or equal to the parent class. Occurs only in classes that have an inheritance Relationship.

  5. auto-transition   orientation: Subclass auto-parent   premise: inheritance relationship   syntax:  [1] Direct Auto Transformation   Parent class Name Object name = new Subclass Constructor ()  student Stu =new unstudent ()  [2] Automatic Transformation in methods   Subclass Name Object name = new Subclass Constructor ();  unstudent unstu =new unstudent ();  teach (unstu) the;  access modifier Returns a value type method name (parent class name parameter Name) { } public void teach (Student Stu) { }  do not have to tube to create the left part of the object, Just look to the right. the constructor for new is a subclass or a parent class, and if it is a subclass, the method of the subclass is called First.   Note: to use automatic transformation, an inheritance relationship must Exist.   format:  Parent Class Name Object name = new Subclass Construction Method (parameter value,...);  student un = new unstudent ();  un.play ();  after using automatic transformation, you cannot call a subclass custom method;  because Java compiles, only the type of object name to find out whether the method exists, If it exists, it is compiled and an error is present if it does not exist.   Example: A university teacher, can teach college students, can also teach junior high school, senior students, students   so, for this teaching method we can write only once. The corresponding automatic conversion is then used to achieve the goal of teaching various classes of Students.   for us, with automatic transformation, you can only invoke methods that have already been defined in the parent class.   For this transformation, we have 2 ways to: : parent class name Parent class object name = new Subclass Constructor (), Second: Parent class Name Parent class object name = new Subclass constructor () subclass Name Subclass Object name = new Subclass Constructor ();  public return Value type method name (parent class name parent class object Name) {  The parent class object Name. method name (); }  method name (parent Class object name);  method Name (subclass object name); however, Automatic transformation also has obvious drawbacks, if you use automatic transformation, The unique method of the subclass cannot be Called. Because the computer is rather stupid, it can only be implemented in a fixed and established way, Java istranslation, is based on the type of object name to compile the program, this time in the subclass, is not found the corresponding method, so the compilation Error.

Technology Sharing: Www.kaige123.com

Inheritance of Java Classes

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.