Posts

Showing posts with the label OOP Concepts

Packages in Java: Definition, Types, and Examples

Package in Java A package in Java is a collection of related classes, interfaces, and abstract classes that are grouped together as part of an application. Packages help in organizing code, avoiding name conflicts, and improving maintainability. Types of Packages Packages in Java are classified into two types: 1. Inbuilt Packages 2. User-Defined (Programmer-Defined) Packages 1. Inbuilt Packages Inbuilt packages are predefined packages that are available in the JDK software. These packages usually start with the java keyword. Examples of Inbuilt Packages java.lang java.sql java.io java.util java.net java.time  📌 Note: The java.lang package is imported automatically by the JVM. 2. User-Defined (Programmer-Defined) Packages User-defined packages are created by programmers according to the application requirements. Package Naming Convention com.companyname.applicationname.modulename org.companyname.applicationname.modulename Example package com.example.billing.invoice; Importing a Pac...

Polymorphism in Java: Definition, Types, and Examples

Polymorphism in Java Polymorphism is an object-oriented programming concept in which a single reference variable can exhibit different behaviors depending on the object it refers to. Types of Polymorphism Polymorphism in Java is classified into two types: Compile-Time Polymorphism Run-Time Polymorphism 1. Compile-Time Polymorphism In compile-time polymorphism, the method resolution is performed by the compiler. Key Points The compiler resolves the method call based on the parameter list. Once method resolution is done, the compiler performs static binding. In static binding:               ✓ The method declaration is bound to the method definition at compile time. Compile-time polymorphism is achieved using method overloading. Example class Calculator {     int add(int a, int b) {         return a + b;     }     double add(double a, double b) {         return a + b;     } } ...

Interface in Java: Definition, Rules, and Examples

Interface in Java An interface is a type definition block in Java that is completely abstract. It is used to achieve full abstraction and to define a contract that implementing classes must follow. Key Points An interface is declared using the interface keyword. An interface can contain:            ✓public static final variables (constants)           ✓public abstract methods (instance methods) An interface cannot be instantiated (object creation is not allowed). An interface can extend another interface using the extends keyword. A class can implement an interface using the implements keyword. An interface cannot extend a class. An interface supports complete abstraction because it contains only abstract methods. An interface supports multiple inheritance Example interface Animal {     int AGE = 5; // public static final by default     void sound(); // public abstract by default } class Dog implements Animal { ...

Abstract Class in Java: Definition, Rules, and Examples

Abstract Class in Java An abstract class is a class that is declared using the abstract keyword. It is used to achieve partial abstraction in Java. Key Points Any class declared with the abstract keyword is called an abstract class. An abstract class can contain:             ✓Abstract methods             ✓Concrete methods An abstract class cannot be instantiated (object creation is not allowed). If a class contains at least one abstract method, then the class must be declared abstract. An abstract class does not support multiple inheritance. An abstract class supports partial abstraction. Abstract Method An abstract method is a method that:                 ✓Does not have a method body                  ✓Ends with a semicolon (;) Abstract methods only provide method declaration, not implementation.             abstra...

Method Overriding in Java: Definition, Rules, and Examples

Method Overriding Method Overriding is a process in Java where a subclass provides a specific implementation for a method that is already defined in its superclass. Key Points 1. Method Overriding requires inheritance. 2. To override a method, the subclass method must have the same name, return    type, and parameter list as the superclass method. 3. During method overriding: The subclass method definition is used (exposed). The superclass method definition is hidden. This concept is sometimes referred to as method hiding (though strictly         speaking, method hiding is for static methods). 4. Only instance methods (non-static methods) can be overridden. 5. Method overriding cannot be achieved within the same class; it only works between subclass and superclass. Example class Animal {     void sound() {         System.out.println("Animal makes a sound");     } } class Dog extends Animal {     @Overri...