6.11.11 inheritance and interface, 2016.11.11 inheritance

Source: Internet
Author: User

6.11.11 inheritance and interface, 2016.11.11 inheritance

class Grandparent {    public Grandparent() {        System.out.println("GrandParent Created.");    }    public Grandparent(String string) {        System.out.println("GrandParent Created.String:" + string);    }}class Parent extends Grandparent {        public Parent() {        super("Hello.Grandparent.");        System.out.println("Parent Created");       // super("Hello.Grandparent.");    }}class Child extends Parent {    public Child() {        System.out.println("Child Created");    }    }public class TestInherits {    public static void main(String args[]) {        Child c = new Child();        }}

 

Conclusion: calling the base class constructor through super must be the first statement in the subclass constructor.

 

The subclass must first call the constructor of the parent class because:

Constructor is a special method. It is mainly used to initialize an object when an object is created, that is, assigning an initial value to the object member variable. It is always used together with the new operator in the statement for creating an object. A special class can have multiple constructors, which can be distinguished based on the number of parameters or different parameter types, that is, the overload of constructors. The constructor is used to define the initialization status when a class object is created.


Construct an object and call its constructor to initialize its member functions and member variables.
Child classes have parent member variables and member methods. If they are not called, the member variables and member methods inherited from the parent class cannot be correctly initialized.
It cannot be called in turn because the parent class does not know what variables exist in the subclass and does not get the initialized parent class variables, which leads to program running errors!


Classes that cannot be inherited:
Final class Name
{

}
The Methods declared by final are not allowed to be overwritten.
Variables declared with final cannot be modified.
Using final, you can design a special "read-only" "immutable class ".


public class ExplorationJDKSource {    /**     * @param args     */    public static void main(String[] args) {        System.out.println(new A());    }}class A{}

 

 

In the example, the main method actually calls:

 

Public void println (Object x), which internally calls the valueOf method of the String class.

 

The valueOf method calls the Object. toString method internally:

 

Public String toString (){

 

Return getClass (). getName () + "@" +

 

Integer. toHexString (hashCode ());

 

}

 

The hashCode method is a local method, which is implemented by the JVM designer:

 

Public native int hashCode ();


Method coverage:
class Parent extends Grandparent {            public int add1(){        return 1;    }}
 
class Child extends Parent {        public int add1(){         return 2;    }    public int  superadd1(){          return super.add1() ;              }     }

 


Conclusion: The method override requires that the subclass and the parent class have the same method; otherwise, the method is overload ).
In a subclass, you can use the super keyword to call the override method in the parent class.
1. The allowed access range of the override method cannot be smaller than that of the original method.
2. The overwrite method cannot throw more exceptions than the original method.
3. The declared final method cannot be overwritten. For example, the getclass () method of the object cannot overwrite.
4. Static methods cannot be overwritten.


Abstract class:

 

1. abstract: A class is called an abstract class. It defines only the methods that should exist and cannot create objects. After a subclass is derived and its unimplemented methods are implemented in the subclass, you can use the new keyword to create an object.

 

Abstract is added before a method to form an abstract method. Only method declaration is required, and no code is implemented.

 

2. The subclass inherited from an abstract class must implement all abstract methods of the parent class. Otherwise, it is still an abstract class.

 

3. the following modes are always true:

 

Abstract class abstract class variable = new the specific subclass derived from the abstract class ();

 

Interface:

1. Define an interface, use the keyword interface to implement an interface, and use the keyword implements

2. The member functions of the interface automatically become public, and the data members become static and final.

3. If the interface is not declared as public, the package is automatically changed.

4. A class can implement multiple interfaces at the same time.

5. The variable = new of the interface type interface implements the specific interface type ().

Interface expansion:

1. You can use an inherited interface to expand an existing interface and form a new interface.

   interface OneInterface {void f1();}interface TwoInterface extends OneInterface {void f2();}

2. classes that implement sub-interfaces must implement all methods defined by the "parent" and "sub" interfaces to be instantiated (I .e., a new object ).

Differences between interfaces and abstract classes:

1. abstract classes are incomplete classes, and interfaces only indicate the "external" Features of classes, without any implementation details.

2. The interface basically does not have any specific characteristics of inheritance. It only promises the methods that can be called by the outside world.

3. A class can implement several interfaces at a time, but a class can only inherit one parent class.








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.