Class in Java

We can categorize java classes in several criteria
criteria-1

1. Top Level class
We can create any number of top level class, interfaces, enumerators, annotations inside the java file. But we can add only one public top level class in that & that class name must be the  name  of the java file.Top level class cant be static.

public class AppInitializer {//file name:AppInitializer.java

}

class A{

}

interface B{

}

@interface C{

}

enum D{

}

2.Nested Class
   2.1 Static Class

   2.2)Non-static Nested class(Inner Class)
        2.2.1)Regular inner class
        2.2.2)Local Inner Class
            2.2.2.a)Anonymous Inner Class

eg:-Nested class
        class AppInitializer {
    class C{


    }
    static class B{


    }
}

Q-A)Can we create inner class inside the interface?
//If we create a class in interface,it will be implicitly static. 

interface A {
    // Directly cant create inner class in  the interface(But can create indirectly).Class will be implicitly static class
    //implicitly: If you do not add static JVM automatically add static key word

     static  class B {//implicitly static
       class F{//regular inner class inside the interface indirectly

        }
    }

    static void myMethod() {
        //but can create inside the static method
        class C {//Local inner class inside the interface indirectly

        }

    }

    default void myMethod2() {
        //but can create inside the default method
        class D {//Local inner class inside the interface indirectly

        }
    }
}

Q-A) What is Regular inner class
If the outer scope of the inner is a type(Class,interface,enum,Annotation),that named regular inner class 
Eg:-
 public class B {//Outer scope
        class A{//regular inner class 
            
        }
    }

Q-A) What Local Inner Class
If the outer scope of the inner is not a type that named regular inner class
Eg:-
class B {
   public void myMethod(){//outer scope
    class A{
            
        }
    }
}
Q-A)How to create object from regular inner class
public class Mytest {

    static class B {//Outer scope
        class A{//regular inner class
            public void myMethod(){
                System.out.println("This mehod is in the regular innner class");
            }
        }
    }
    
    public static void main(String[] args) {
        
        B.A a = new B().new A();//object
        a.myMethod();
    }
}

Q-A)How to create object from local inner class

public class Mytest {
    public static void main(String[] args) {
        Top.Nested nested = new Top.Nested();
        nested.myMethod();
    }
}
class Top{
    static class Nested{
        void myMethod(){
            System.out.println("static nested class object");
        }
    }
}

Q-A)what is an anonymous Inner Class
  • Special type of local inner class.
  • only one object can create from this class.
  • no type & no name specified for this class.
  • no constructor.
Eg1:-
    new Something(){};

eg2:-
class Animal{
    void speak(){
        System.out.println("Animal is speaking");
    }
}

Q-A)Can we create object from an interface.
We cant create object from directly.but after implementing to the class we can create object from that class.

Q-A)What is a functional interface.
there is possibility to include several methods(static,default,abstract).If there is only one abstract method in the interface(But can include non abstract method in functional interfaces) it will called functional interface.(Introduce from java 8)
we can apply "Lamda expression to functional interfaces."

Q-A)Method references.
IF we,
    create anonymous inner class from fictional interface ,
   That AIC include only one code line,
    If that code line invoke a method,
    when invoke the method the method parameter list match to the outer scope parameter list
We can replace that from method references.

 



criteria-2
Concrete Class
Fully implemented class.

Abstract Class
partially implemented class.Fully abstract class become an interface

abstract class A{//class must be abstract,else compile time error

    abstract String mymehod();//abstract method

    int myMethod2(){//Instance method in abstract class
        return 0;
    }
    static int myMethod3(){//static method in abstract class
        return 0;
    }
}

//abstract class can present empty
abstract class C {

}

   
Final Class(leaf class-OOP name)
fully implement but can't inherit & cant create objects.
Eg:-
class AppInitializer  implements  B{//wrong-cant inherit
   
    }
final class B{
    
    }

abstract final class B{//wrong-must fully implement
    abstract void mymethod1();
}

final class B{//Right-Fully implemented
  void myMethod1(){
      
  }
}


How to laoad class to ram 3 methods
Class.forname() 
Create Object
Access to static Variable or method             
                                                                                              Will be continue.....


Comments

Popular posts from this blog

Hibernate-2

Hibernate-1

Avoid Round-off error in large & Small numbers using BigDecimal Class in Java