Java Interfaces and classes

Source: Internet
Author: User

The task of the recent group is not very busy, looking at the content of MyBatis, found a lot of interfaces in the project, the future to transform some of the code to see a bit. When reading the code of the predecessor, found a feature: the interface of many inheritance. So I went to the internet to find some inheritance and interface usage, revisit, also summarize the next.

Inherited

A class in 1.Java does not support multiple inheritance, that is, a subclass has at most one parent class.

2. Subclasses inherit member variables and member methods that are not private in the parent class.

3. When a member variable in a subclass is consistent with a member method and a parent class, the method of the parent class is overwritten.

1234567891011121314151617181920212223242526272829 public class Test {        publicstatic String a = "a";    privateString b = "b";        {        System.out.println("no static");        System.out.println("b:"+ b);    }        static{        System.out.println("static");        System.out.println("a:"+a);    }        publicTest(){        this("un");        System.out.println("Test():"+this.b);    }        publicTest(String val){        this.b = val;        System.out.println("Test(val):"+this.b);    }        publicstatic void main(String[] args) {        newTest();    }}

The output is as follows:

123456 statica:ano staticb:bTest(val):unTest():un

To explain:

1. When instantiating an object, allocate the memory space for the static variable and initialize it before executing the static block.

2. Initialize all non-static variables as default values, and then display initialization, i.e. B = "B".

3. Perform non-static method blocks after initializing non-static variables.

4. Execute the constructor, calling the constructor with parameters in the default constructor.

The relationship between the draw line class, the member variables of the subclass, and the member method and the parent class

1234567891011121314151617181920 public abstract class Parent {    //父类是一个抽象类,可以有自己的成员变量和成员函数,成员变量和成员函数可以是abstract的,也可以是非abstract的。但是父类不能被实例化    static{        System.out.println("Parent");    }        inti;        Parent(inti){        this.i = i;        System.out.println("Parant:"+i);    }        abstractpublic void f1();    abstractpublic void f2();    publicvoid f3(){        System.out.println("Parennt f3():"+ i);    }}

Subclass 1: Implements all methods of the parent class, both abstract and non-abstract.

1234567891011121314151617181920212223242526272829 public class Child1 extendsParent {    //子类继承抽象的父类,必须实现其所有抽象方法及构造函数。    Child1(inti) {        super(i);        i++;        System.out.println("Child1:"+ i);    }    @Override    publicvoid f1() {        System.out.println("Child1:f1():"+i);    }    @Override    publicvoid f2() {        // TODO Auto-generated method stub    }        publicvoid f3(){        this.i =5;        System.out.println("Child f3():"+ i);        System.out.println("Child f3() spuer "+ super.i);        super.f3();    }}

Subclass 2: An abstract method that implements the parent class. When a subclass inherits an abstract class, it must implement all the abstract methods in the abstract class.

123456789101112131415161718192021 public class Child2 extendsParent {    Child2(inti) {        super(i);//必须在第一行        this.i =8;        System.out.println("C2 "+ this.i);    }    @Override    publicvoid f1() {        // TODO Auto-generated method stub    }    @Override    publicvoid f2() {        // TODO Auto-generated method stub        System.out.println("Child2 f2():"+ i);    }}

Write a main function and execute:

123456789101112131415161718 public static void main(String[] args) {    System.out.println("main start");    Child1 c1 =new Child1(0);    Child2 c2 =new Child2(0);         System.out.println("new");        c1.f1();    c1.f2();    c1.f3();        System.out.println("Child1 over");        c2.f1();    c2.f2();    c2.f3();    }

We know that subclasses inherit the parent class and have access to the non-privatized member variables and member methods of the parent class for subclasses. If the amount of space used for analogy, the parent class and subclass have two of the same type, but the name of the variable, if the parent class occupies memory space is 1M, then the child class occupies a memory space of 2M, this is hypothetical situation, because there is a hidden reference in the child class super will point to the parent class instance, A parent class is instantiated before the child class is instantiated, that is, the constructor of the parent class is executed first.

1234567891011121314 parent main start parant: 0 Code class= "Java Plain" >child1: 1 parant: Code class= "Java value" >0 child2 8 new child1:f1 (): 0 Code class= "Java Plain" >child F3 (): 5 child F3 () Spuer 5 parennt F3 (): 5 child1 over child2 F2 (): 8 parennt F3 (): 8

Subclass Child2 does not implement the F3 method of the parent class, invokes the implementation of the parent class, but only uses the parent class's method, the data member, or its own.

Super keyword

The Super keyword's role in Java is to make the masked member variable or member method visible. Or it is used to refer to masked member variables and member methods.

Interface

The Java language does not support a class with multiple direct parent classes, but it can implement multiple interfaces and indirectly implement multiple inheritance. The methods in the Java interface are public,abstract types, no method bodies, and cannot be instantiated. Interface is a pure abstract class, only abstract methods, absolutely no implementation. Implementing an interface is adding one or more methods to your own class. The interface is used for specification in design mode.

The interface facilitates the extension of the code function, that is, adding functionality. Inheritance is useful for modifying the function of the code (modifying the perfect function). The modification of the old functionality here and the addition of new features are not the function of modifying the parent class, and the function of the parent class is still available.

Java Interfaces and 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.