Abstract class interface inner class

abstract class

  • The abstract modifier can be used to modify a method or a class. If a method is modified, the method is an abstract method; If you modify a class, it is an abstract class.
  • Abstract classes can have no abstract methods, but classes with abstract methods must be declared as abstract classes.
  • Abstract classes cannot use the new keyword to create objects. It is used to let subclasses inherit.
  • Abstract methods have only method declarations and no method implementations. They are used to implement subclasses.
  • If a subclass inherits an abstract class, it must implement an abstract method that the abstract class does not implement, otherwise the subclass must also be declared as an abstract class.
package com.oop.demo08;

//Abstract abstract class: class extensions: single inheritance ~ (interfaces can inherit multiple)
public abstract class Action {

    //Constraint ~ someone helped us achieve it~
    //Abstract, abstract method, only method name, no method implementation!
    public abstract void doSomething();

}
package com.oop.demo08;

//All methods of an abstract class that inherit its subclasses must implement its methods ~ unless the subclasses are also abstract and implemented by subclasses
public class A extends Action{
    @Override
    public void doSomething() {

    }
}
  1. new is not an abstract class, it can only be implemented by subclasses; Constraints!
  2. Ordinary methods can be written in abstract classes~
  3. Abstract methods must be in abstract classes~

Abstract: Constraints~

Significance of existence: abstract it to improve development efficiency

Interface

  • Common class: only concrete implementation
  • Abstract classes: concrete implementations and specifications (abstract methods) are available!
  • Interface: only specification! I can't write professional constraints! Separation of constraints and Implementation: interface oriented programming
  • An interface is a specification. It defines a set of rules, which embodies the idea of "if you are... You must be able to..." in the real world. If you are an angel, you must be able to fly. If you are a car, you must be able to run. If you are a good man, you must kill the bad man; If you are a bad person, you must bully the good person.
  • The essence of interface is contract, just like the law between us. After making it, everyone abides by it.
  • Proficient in OO is the abstraction of objects. The interface can best reflect this. Why we discuss design patterns only for languages with abstract ability (such as C + +, java, c# etc.) is because what design patterns study is actually how to abstract reasonably.

The keyword for declaring a class is class, and the keyword for declaring an interface is interface

package com.oop.demo09;

//Abstract thinking ~ Java

//The keyword defined by interface and the interface need to have an implementation class
public interface UserService {

    //Constant ~ public static final
    int AGE = 99;

    //All definitions in the interface are actually Abstract public abstract s
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);
}
package com.oop.demo09;

public interface TimeService {
    void timer();
}
package com.oop.demo09;

//Abstract class: Extensions~
//Class can implement the implementation interface
//If you implement the class of the interface, you need to rewrite the methods in the interface

//Multi inheritance ~ realize multi inheritance by using interface~
public class UserServiceImpl implements UserService,TimeService{
    @Override
    public void add(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void update(String name) {

    }

    @Override
    public void query(String name) {

    }

    @Override
    public void timer() {

    }
}

Function (summary):

  1. constraint
  2. Define some methods for different people to implement~
  3. public abstract
  4. public static final
  5. The interface cannot be instantiated. There is no constructor in the interface
  6. Implementation can implement multiple interfaces
  7. You must override the methods in the interface~

Inner class

  • An internal class is to define another class within a class. For example, if a class B is defined in class A, class B is called an internal class relative to class A, and class A is an external class relative to class B
    1. Member inner class
package com.oop.demo10;

public class Outer {

    private int id = 10;
    public void out(){
        System.out.println("This is the method of an external class");
    }

    public class Inner{
        public void in(){
            System.out.println("This is the method of an external class");
        }

        //Get the private properties of the external class~
        public void getId(){
            System.out.println(id);
        }
    }
}
package com.oop;

import com.oop.demo10.Outer;

public class Application {

    public static void main(String[] args) {
        Outer outer = new Outer();
        //Instantiate the inner class through this outer class~
        Outer.Inner inner = outer.new Inner();
        inner.getId();
    }

}
    1. Static inner class (plus static)
    1. Local inner class
    package com.oop.demo10;
    
    public class Outer {
        
        //Local inner class
        public void method(){
            class Inner{
                public void in(){
                    
                }
            }
        }
    }
    
    1. Anonymous Inner Class
package com.oop.demo10;

public class Test {
    public static void main(String[] args) {
        //An initialization class without a name does not need to save an instance to a variable~
        new Apple().eat();

        UserService userService = new UserService(){
            @Override
            public void hello() {

            }
        };
    }
}

class Apple{
    public void eat(){
        System.out.println("1");
    }
}

interface UserService{
    void hello();
}

Other supplements

//A java class can have multiple class classes, but only one public class
class A{
    public static void main(String[] args) {

    }
}

Origin of notes - crazy teacher

Posted on Sun, 28 Nov 2021 05:23:06 -0500 by shane18