Java Interview Collection (vi)

Source: Internet
Author: User

1. Abstract abstraction

What is abstract , Chinese is abstract, extract from concrete things, generalize their common aspect, essential attribute and relation, etc., call abstract. invisible, the touch of something called abstraction, abstraction is the feeling of all things in the world, with specific images expressed, to understand the abstract things, it is necessary to feel them from the heart.

What is abstract class

Definitions and usage formats for abstract classes

abstract class 类名{       类体}
class 类名 extends 抽象类 {          //实现全部抽象方法}

Abstract class, with abstract decorated class as abstract class, a class if all its subclasses have overridden this method, then add abstract declaration to it, then this class is abstract class.

Then the class of this abstract method is abstract class, the class with abstract method is abstract class, but abstract class does not necessarily have abstract method, abstract class can have construction method, cannot create object.

If there is an abstract method in an abstract class, then the subclass inherits the abstract class and must rewrite all the abstract methods in the abstract class.

When a class abstract is decorated as an abstract class, then this abstract class cannot be used final to decorate, abstract classes are abstract collections of class objects, abstract classes are abstract decorated with keywords, abstract and cannot be final decorated with a class.

For abstract methods, it is not static possible final to use and and private to modify

Abstract classes are specifically abstract properties and behavior, in an abstract class, the class containing abstract methods are abstract classes, must be abstract classes, but abstract classes do not necessarily contain abstract methods.

Case:

//抽象类:作为抽象方法是可以重载的public abstract class Person { public abstract void run();}//子类继承抽象类public class Studentone extends Person{ @Override public void run(){  System.out.println("人会跑"); }}//测试public class Test { public static void main(String[] args){  Person person = new Student();  person.run(); }}//结果人会跑
2. Interface what is interface (interface)

The methods in the interface are abstract methods , public permissions, all abstract functions, cannot generate objects

interface Student{public void read();public void write();}class ChineseStudent implements Student{//复写public void read(){ System.out.println("read");}public void write(){ System.out.println("write");}}class Test{public static void main(String args[]){ ChineseStudent chinesestudent = new ChineseStudent(); Student student = chinesestudent;  student.read(); student.write();}}

Interface definition and usage format

interface 接口名 {          [public] [static] [final] 变量;          [public] [abstract] 方法;}
class 类名 implement 接口列表 {          //实现所有接口中声明的方法}

What is an interface? All the methods in this class are abstract methods.

interfaces are different abstractions, there is no construction method , and the interface is not a class.

An object cannot be created in an interface , nor can an abstract method.

In the JDK1.8 past, interface to define an interface through which the implements interface and class are generated by a keyword, the class implements the interface, overriding all the abstract methods in the interface.

In the Java only single-inheritance, multi-implementation, a class can inherit a parent class, but a class can implement multiple interfaces.

实现接口用implements关键字,一个接口可以实现多个接口,一个接口可以继承多个接口interface Student{public void read();public void write();}interface Teacher{public void teach();public void test();}class Person implements Student,Teacher{public void read(){ System.out.println("read"):}public void write(){ System.out.println("write");}public void teach(){ System.out.println("teach"):}public void test(){ System.out.println("test"):}}
// 测试class Test{public static void main(String args[]){ Person person = new Person(); Student student = person; student.read(); student.write(); Teacher teacher = person; teacher.teach(); teacher.close();}}

Interface solves the problem of multiple inheritance , the mechanism of multi-inheritance java is implemented through the interface, the interface and interface is the inheritance relationship.

interface A{ void showa();}interface B{ void showb();}class C implements A,B{ public void showa(){}; public void showb(){};}

The properties in the interface are by default using public static final the decorated

interface A {void add();}class B implements A { // 接口中的方法默认是使用public修饰void add(){}}

Implementing a multi-interface, when there is a consistent method name, can lead to ambiguity when the method is rewritten.

Abstract class and interface ratio
    • An abstract class is an abstraction of a class, and an interface is an abstraction of the behavior.
    • An abstract class is an inheritance relationship, an interface that implements a contractual relationship between a class and an interface.
    • Abstract classes are single-inheritance, with multiple "inheritance" interfaces. Subclasses can inherit only one abstract class, but can implement multiple interfaces.
Code
//抽象类abstract class Person { abstract void run(); abstract void jump();}//接口interface School{ void go();}//实现class Student extends Person implements School{ void run(); void jump(); void go();}

The member variable in the abstract class defaults to friendly the function modifier public,protected,default .

The interface property is static , public static final and the member function is public .

3. Inner class

Classification of inner classes:

    • Static Inner class
    • Local inner class
    • member Inner class
    • Anonymous inner class
// 外部类class Demo{ private int num = 3; // 定义内部类 class Int{  void show(){   System.out.println("num="+num);   } }  void method(){  Int int = new Int();  int.show(); }}class Inner{ public static void main(Stirng[] args){  Demo demo = new Demo();  demo.method(); }}

Internal classes have non-static, static, anonymous classes

Grammar:

New external Class (). New Inner Class ()

member Inner class:

The class defined at the position of the member variable is a member inner class.
If you want to use a member inner class, use an external class. Internal classes to access.

Static Inner class:

An inner class decorated with a member position static is a static inner class.

Static inner classes can access only static members of external classes and members of this inner class, and cannot access non-static members of external classes .

Anonymous inner class: (Subclass of the corresponding class or implementation class for the corresponding interface)

is a special kind of local inner class.

accessing inner classes in external classes, non-static

Outer.Inner int = new Outer().new Innter();

Non-static members in static inner classes

Outer.Inner int = new Outer().new Innter();

With the inner class of the declaration, the static inner class becomes an outer class, and the static declared inner class cannot access a non- static external class property, and the properties of the outer class are to be static accessed.

Internal classes can use all methods and variables of the external class, even if they are privatized, such as:

class Outer{ private int x; // 创建内部类的实例 Inner inner = new Inner(); public void do(){  // 调用内部类的方法  inner.go(); } // 内部类 class Inner{  void go(){   x = 30;  } }}
4. Package

javadifferent classes and interfaces are provided in the different packages.

Common packages

java.appletjava.awtjava.netjava.utiljava.langjava.awt GUIjava.io 数据传输java.math  数学运算java.nio 高并发java.net  网络编程java.text  格式化java.sql  和数据库交互

Format of package:

package 包名[.子包名[…]];

Introduction java Package

import  包层次结构的类名;
Garbage generation and recycling mechanism

Garbage collection mechanism for heap memory, all memory is Java automatically allocated to the recycle, garbage collector.

Conclusion
    • Below I will continue to Java the Android other knowledge, in-depth explanation, interested can continue to follow
    • A little gift to walk or like.

Java Interview Collection (vi)

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.