Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Object oriented concepts

  • Object orientation is to divide the problem to be solved into multiple independent objects according to certain rules, and then solve the problem by calling the method of the object
  • The three characteristics are encapsulation, inheritance and polymorphism respectively
  • Encapsulation is the core idea of object orientation, which encapsulates the attributes and behaviors of objects without letting the outside world know the specific implementation details
  • Inheritance mainly describes the relationship between classes. Through inheritance, the functions of the original class can be extended without the need to rewrite the original class
  • Polymorphism refers to the duplication of names allowed in programs. It means that when attributes and methods defined in one class are inherited by other classes, they can have different data types or exhibit different behaviors, which makes the same attribute and method have different semantics in different classes

Relationships between classes and objects

  • Declare a class using the “class” keyword
  • A class is an abstraction of an object that describes the common characteristics and behavior of a group of objects
  • Member variables are used to describe the characteristics of an object

  • Class encapsulation means that when defining a class, the attributes in the class are privatized. That is, the private keyword is used to modify the attributes. The private attributes can only be accessed in the class in which they reside

  • A constructor is a special member of a class that is called automatically when the class instantiates an object

  • The constructor is preceded by the class header after the class name. If the main constructor does not have any annotations or visibility modifiers (such as public), the constructor keyword may be omitted

  • When a class is defined, the Kotlin compiler generates a no-argument constructor for it by default if the specified constructor is not displayed

  • When assigning values in a primary constructor, init{} is typically used as a block of initialization code specifically for the initialization of attributes
  • The this keyword is provided in Kotlin to access other members of an object in a function in the syntactic format of “this”. Member name”

– The secondary constructor must call either the primary constructor or another secondary constructor as “secondary constructor :this (argument list)”.

  • When the newly defined secondary constructor calls the primary or secondary constructor, the order of arguments in the called constructor must be the same as the order of arguments in the newly defined secondary constructor, and the number of arguments must be smaller than the number of arguments in the newly defined secondary constructor

  • Class inheritance refers to the construction of a new class on the basis of an existing class. The new class constructed is called a subclass, and the existing class is called a parent class. The subclass automatically has all the inheritable attributes and methods of the parent class. Because all classes in Kotlin are final by default and cannot be inherited, you need to prefix a class with the open keyword when you inherit it

  • In Kotlin, a class can only inherit from one parent, not multiple, that is, a class can only have one parent

  • Multiple classes can inherit from a single parent class

  • Multi-level inheritance is also possible, that is, the parent of A class can inherit from another parent. For example, class C can inherit from class B, and class B can inherit from class A. In this case, class C can also be called A subclass of class A

  • Subclasses and superclasses are relative concepts. That is, a class that is a parent can also be a subclass of another class

  • Methods overridden in a subclass must have the same method name, argument list, and return value type as those overridden in the parent class, and must be preceded by the “override” keyword

  • An attribute overridden in a subclass must have the same name and type as an attribute overridden in a parent class, and must be preceded by the override keyword

  • Properties and methods that need to be overridden in the parent class must be preceded by the open keyword

  • The syntax for calling member variables and member methods of the parent class using the super keyword is as follows

  • All classes in Kotlin inherit from Any, which is the parent of all classes. If a class is declared without a parent, the default parent is Any, which is automatically mapped to the Java.lang.object class in Java when the program runs.

  • Abstract methods are qualified with the abstract keyword. This method has no body and needs to be implemented when used. When a class contains an abstract method, the class must be defined as abstract using the abstract keyword

  • Classes that contain abstract methods must be declared abstract, but an abstract class may contain no abstract methods and be decorated with the abstract keyword

  • Abstract classes cannot be instantiated because they may contain abstract methods that have no body and cannot be called

  • If all the methods in an abstract class are abstract, the class can be defined in another way, that is, an interface, declared with the interface keyword

  • Methods in interfaces are abstract and cannot instantiate objects

  • When a class implements an interface, it implements only some of the methods in the interface if the class is abstract; otherwise, it implements all of the methods in the interface.

  • When a class implements an interface with a colon (:), it can implement multiple interfaces, separated by commas

  • An interface can inherit multiple interfaces with a colon (:), separated by commas

  • A class can implement an interface while inheriting from another class, and both the inherited class and the implemented interface can be followed by a colon (:)

Commonly used class

  • A nested class is a class that can be nested within another class and cannot access the members of an external class

  • An inner class is a member that can be marked with inner so that it can access the outer class

  • In Java, a class defined inside another class is called a member inner class, or a static inner class if the member inner class is modified static. All members of an external class can be accessed from a member inner class in Java

  • In Kotlin, if you define a class inside another class without any modifier, that class defaults to being a nested class or, with inner modifier, an inner class. The inner class in Kotlin can access variables in the outer class, but the nested class cannot

  • Enumeration classes are preceded by the enum keyword

  • A sealed class is an extension of an enumerated class, that is, a collection of values of an enumerated type. It must be specified with the sealed keyword. Subclasses can only be defined inside the sealed class or in the same file

  • Sealed classes are good for countable subclasses, while enumerated classes are good for countable instances

  • Data classes are marked with the keyword data

  • The main constructor of a data class takes at least one argument. If you need a constructor that takes no arguments, you can set all the arguments in the constructor to default values.

  • Arguments passed in the primary constructor of a data class must be qualified with val or var.

  • Data classes cannot be modified with the keywords abstract, open, sealed, or inner.

  • Prior to Kotlin 1.1, data classes could only implement interfaces. After 1.1, data classes could inherit from other classes.

  • The compiler can automatically generate common methods like equals(), hashCode(), toString(), componentN(), copy(), and so on, and these methods can also be customized

  • Singleton mode is completed through the object keyword, the class modified by Object is a singleton class, singleton class in the program has only one instance

  • Companion objects are defined by the “Companion” keyword. Since there is only one companion object in each class, you may not specify the name of the companion object, and it may be shared by other objects

  • Named: The call method is class name. Associated object name. Member name or class name. Member name”
  • No name: The call method is class name. Companion. Member name or class name. Member name

Delegate use and exception handling

  • Delegates are implemented with the by keyword

  • Attribute delegation means that an attribute value of a class is not directly defined in the class, but delegated to a proxy class, so as to achieve unified management of the attributes of the class

  • Note:

The setValue() and getValue() methods must be preceded by the operator keyword; The return type of the getValue() method must be the same as the delegate property or a subclass of it; If the delegate property is read-only, that is, of type Val, then the delegate class needs to implement the getValue() method. If the delegate property is mutable, that is, of type var, then the delegate class needs to implement the getValue() and setValue() methods.

  • Lazy loading is identified by the “by lazy” keyword. Variables are required to be declared val, which is an immutable variable. Lazy loading is a form of delegation.

  • A lazy-loaded variable outputs the entire contents of the code block the first time it is initialized, and then only the contents of the last line of code are output when the variable is called

  • ArithmeticException, ClassNotFoundException, ArrayIndexOutOfBoundsException, such as IllegalArgumentException exception classes are inherited Java. Lang. Throwable class

  • Exception catching is usually done using try… Catch statement

  • Try catch Adds a finally statement. Statements in finally are executed. It is often used to do things that must be done, such as releasing system resources. Note that when a system.exit (0) statement is executed ina catch block, the block in finally does not execute. System.exit (0) exits the current program. After exiting the current program, no more code can be executed

  • Unlike Java, try… The catch… In finally, a try expression is an expression that can return a value. The return value of a try expression is the result of the last expression in the try block or the last expression in any catch block. What is in the finally block does not affect the result of the expression.

  • In the try… The catch… In a finally statement, there may be multiple catch blocks or no catch blocks. A finally block may be omitted, but at least one catch and finally block must exist.

  • Nothing is an uninhabited type, which means that no object of type Nothing is produced while the program is running. This type is used to flag a function that never returns data


Once studied Kotlin notes, transplantation.