Posts

Showing posts from December, 2020

Enum private Constructor

enum has constructor. This constructor is private. Why do we need a private constructor? because enum has finite values.  If the constructor is public , anyone can change the values of enum. It can increase the values  This would extend the initial values of enum.  It is a compile time error if the constructor of an enum type is declared as public or protected.  If no access modifier is specified for the constructor of an enum type, The constructor is by default private. package com.simple   public enum  BankAccount    {      AXIS  ("Axis Bank Account"),     DENA  ("Dena Bank Account"),     HDFC  ("Hdfc Bank Account");     private final String desc ;     BankAccount    ( String   desc) {        this.desc = desc;       }     public String getDesc(){       return desc;    } Here we have a Ba...

Predicate The Functional interface in java

What is a functional interface?  A Functional Interface is an Interface which allows only one abstract method within the interface scope. To use a normal interface there are two steps. 1. Declare one interface with abstract method. 2. Provide  body of Interface  by implementing on a class. Same happen in case of functional interface. Important difference in functional interface there is only one abstract method declaration. While implementing we use the benefit of single abstract method , we write anonymous function for single abstract method. This functional interface helps us to write lambda in java. public interface MyFunctionalnterface < T > { String funPart (T t) ; } public class UseFunctional { public static void main(String [] arg) { String str =" Functional World !"; M yFunctionalnterface<String> callFun; callFun= (xyz)-> { return str + xyz; }; System.out.println(callFun. funPart (" in java ")); } } Som...