Abstract class, interface, internal class

**

abstract class

**
An abstract method is one that has no'{}'and is preceded by abstract
Classes with abstract methods are abstract classes, which are declared using the abstract keyword.
The code is as follows:

abstract class A{//Define an abstract class
    public void fun(){//General method
        System.out.println("Method with method body");
    }
    public abstract void print();//Abstract method, no method body, decorated with abstract keyword

}

Rules for the use of abstract classes:
1. When a subclass inherits an abstract class, all abstract methods in the abstract class must be overridden, and only one abstract class can be inherited by a subclass
2. Abstract classes cannot be instantiated directly, they must be transformed upwards.The code is as follows

abstract class A{//Define an abstract class
public  A(){
	System.out.println("A Construction method");
}
    public void fun(){//General method
        System.out.println("Method with method body");
    }

    public abstract void print();//Abstract method, no method body, decorated with abstract keyword

}
//single inheritance
class B extends A{//Class B is a subclass of abstract class and a common class
public  B(){
	System.out.println("B Construction method");
}
    @Override
    public void print() {//Force override
        System.out.println("Hello World !");
    }

}
public class TestDemo {

    public static void main(String[] args) {
        A a = new B();//Upward Transition

        a.fun();//Methods overridden by subclasses
    }
}

Output results:
A Construction Method
B Construction Method

3. At the same time, as in the code above, the parent class's construction method is executed first, and the child class's construction method is executed as well as inheritance when the child class object is instantiated.
Interface
1. Definition
Interface, an abstract type in the JAVA programming language, is a collection of abstract methods.Interfaces are usually declared as interfacets.A class inherits the abstract methods of an interface by inheriting the methods of the interface, only the abstract methods and constants

interface A{//Define an interface
   	 public static final String MSG = "hello";//Global Constants
   	 public abstract void print();//Abstract method
}

Be careful;
1. Variables in interfaces can only be "public static final" so they are also constants
2. Methods in interfaces can only be decorated with "public abstract"
2. Use

interface A{//Define an interface A

    public static final String MSG = "hello";//Global Constants

    public abstract void print();//Abstract method
}

interface B{//Define an interface B

    public abstract void get();
}

class X implements A,B{//Class X implements both A and B interfaces

    @Override
    public void print() {
        System.out.println("Interface A The abstract method of print()");
    }

    @Override
    public void get() {
        System.out.println("Interface B The abstract method of get()");
    }

}

public class TestDemo {

    public static void main(String[] args){

        X x = new X();//Instantiate Subclass Object
        A a = x;//Upward Transition
        B b = x;//Upward Transition

        a.print();
        b.get();
    }

}

The summary is as follows:
(1) Interfaces must have subclasses, but at this time a subclass can use the implements keyword to implement multiple interfaces;
(2) Subclasses of interfaces (if not abstract classes), then all abstract methods in interfaces must be overridden;
(3) Objects of the interface can be instantiated by the upward transition of subclass objects.

Internal Class
InnerClass is an internal class as shown in the following code

public class OuterClass {
    private String outerName;
    private int outerAge;
    public class InnerClass{
        private String innerName;
        private int innerAge;
    //Advantage:
1.Internal classes provide better encapsulation, only external classes can access internal classes
2.Internal classes can inherit an interface independently, independent of whether external classes inherit an interface or not 
3.The attributes and methods in an internal class cannot be directly accessed even by an external class. Instead, an internal class can directly access the attributes and methods of an external class, even if private lic class OuterClass {
    public void display(){
        //External classes access internal class elements, which need to be accessed through internal class references
        InnerClass innerClass=new InnerClass();
        innerClass.innerDisplay();
    }
    public class InnerClass{//Internal Class
        public void innerDisplay(){
            System.out.println("I am inner class");
        }
    }
    public static void main(String[] args) {
        OuterClass outerClass=new OuterClass();
        outerClass.display();
    }
}

Similarly, there are only two ways to create objects of internal classes:
The first is outside the external class: you must first create an external class before creating an internal class
OuterClass outerClass = new OuterClass();
OuterClass.InnerClass innerClass1 = outerClass.new InnerClass();
Second: in external classes
InnerClass innerClass=new InnerClass();
innerClass.innerDisplay();

Tags: Java Programming

Posted on Mon, 09 Sep 2019 20:06:24 -0700 by baw