Category: 02. JAVA OOPs
-
Java – Enum String
Java enum is a special construct to represents a group of pre-defined constant strings and provides clarity in code while used as constants in application code. By default, an enum string representation is the same as its declaration. Consider the following example: If we print the string representation of the above enum using the enum directly, using toString(), or using the name() method, it will…
-
Java – Enum Constructor
Java enum is a special kind of class that represents a group of pre-defined constant values and can be used in switch expressions for comparison, to be used as constants in application code. By default, an enum does not require any constructor and its default value is the same as its declaration. Consider the following example: If we print the any of…
-
Java – Enum class
Introduction The Java Enum class is the common base class of all Java language enumeration types. Class Declaration Following is the declaration for java.lang.Enum class − Class constructors Sr.No. Constructor & Description 1 protected Enum(String name, int ordinal)This is the single constructor. Class methods Sr.No. Method & Description 1 int compareTo(E o)This method compares this enum with the specified…
-
Java – Singleton Class
Java Singleton Design Pattern Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object. Java Singleton Class This pattern involves a single class which is responsible to create an object while making sure…
-
Java – Anonymous Classes
Java Anonymous Class An anonymous class in Java is an inner class which is declared without any class name at all. In other words, a nameless inner class in Java is called an anonymous inner class. Since it does not have a name, it cannot have a constructor because we know that a constructor name is the…
-
Java – Static Classes
In Java concept of static class is introduced under concept of inner classes, which are specially designed for some delicate functionality in a class. Java Static Classes Static classes in Java are allowed only for inner classes which are defined under some other class, as static outer class is not allowed which means that we can’t use…
-
Java – Inner classes
Java Inner Class A Java inner class is a class that is defined inside another class. The concept of inner class works with nested Java classes where outer and inner classes are used. The main class in which inner classes are defined is known as the outer class and all other classes which are inside the outer class are…
-
Java – Packages
Java Packages Packages are used in Java in order to prevent naming conflicts, control access, make searching/locating and usage of classes, interfaces, enumerations, and annotations easier, etc. A Java package can be defined as a grouping of related types (classes, interfaces, enumerations, and annotations ) providing access protection and namespace management. Types of Java Packages Java packages are of two…
-
Java – Interfaces
Java Interfaces Java interface is a collection of abstract methods. The interface is used to achieve abstraction in which you can define methods without their implementations (without having the body of the methods). An interface is a reference type and is similar to the class. Along with abstract methods, an interface may also contain constants, default methods, static methods,…
-
Java – Encapsulation
Java Encapsulation Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction. Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the…