[Crazy God said] JavaSE study notes object-oriented

The contents of the notes are written in the order of the videos that Mad God said;
Mad God said video homepage
JAVA learning route
Click here to see more JavaSE notes

Java object-oriented

  • The core idea of ​​​​Java is object-oriented programming (OOP)

1. Process-oriented & object-oriented

  • process-oriented thinking
    • The steps are clear and simple, what to do in the first step, what to do in the second step...
    • Facing the process is suitable for dealing with some relatively simple problems
  • object-oriented thinking
    • Birds of a feather flock together, the thinking mode of classification, when thinking about problems, first of all, which classifications are needed to solve the problem, and then think about these classifications separately. Finally, process-oriented thinking is carried out on the details under a certain category.
    • Object-oriented is suitable for dealing with complex problems, and it is suitable for dealing with problems that require multi-person cooperation!
  • For describing complex things, in order to grasp it from a macro perspective and analyze it reasonably as a whole, we need to use object-oriented thinking to analyze the entire system. However, when it comes to specific micro-operations, a process-oriented approach is still required.

2. What is object-oriented

  • Object-Oriented Programming (OOP)
  • The essence of object-oriented programming is to organize code in the form of classes and organize (encapsulate) data in the form of objects.
  • Abstraction: programming ideas! Keep learning, Mose suddenly enlightened! Practice more, test the ideas in your brain more! Real knowledge comes from practice~
  • Three major features:
    • encapsulation
    • inherit
    • polymorphism
  • From an epistemological point of view, there are objects first and then classes.
    • Object: is a specific thing.
    • Class: It is abstract, and it is an abstraction of objects.
  • From the perspective of code operation, there are classes first and then objects. Classes are templates for objects.

2.1. Definition of review method

  • method definition
    • Modifier
    • return type
    • break: jump out of the switch and end the loop
    • return: end the method, return the result, this result can be empty
    • Method name: Pay attention to the specification and it will be OK, see the name and know the meaning
    • Parameter list: (parameter type, parameter name)...
    • Exception thrown: doubt, explained later
package com.oop.demo01;

import java.io.IOException;

//Demo01 class modification, this is a class
public class Demo01 {
    //There is only one main method in each program
    public static void main(String[] args) {

    }

    /*
    Method definition:
    ===========================================================
    modifier return value type method name (parameter) {
        method body
        return  return value;
    }
     */
    //return ends the method and returns the result, which can be empty
    public String sayHello(){
        return "Hello";
    }
    public int max(int a, int b) {
        return a>=b ? a : b;	//ternary operator
    }

    //Throw an exception
    //Currently seen exceptions: ArrayIndexOutOfBounds
    public void readFile(String file) throws IOException {

    }
}

2.2. Review method calls

  • static method
  • non-static method
package com.oop.demo01;

public class Demo02 {
    public static void main(String[] args) {
        //Static methods can be called directly by class name. method name
        //Because the static method is loaded with the startup of the program
        Student.student1();

        //Non-static methods need to instantiate this class to call
        //object type object name = object value
        //call method one
        Student student = new Student();
        int i = student.student2();
        System.out.println(i);
        //call method two
        int j = new Student().student2();
        System.out.println(j);
    }
}
package com.oop.demo01;

public class Student {
    //static method
    public static void student1() {
        System.out.println("static");
    }

    //non-static method
    public int student2() {
        System.out.println("non static");
        return 1;
    }
}
  • formal parameters and actual parameters
package com.oop.demo01;

public class Demo03 {
    public static void main(String[] args) {
        //The types of actual parameters and formal parameters should correspond
        int add = Demo03.add(2, 3);
        System.out.println(add);
    }

    public static int add(int a, int b) {
        return a + b;
    }
}
  • pass by value and pass by reference
package com.oop.demo01;

public class Demo04 {
    //pass by value
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a);
        Demo04.change(a);
        System.out.println(a);   //a=1
    }

    public static void change(int a) {
        a = 10;
    }
}
package com.oop.demo01;

//Passing by reference: object, essence or value passing
public class Demo05 {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);    //null

        Demo05.change(person);
        System.out.println(person.name);    //wukunyan
    }

    public static void change(Person person) {
        //person is an object: it points to ----> Person person = new Person(); This is a specific object, which can change attributes!
        person.name = "wukunyan";
    }
}
//Define a Person class with one attribute: name
class Person{
    String name;
}
  • this keyword

3. The relationship between classes and objects

  • A class is an abstract data type, which is an overall description and definition of a certain type of thing, but it cannot represent a specific thing.
    • Animals, plants, mobile phones, computers...
    • Person class, Per class, Car class, etc. These classes are used to describe and define the characteristics and behaviors that a certain type of specific things should have
  • Objects are concrete instances of abstract concepts
    • Zhang San is a concrete example of a human being, and the prosperity of Zhang San's family is a concrete example of enough.
    • It is a concrete example, not an abstract concept, that can embody characteristics and functions.

We can convert this idea into code implementation!

3.1. Create and initialize objects

  • Objects are created using the new keyword
package com.oop.Demo02;

//A project should have only one main method
public class Application {
    public static void main(String[] args) {

        //Class: abstract, needs to be instantiated
        Student student = new Student();    //new can instantiate the class, and it will return an object of its own after instantiation
        //The student object is a concrete instance of the Student class!

        Student xiaoming = new Student();
        Student xiaohong = new Student();   //The same class can produce different objects, but they all have common characteristics

        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);

        xiaoming.name = "Xiao Wu";
        xiaoming.age = 13;

        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);

    }
}
package com.oop.Demo02;

//student class
public class Student {  //A class has only properties and methods
    //property: field
    String name;    //default null
    int age;    //Default 0

    //method
    public void study() {
        System.out.println(this.name + "studying");  //this. Indicates the variables in the current object
    }
}
  • When using the new keyword to create, in addition to allocating memory space, the created object will be initialized by default to call the constructor in the class
  • The constructor in a class is also called a constructor, which must be called when creating an object. And the constructor has the following two characteristics:
    • 1. Must have the same name as the class
    • 2. There must be no return type, and void cannot be written
  • The constructor must master
package com.oop.Demo02;

public class Application {
    public static void main(String[] args) {

        Person person = new Person();
        System.out.println(person.name);

        Person wucanbin = new Person("wucanbin");
        System.out.println(wucanbin.name);
    }
}
package com.oop.Demo02;

public class Person {
    //Even if a class does not write anything, there will be a method, which is a constructor

    String name;

    //Explicitly define the constructor
    public Person() {
        //This is a parameterless constructor, the constructor can be instantiated and given an initial value
        this.name = "Canbin";
    }

    //Argumentary construction: Once a parametric construction is defined, no argument must be explicitly defined
    public Person(String name) {
        this.name = name;
    }

    /*
    IDE You can quickly create a constructor with parameters and without parameters:
    Alt + Insert

    Constructor:
        1,same as class name
        2,no return value
    effect:
        1,new The essence is to call the constructor
        2,initialize the value of the object
    be careful:
        1,After defining the parameterized construction, if you want to use the no-argument construction, you must explicitly define a no-argument construction method
     */
}

3.2. Create object memory analysis

package com.oop.Demo03;

public class Application {
    public static void main(String[] args) {
        Pet dog = new Pet();
        dog.name = "prosperity";
        dog.age = 3;
        System.out.println(dog.name);
        System.out.println(dog.age);
        dog.sout();

        Pet cat = new Pet();
    }
}
package com.oop.Demo03;

public class Pet {
    String name;
    int age;

    public void sout(){
        System.out.println("yelled");
    }
}

3.3, simple summary class and object

  • Classes and Objects:
    • A class is a template: abstraction
    • object is a concrete example
  • method:
    • Define, call!
  • object reference
    • Reference type: Primitive type (8)
      • Objects are manipulated by reference: stack —> heap
  • Attribute: Field Field member variable
    • Default initialization:
      • Number: 0 0.0
      • char: u0000
      • boolean: false
      • References: null
    • Modifier Attribute Type Attribute Name = Attribute Value
  • Object creation and use
    • Objects must be created using the new keyword, and there must be a constructor
      • Person wucanbin = new Person();
    • properties of the object
      • wucanbin.name
    • object method
      • wucanbin.sleep();
  • kind:
    • static property property
    • Dynamic Behavior Methods

4. Packaging

  • What should be exposed, what should be hidden
    • Our program design should pursue **"high cohesion, low coupling"**. High cohesion means that the internal data operation details of the class are completed by ourselves, and no external interference is allowed; low coupling means that only a small number of methods are exposed for external use.
  • Encapsulation (hiding of data)
    • In general, direct access to the actual representation of data in an object should be prohibited, and should be accessed through the operational interface, which is called information hiding.
  • It is enough to remember this sentence: attribute private, get/set
  • Example:
package com.oop.Demo04;

public class Application {
    public static void main(String[] args) {
        /*
        Why use get/set encapsulation?
            1,Improve program security and protect data
            2,hide the implementation details of the code
            3,unified interface
            4,System maintainability increased
         */
        Student student = new Student();

        student.setName("wucanbin");
        System.out.println(student.getName());

        student.setAge(999);    //illegal
        System.out.println(student.getAge());
    }
}
package com.oop.Demo04;

//Student class private private
public class Student {

    //attribute private
    private String name;    //name
    private int age;    //age
    private int id;     //student ID

    //Provide some methods that can manipulate this property
    //Provide some public get and set methods

    //get get this data
    public String getName(){
        return this.name;
    }
    //set sets the value for this data
    public void setName(String name) {
        this.name = name;
    }

    //Alt + Insert shortcut to add get/set


    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age>=120 || age<0) {    //Avoid illegal incoming data and judge incoming data
            this.age = 3;
        }else {
            this.age = age;
        }
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

5. Inheritance

  • The essence of inheritance is the abstraction of a certain batch of classes, so as to better model the real world.
  • extends means "extended". A subclass is an extension of a parent class.
  • Classes in Java only have single inheritance, not multiple inheritance!
  • Inheritance is a relationship between classes and classes. In addition, the relationship between classes and classes includes dependency, composition, aggregation, etc.
  • There are two classes in the inheritance relationship, one is the subclass (derived class) and the other is the parent class (base class). A subclass inherits from a parent class, represented by the keyword extends.
  • In a sense, there should be an "is a" relationship between the subclass and the parent class.
  • A class modified by final cannot be inherited
  • object class
  • Example:
package com.oop.Demo05;

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.sout();
        System.out.println(student.getMoney());
    }
}
package com.oop.Demo05;

//  people
//  base class, parent class
//  If you want the subclass to inherit the things of the parent class, you must use public (public) instead of private (private). In general, methods are public and properties are private.
//  Ctrl + H can display the inheritance relationship of the current class
//  In JAVA, all classes inherit directly or indirectly from the Object class by default.

public class Person /*extends Object*/ {
    private int money = 1_0000_0000;    //A new feature of JDK 7, the underscore in the number will not be displayed.

    public void sout(){
        System.out.println("parent said something");
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}
package com.oop.Demo05;

//  student is person
//  derived class, subclass
//  The subclass inherits the parent class and will have all the methods of the parent class
public class Student extends Person{    //extends inheritance

}

5.1,super

  • super attention points:

    • super calls the constructor of the parent class, which must be the first line of the constructor
    • super must only appear in subclass methods or constructors
    • super and this cannot call the constructor at the same time
  • Compare this:

    • The objects represented are different:
      • this: the object of the caller itself
      • super: represents a reference to the parent class object
    • premise:
      • this: Can be used without inheritance
      • super: can only be used under inheritance conditions
    • Construction method:
      • this(); This class construct
      • super(); parent class construction
  • Example:

    package com.oop.Demo05;
    
    public class Application {
        public static void main(String[] args) {
            Student student = new Student();    //new will execute the constructor of the class
    
            student.sout("Wu Canbin");
            System.out.println("==============================================");
            student.text();
        }
    }
    
    package com.oop.Demo05;
    
    public class Person {
        public Person() {
            System.out.println("Person The no-argument constructor executes");
        }
    
        protected String name = "wucanbin"; //protected protected
    
        public void print() {   //If the subclass wants to call this method, the modifier of the method cannot be private, and the private one cannot be called by the subclass
            System.out.println("Person");
        }
    }
    
    package com.oop.Demo05;
    
    public class Student extends Person{
    
        public Student() {
            //Code Hidden: By default, the no-argument constructor of the parent class is called
            //super();
            //Calling the constructor of the parent class must be in the first line of the constructor of the subclass, that is, super(); cannot be placed later.
            //If the parent class has no no-argument construction and only has argument construction, then write super( parameter);
            System.out.println("Student The subclass constructor executes");
        }
    
        public String name = "wukunyan";
    
        public void sout(String name) {
            System.out.println(name);       //The name of the incoming formal parameter
            System.out.println(this.name);  //the name of the current class
            System.out.println(super.name); //the name of the parent class
        }
    
        public void print() {
            System.out.println("Student");
        }
    
        public void text() {
            print();    //Call the method of the current class by default
            this.print();   //Call the method of the current class
            super.print();  //Call the method of the parent class
        }
    }
    

5.2, method rewriting

  • Premise: There needs to be an inheritance relationship, and the subclass overrides the method of the parent class!

  • Features:

    • method names must be the same
    • The argument lists must be identical
    • Modifiers: the range can be expanded but not narrowed
      • public > protected > default > private
    • Exception thrown: range can be narrowed, but not widened
      • Exception > ClassNotFoundException (class not found exception)
  • Summarize:

    • The method of the subclass must be consistent with the parent class, but the method body is different!
  • Why do you need to rewrite?

    • The functional subclass of the parent class does not necessarily need, or not necessarily satisfy!
  • hot key:

    • Alt + Insert then select override (override method)
  • Methods that cannot be overridden:

    • static static method, belongs to the class, it does not belong to the instance
    • final constant
    • private private method
  • Example:

package com.oop.Demo06;

public class Application {
    public static void main(String[] args) {
        //There is a big difference between static methods and non-static methods
        //Static method: the method call is only related to the data type defined on the left
        Son a = new Son();
        a.test();   //Son

        //The reference of the parent class points to the subclass
        Father b = new Son();
        b.test();   //Father

        //Non-static methods: override
        Son c = new Son();
        c.sort();
        Father d = new Son();// The subclass overrides the method of the parent class
        d.sort();
    }
}
package com.oop.Demo06;

//Rewriting is the rewriting of methods, and has nothing to do with attributes
public class Father {
    public static void test() {
        System.out.println("Father => test");
    }

    public void sort() {
        System.out.println("Son");
    }
}
package com.oop.Demo06;

//inherit
public class Son extends Father{
    public static void test() {
        System.out.println("Son => test");
    }

    //Override
    @Override   //Annotations: Functional annotations!
    public void sort() {//The scope of the subclass modifier cannot be smaller than that of the parent class
        System.out.println("Son");
    }
}

6. Polymorphism

  • Notes on polymorphism:
    • Polymorphism is the polymorphism of methods, but no polymorphism of properties
    • The parent class and the subclass must be connected Type conversion exception (ClassCastException)
    • Existence conditions:
      • Inheritance
      • Subclass overrides parent class method
      • The parent class reference points to the child class object Father f1 = new Son();
  • Polymorphism means that the same method can adopt many different behavior methods according to the different sending objects
  • The actual type of an object is determined, but there are many types of references that can point to the object (parent class, related class)
package com.oop;

import com.oop.Demo07.Person;
import com.oop.Demo07.Student;

public class Application {
    public static void main(String[] args) {
        //The actual type of an object is determined
        //new Student();
        //new Person();

        //But the reference type pointed to is not sure
        //The methods that Student can call are all their own or inherited from the parent class!
        Student s1 = new Student();
        //The supertype of Person can point to subclasses, but cannot call methods unique to subclasses!
        Person s2 = new Student();  //Parent class references point to subclasses
        Object s3 = new Student();

        //Which methods an object can execute depends mainly on the type on the left side of the object, and has little to do with the right side!
        s2.run();   //If the subclass overrides the method of the parent class, execute the method of the subclass
        //s2.eat(); //Because the reference type of s2 is the parent class, there is no such method in the parent class, so an error is reported. When the parent class has this method, the parent class method is executed if the subclass does not override it. Write then execute subclass method
        ((Student)s2).eat();//call eat() after casting;

        s1.run();
        s1.eat();
    }
package com.oop.Demo07;

public class Person {
    public void run() {
        System.out.println("run");
    }
}
package com.oop.Demo07;

public class Student extends Person{
    @Override
    public void run() {
        System.out.println("son");
    }

    public void eat(){
        System.out.println("eat");
    }
}
  • instanceof judges whether there is a relationship between them
  • type conversion
    • Transform the subclass into the parent class and transform upwards; direct assignment
    • Transform the parent class into a subclass and downcast; mandatory conversion
    • Advantages: Convenient method call, reduce duplication of code!
package com.oop;

import com.oop.Demo07.Person;
import com.oop.Demo07.Student;
import com.oop.Demo07.Teacher;

public class Application {
    public static void main(String[] args) {
        //System.out.println(X instanceof Y); //Whether it can be compiled or not mainly depends on whether X and Y have a parent-child relationship

        Object object = new Student();
        System.out.println(object instanceof Student);//ture
        System.out.println(object instanceof Person);//ture
        System.out.println(object instanceof Object);//ture
        System.out.println(object instanceof Teacher);//false
        System.out.println(object instanceof String);//false
        System.out.println("================================================");

        Person person = new Student();
        System.out.println(person instanceof Student);//ture
        System.out.println(person instanceof Person);//ture
        System.out.println(person instanceof Object);//ture
        System.out.println(person instanceof Teacher);//false
        //System.out.println(person instanceof String); //Compilation error, because there is no parent-child relationship
        System.out.println("================================================");

        Student student = new Student();
        System.out.println(student instanceof Student);//ture
        System.out.println(student instanceof Person);//ture
        System.out.println(student instanceof Object);//ture
        //System.out.println(student instanceof Teacher);//Compilation error, because there is no parent-child relationship
        //System.out.println(student instanceof String); //Compilation error, because there is no parent-child relationship


        //Conversion between types: parent child
        //high low
        Person s1 = new Student();
        //Convert the s1 object to the Student type, and then you can use the methods in the Student
        ((Student)s1).go();//Mandatory type conversion, the parent class goes to the subclass,

        Student s2 = new Student();
        Person s3 = s2;//To convert a subclass to a parent class, you can directly assign and convert, but some methods of the subclass may be lost after the conversion
        s3.run();
        //s3.go(); An error will be reported here, because the go(); method is lost after conversion

    }
}
package com.oop.Demo07;

public class Person {
    public void run() {
        System.out.println("run");
    }
}
package com.oop.Demo07;

public class Student extends Person{
    public void go() {
        System.out.println("go");
    }
}
package com.oop.Demo07;

public class Teacher extends Person{
}

7. static keyword

package com.oop.Demo08;

public class Person {
    //  Second, it can be used to assign initial values
    {
        System.out.println("anonymous code block");
    }
    //  First, no matter how many new after starting the class, it will only be executed once
    static {
        System.out.println("static block");
    }
    //  third
    public Person(){
        System.out.println("Construction method");
    }

    public static void main(String[] args) {
        Person person = new Person();//View the output results to judge the order of execution
        System.out.println("===========================================");
        Person person1 = new Person();
    }
}
Output result:
    static block
	anonymous code block
	Construction method
	===========================================
	anonymous code block
	Construction method

	process has ended,exit code 0
  • static import package

    package com.oop.Demo08;
    
    //Static import package~
    import static java.lang.Math.random;
    
    public class Test {
        public static void main(String[] args) {
            System.out.println(Math.random());
            System.out.println(random());   //You can statically import the package first to reduce code input
        }
    }
    

8. Abstract class

  • Features:

    • The abstract class cannot be new, it can only be realized by subclasses, and the abstract class is only a constraint.
    • Ordinary methods can be written in abstract classes
    • Abstract methods must be written in abstract classes
    • Abstraction of abstraction: is the role of constraints
    • An abstract class does not necessarily have abstract methods
  • Questions to think about:

    • Does an abstract class have a constructor when it is new?
      • There is a constructor, even if you don't provide any constructor, the compiler will add a default no-argument constructor to the abstract class, without which your subclass won't compile because the first statement in any constructor implicitly call super().
    • The meaning of the existence of abstract class?
      • Abstract to improve development efficiency
  • Example;

package com.oop.Demo09;

//abstract abstract class
public abstract class Action {

    //Constraints~someone help us achieve
    //abstract abstract method, only the method name, no method implementation
    public abstract void doSomethinh();
}
package com.oop.Demo09;

//A subclass that inherits an abstract class must implement its abstract method~ unless the subclass is also an abstract class
//Java classes are single-inherited, but interfaces can implement multiple inheritance
public class A extends Action{

    @Override
    public void doSomethinh() {
        //method implementation
    }
}

9. Interface

  • Ordinary class: only concrete implementation
  • Abstract class: both concrete implementation and specification (abstract method)!
  • Interfaces: only specifications! I can't write method professional constraints! Separation of Constraints and Implementation: Programming to Interfaces
  • An interface is a specification, which defines a set of rules and 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 guy, you have to kill and replace. If you're a replacement, you have to bully the good guys.
  • The essence of the interface is a contract, just like the laws in our world, everyone abides by it after it is formulated.
  • The essence of OO is the abstraction of objects, and the interface that best embodies this is the reason why we discuss design patterns only for languages ​​with abstract capabilities (such as: C++, java, C#, etc.), because the research of design patterns , in fact, is how to reasonably abstract.

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

  • Points to note about the interface:
    • constraint
    • Define some methods for different people to implement
    • public abstract default modification method
    • public static final default modifier constant
    • Interface cannot be instantiated, interface has no constructor
    • implements can implement multiple interfaces
    • To implement an interface, you must override the methods in the interface
package com.oop.Demo10;

/*
class can implement interface implements interface
 The class that implements the interface needs to rewrite the method in the interface~
Multiple Inheritance Using Interfaces to Implement Multiple Inheritance
 */
public class UserServiceImpl implements UserService,TimeService{
    @Override
    public void run(String name) {

    }

    @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 time() {

    }
}
package com.oop.Demo10;

//interface interface
//Interfaces need to have implementation classes
//class class
public interface UserService {
    //constant ~ public static final
    int AGE = 99;

    //All definitions in the interface are actually abstract public abstract
    public abstract void run(String name);  //The method defined in the interface can directly ignore the public abstract, and the default will be
    //Only need: return value type method name (parameter)
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);
}
package com.oop.Demo10;

public interface TimeService {
    void time();
}

10. Internal classes (optional)

  • An inner class is to define a class inside a class. For example, if a class B is defined in class A, then class B is called an inner class relative to class A, and class A is an outer class relative to class B.

  • member inner class

package com.oop.Demo11;

public class Application {

    public static void main(String[] args) {
        Outer outer = new Outer();
        //Instantiate the inner class through this outer class
        //Outer class. Inner class
        Outer.Inner inner = outer.new Inner();
        inner.getID();
    }
}
package com.oop.Demo11;

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

        //Get the private properties of the outer class
        public void getID(){
            System.out.println(id);
        }
    }
}
  • static inner class

It is to add static on the basis of the internal class of the above member, but it should be noted that static is loaded with the class, and the id cannot be obtained after the internal class is modified with static, unless the id is also modified with static.

  • anonymous inner class

    package com.oop.Demo11;
    
    public  class Outer {
    
        //local inner class
        public void method() {
            class Inner{
                public void in() {
    
                }
            }
        }
    }
    
    //There can be multiple classes in a java file, but only one public class
    class A {
    }
    
    package com.oop.Demo11;
    
    public class Test {
        public static void main(String[] args) {
            //There is no name to initialize the class, no need to save the instantiation in a variable~
            new Apple().eat();
            //When new an interface needs to implement the abstract method inside
            UserService userService = new UserService() {
                @Override
                public void dog() {
                    //method body
                }
            };
        }
    }
    class Apple{
        public void eat() {
            System.out.println("1");
        }
    }
    
    interface UserService{
        void dog();
    }
    
    

Tags: Java programming language

Posted by nanny79 on Fri, 30 Dec 2022 09:48:07 +0530