Object-Oriented Java Concepts Explained
Object-Oriented Java Concepts Explained
Inner classes in Java serve the purpose of logically grouping classes that are only used in one place, increasing encapsulation, and can also access private members of the outer class. An example is class `Outer`, which contains a non-static inner class `Inner`. The message `"Hello from inner"` is printed when `msg()` is called on an instance of Inner, showing how inner classes operate within their outer class context .
Constructor overloading in Java allows a class to have more than one constructor with different parameter lists, enabling objects to be instantiated in different ways. An example is the Box class which has two constructors. The default constructor initializes the dimensions to 1: `Box() { w=h=1; }`, and the parameterized constructor allows for specific dimensions: `Box(int x,int y) { w=x; h=y; }`. This allows for creating boxes of default size or specific sizes .
Enums in Java represent a fixed set of constants. They offer type safety and can include methods and fields. Typically, enums are used to define a collection of constants. For example, the `Day` enum declares the days of the week as constants, which can then be iterated and used throughout the application to manage day-related logic effectively .
Java supports inheritance through the `extends` keyword, allowing a class to inherit properties and behaviors (methods) from another class. Method overriding occurs when a subclass provides a specific implementation of a method already present in its superclass. For instance, in Animal and Dog classes, the Dog class overrides the `sound()` method to output "Bark" instead of "Some sound", demonstrating inheritance and polymorphism .
An abstract class in Java provides a base for other classes to extend and can contain fields, constructors, and implemented methods. Abstract methods declared in an abstract class must be implemented by subclasses. In contrast, interfaces can only contain method signatures (until Java 8) and are aimed at providing a contract for classes that implement them, allowing multiple inheritance of type. An example is the Vehicle class, which is abstract and contains the abstract method `move()`, which is then implemented in the Car class .
Polymorphism in Java is demonstrated through method overriding and dynamic dispatch where a subclass provides a specific implementation of a method that is already defined in its superclass. This occurs at runtime, allowing for dynamic method resolution. For example, in the code `Shape s=new Circle(); s.draw();`, the draw method in the Circle class overrides the one in Shape class. Even though the reference type is Shape, the draw method of Circle is executed, illustrating dynamic dispatch .
A static member in Java is shared among all objects of a class, meaning that it is not tied to a particular object instance. This can be used to maintain a counter across all instances. For example, in the Counter class, the static field `count` is incremented each time a Counter object is created. When executed, `StaticDemo` prints `2` after creating two `Counter` objects, as `count` reflects the number of instances created .
Overriding the `toString()` method in Java enhances the program's readability and debugging processes by providing a human-readable depiction of an object. For example, in the class `Point`, `toString()` is overridden to output coordinates in the format `"(x,y)"`. When `System.out.println(new Point(2,3));` is run, it prints `(2,3)`, offering insight into the object's data rather than its memory address .
Encapsulation in object-oriented programming restricts direct access to some of an object's components and can prevent the unintended modification of data. In Java, encapsulation is implemented using private fields and public getter and setter methods. For instance, in the Encapsulate class, the `value` field is private, and the public methods `setValue()` and `getValue()` encapsulate access to this field, controlling how it is modified and accessed .
The `equals()` method in Java is overridden to provide a specific definition of equality for objects. This is crucial for comparing two objects meaningfully rather than just their references. For instance, in the Item class, `equals()` is overridden such that it checks if the ids of two Item objects are the same, which helps in comparing the logical data equality. This override allows customization according to class specifications and is essential for collections that rely on equality checks .