Saturday, July 23, 2022

Java

What are Inner Classes?

    An inner class is a class nested inside some outer class. You can define an (inner) class inside a (outer) class. The inner class has access to everything in the outer class. It's called like "OuterClass.InnerClass myInnerClassObject = new OuterClass.new InnerClass()". 

What are Abstract Classes?
    Abstract classes are classes where nothing contains a body or definition. The point is that subclasses will provide these. An analog to math is that an n-dimensional hypercube is really an abstract class of squares/cubes; it has a theoretical structure with sides, faces, a volume etc, but that is all it is, a theoretical structure. The subclass of n-dimensional hypercubes where n=3 is a "concrete" class which we can provide definitions to and instantiate in real-life - V=s^3, SA=6s^2, etc. (Of course we can assign such formulae to n-D cubes in actual math but I am pretending that we don't for the sake of illustration)

What are Interfaces?
    Interfaces are abstract classes EXCEPT (as per the Java docs): 
        all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public
        you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces. 

What are Lambda Expressions?
    Suppose you have an interface with only one (abstract) method. This is also called a functional interface. Normally to use this method, you would have to make a class implementing this interface, make the method concrete by defining it, instantiate an object from this class, then call the method from the new object. A shorter way to do this is with lambda expressions. Suppose you have an Interface I with single method M. Then you can instantiate an object i from I possessing method M only with definition D via a Lambda Expression like so:
    I i = (args) -> { D };
    i.M(args);
It's as simple as that! So the functional interface defines a structure for the method, and the lambda expression provides the details of its implementation. 

What are enums?
    An enum is a set of constants. It is defined like so:
        public enum myKids {bill, mike, kathy}

What are initialization blocks? What is the initialization order?
    Initialization blocks are {} blocks which initialize variables that were previously declared. They can be static or nonstatic. Static blocks run before nonstatic blocks runs before constructor blocks. These blocks can contains code other than initializations, such as sysouts.