## javaSE Part 3: Object-oriented

abstract class

An abstract class is a template pattern. Abstract classes provide a general template for all subclasses. Subclasses can be extended on the basis of this template. Through Abstract classes, the randomness of subclass design can be avoided.

  • Static methods cannot be overridden after inheritance.

  • Abstract classes cannot create instances, but constructors are provided to allow subclasses to create instances.

  • Abstraction can have general methods, member variables.

  • Abstract methods cannot be modified by private, static, final, and must be modified by abstract.

Interface

Composition of jdk1.8 post-interface


//Interfaces have other interfaces in the form of inheritance, which can be inherited more.

public interface Output extends Output1 , Output2

{

    int MAX_SIZE = 50 ; //public static final is used as the default modifier in the interface.



    /* No constructors in interfaces */



    // The common method in the interface defaults to abstract method, and the method defaults to public abstract modification.

    //When a subclass implements an interface as an excuse, the method can be repeated, and one can be implemented.

    public void out () ;

    void getData ( String msg ) ;



    // jdk1.8 adds default modifier and default modifier uses public modifier by default.

    default void test ()

    {

        System.out.println ( "Default test()Method" ) ;

    }

    //The Static method added by jdk1.8 defines the static method in the interface and modifies the default permission public with the static method.

    static String staticTest()

    {

        return "Class methods in interfaces" ;

    }

    //So jdk1.8 interface can add main method as the entry of the program...

}


jdk1.8 interface add default method and subclass realize multi-interface method duplication problem
  • Repetition of static methods: Subclasses cannot call static methods of interfaces.

  • Default method duplication problem

public class TestInterface implements IA , IB{

    //At this point, the subclass must specify a default method of the interface for rewriting

    @Override

    public void say() {

        IA.super.say();

    }

}

interface IA{

    default void say() {System.out.println("this is A......");}

}

interface IB{

    default void say() {System.out.println("this is B......");  }

}


Common interfaces Comparable interface and comparator interface
  • Both comparator and Comparable interfaces provide a method for comparison.

  • Implementation of Comparable Method

//Generics specify the elements of comparison.

public class Student implements Comparable<Student> {



    private String name;

    private int age;

    private double score;



    //.... Ellipse constructor getter and setter methods

    //The implementation method can customize the comparison mode and return a value of type int.

    @Override

    public int compareTo(Student o) {

        return this.getAge() - o.getAge;

    }

}
  • comparator
//critical code

    //ArrayUtils. sort (Student [] obj, Comparator c); pass in the target array, sort with the Comparator object, or implement the comparison method of the interface in the form of inheritance

    ArrayUtils.sort(s, new Comparator<Student>() {

            @Override

            public int compare(Student o1, Student o2) {

                return o1.getAge() - o2.getAge() ;

            }

    }) ;


False Iterable iterator Iterable Iterator Iterative Interface
  • The Iterable iterator interface is used to help custom collection classes complete the forEast loop
//Simple Queue Implementation Reference Algorithms 4th Edition Lecture.

package chapter.one.quarter.three;



import java.util.Iterator;

//Implementing Iterable < T > Interface by Customizing Collection Classes

public class Queue <Item> implements Iterable<Item>{



    private Node first ;

    private Node last ;

    private int N ;

    private class Node{

        Item item ;

        Node next ;

    }

    public boolean isEmpty() {  return first == null ;}

    public int size()        {  return N ;            }

    //Adding and deleting code without exception mechanism is incomplete.

    public void enqueue(Item item){

        Node oldlast = last ;

        last = new Node() ;

        last.item = item ;

        last.next = null ;

        if(isEmpty()) first = last ;

        else          oldlast.next = last ;

        N++ ;

    }

    public Item dequeue(){

        Item item = first.item ;

        first = first.next ;

        if(isEmpty()) last = null ;

        N-- ;

        return item ;

    }

    //Override iterator(), and rewrite an object that implements the Iterator interface

    @Override

    public Iterator<Item> iterator() { return new ListIterator() ;}

    //Implementing Iterator Interface

    private class ListIterator implements Iterator<Item>{

        private Node current = first ;

        @Override

        public boolean hasNext() { return first != null ;}

        @Override

        public Item next() {

            Item item = current.item ;

            current = current.next ;

            return item ;

        }

        @Override

        public void remove() {/* This example does not need to be deleted, so it is not written.*/}

    }

}


Other

Internal Class Anonymous Class

  • Grammatical Notes for Internal Classes:
public class Test {



    private String name = "A''";

    public class Inner {

        String name = "B''";

        public void show() {

            //In this case, we use external class pre-positioning to distinguish.

            System.out.println("In external classes name: " + Test.this.name );

        }

    }

    public static void main(String[] args) {

        //How to create objects of internal classes

        Test o = new Test() ;

        Inner inn = o.new Inner() ;

    }

}


Anonymous Inner Class

  • When the reference parameter of a method is an interface or a class, it can be overridden with an anonymous inner class.
interface Product {

    public String getName();

}



public class AnonymousTest {

    public void test(Product p) {

        System.out.println("Buy one" + p.getName());

    }

    public static void main(String[] args) {

        AnonymousTest ta = new AnonymousTest();

        // When calling the test () method, you need to pass in a product parameter

        // Instances of internal classes.

        ta.test(new Product() {

            public String getName() {

                return "ds";

            }

        });

    }

}


enumeration

//Enumeration implements interfaces

public enum TestEnum implements A {

    //Enumeration values are placed on the first line, separated by commas.

    MALE("male"){

        public void info(){

            System.out.println("s ss");

        }

    }

    //When you want to instantiate enumerations, you need to add attributes and constructors

    private final String name;

    private TestEnum(String name) {

        this.name = name;

    }

    public String getName() {

        return this.name;

    }

    //When interfaces are inherited, enumeration must be instantiated and implemented. Or implement the method here.

    public abstract void info() ;

}

interface A

{

    void info() ;

}


Keywords: Java

Added by barffyman on Wed, 17 Jul 2019 23:02:22 +0300