Abstract Classes

Abstract Classes An abstract class in Java is a class that cannot be instantiated on its own and must be subclassed by another class. It is used to provide a common interface for other classes to implement. An abstract class may contain abstract methods (without implementation) and concrete methods (with implementation). Key Points: An abstract class can have abstract methods (methods without body) and concrete methods (methods with body). A subclass that extends an abstract class must provide concrete implementations for all abstract methods. An abstract class can have fields, constructors, and methods just like regular classes. Abstract methods in an abstract class must be implemented by the subclass unless the subclass is also abstract. Example: abstract class Animal { // Abstract method (does not have a body) abstract void sound(); // Regular method void sleep() { System.out.println("Sleeping..."); } } class Dog extends Animal { // Implementing the abstract method void sound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { // Animal animal = new Animal(); // This will give an error because Animal is abstract Dog dog = new Dog(); dog.sound(); // Output: Dog barks dog.sleep(); // Output: Sleeping... } } Example with multiple abstract methods: abstract class Shape { // Abstract method (no body) abstract void draw(); abstract double area(); } class Circle extends Shape { double radius; Circle(double radius) { this.radius = radius; } // Implementing abstract methods void draw() { System.out.println("Drawing a circle"); } double area() { return Math.PI * radius * radius; } } public class Main { public static void main(String[] args) { Shape shape = new Circle(5.0); shape.draw(); // Output: Drawing a circle System.out.println("Area: " + shape.area()); // Output: Area: 78.53981633974483 } } 🔗 Related Notes Access Modifiers Method Overloading Static Keyword Inheritance Types of Inheritance Method Overriding super to Access Superclass Members

May 8, 2025 · 2 min · Rohan

Method Overriding

Method Overriding Method overriding is an OOP concept where a subclass provides a specific implementation for a method that is already defined in its superclass. The method in the subclass must have the same signature (name, return type, and parameters) as the one in the parent class. Key Points: Overriding is used to define a new behavior in the subclass for a method inherited from the superclass. The method in the subclass must have the same method signature as the one in the parent class. The @Override annotation is used to indicate that a method is being overridden. The method in the parent class must be accessible (i.e., not private). Example: class Animal { void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { // Method overriding @Override void sound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Animal myAnimal = new Animal(); myAnimal.sound(); // Output: Animal makes a sound Animal myDog = new Dog(); myDog.sound(); // Output: Dog barks } } Example with @Override annotation: class Animal { void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Animal animal = new Animal(); animal.sound(); // Output: Animal makes a sound Dog dog = new Dog(); dog.sound(); // Output: Dog barks } } 🔗 Related Notes Access Modifiers Method Overloading Static Keyword Inheritance Types of Inheritance super to Access Superclass Members

May 8, 2025 · 2 min · Rohan

Super to Access Superclass Members

super to Access Superclass Members In Java, the super keyword refers to the immediate parent class of a given object. It is used to access members (fields, methods, and constructors) of the parent class from the child class. Key Uses of super: Accessing Superclass Methods: It is used to call a method of the superclass when the subclass has overridden that method. Accessing Superclass Constructor: It is used to call the constructor of the superclass from the subclass. Accessing Superclass Fields: It allows access to the fields of the superclass. Example 1: Using super to call a superclass method class Animal { void eat() { System.out.println("Animal is eating..."); } } class Dog extends Animal { void eat() { super.eat(); // Call the eat() method of the parent class Animal System.out.println("Dog is eating..."); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); // Output will be: Animal is eating... Dog is eating... } } Example 2: Using super to call a superclass constructor class Animal { Animal() { System.out.println("Animal constructor"); } } class Dog extends Animal { Dog() { super(); // Call the constructor of the parent class Animal System.out.println("Dog constructor"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); // Output will be: Animal constructor Dog constructor } } Example 3: Using super to access a superclass field class Animal { String name = "Animal"; } class Dog extends Animal { String name = "Dog"; void display() { System.out.println("Name from Dog class: " + name); System.out.println("Name from Animal class: " + super.name); // Access the name from Animal class } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.display(); // Output will be: Name from Dog class: Dog Name from Animal class: Animal } } 🔗 Related Notes Access Modifiers Method Overloading Static Keyword Inheritance Types of Inheritance Abstract Classes

May 8, 2025 · 2 min · Rohan

Types of Inheritance

Types of Inheritance In Java, inheritance allows one class to inherit the attributes and behaviors (fields and methods) of another. There are several types of inheritance, each with its own characteristics and applications in object-oriented programming. 1. Single Inheritance In single inheritance, a class inherits from only one parent class. This is the simplest form of inheritance. Example: class Animal { void eat() { System.out.println("Eating..."); } } class Dog extends Animal { void bark() { System.out.println("Barking..."); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); // Inherited method from Animal dog.bark(); // Method specific to Dog } } 2. Multilevel Inheritance In multilevel inheritance, a class can inherit from another class, which itself is a subclass of another class. This forms a chain of inheritance. ...

May 8, 2025 · 2 min · Rohan

Inheritance

Inheritance Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows one class (child or subclass) to inherit the fields and methods of another class (parent or superclass). This promotes code reusability and allows for hierarchical class relationships. Key Points: The child class can extend the functionality of the parent class. The child class can access public and protected members of the parent class. In Java, the extends keyword is used to create a subclass. Types of Inheritance: Single Inheritance: A subclass inherits from one parent class. Multilevel Inheritance: A subclass inherits from a parent class, and that parent class can be further extended. Hierarchical Inheritance: Multiple subclasses inherit from a single parent class. Multiple Inheritance (through interfaces): Java does not support multiple inheritance through classes, but it can be achieved through interfaces. Example: class Animal { void eat() { System.out.println("Eating..."); } } class Dog extends Animal { void bark() { System.out.println("Barking..."); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); // Inherited method dog.bark(); // Method of Dog class } } 🔗 Related Notes Access Modifiers Method Overloading Static Keyword Abstract Classes Packages and Interfaces

May 8, 2025 · 1 min · Rohan

Static Keyword

Static Keyword The static keyword in Java is used to indicate that a particular member (variable, method, or inner class) belongs to the class itself, rather than to instances of the class. It can be applied to variables, methods, blocks, and nested classes. Key Points: Static variables are shared among all instances of the class. They can be accessed without creating an instance of the class. Static methods can be called without creating an instance of the class and can only access static variables and methods. Static blocks are used for static initializations and are executed when the class is loaded into memory. Example: class Counter { static int count = 0; // Static method to increment the count public static void increment() { count++; } // Static method to display the count public static void displayCount() { System.out.println("Count: " + count); } } public class Main { public static void main(String[] args) { Counter.increment(); Counter.increment(); Counter.displayCount(); // Output: Count: 2 } } 🔗 Related Notes Access Modifiers Method Overloading Inheritance Abstract Classes Packages and Interfaces

May 8, 2025 · 1 min · Rohan

Method Overloading

Method Overloading Method overloading in Java refers to the ability to define multiple methods with the same name but different parameter lists. Overloading is resolved at compile time, and the appropriate method is selected based on the number and type of arguments passed. Key Points: Overloading is related to the method signature (name and parameters). Return type alone cannot be used to overload methods. It improves code readability and allows the same method to perform different tasks. Example of Method Overloading: class Calculator { // Overloaded method to add two integers public int add(int a, int b) { return a + b; } // Overloaded method to add three integers public int add(int a, int b, int c) { return a + b + c; } // Overloaded method to add two double numbers public double add(double a, double b) { return a + b; } } Example of Method Overloading with Different Return Types: class MathOperation { // Method to add two integers int add(int a, int b) { return a + b; } // Method to add three integers int add(int a, int b, int c) { return a + b + c; } // Method to add two double values double add(double a, double b) { return a + b; } } public class Main { public static void main(String[] args) { MathOperation math = new MathOperation(); System.out.println("Sum of two integers: " + math.add(5, 10)); // Output: 15 System.out.println("Sum of three integers: " + math.add(5, 10, 15)); // Output: 30 System.out.println("Sum of two doubles: " + math.add(5.5, 10.5)); // Output: 16.0 } } 🔗 Related Notes Access Modifiers Static Keyword Inheritance Abstract Classes Packages and Interfaces

May 8, 2025 · 2 min · Rohan

Access Modifiers

Access Modifiers Access modifiers are keywords in Java that set the visibility and accessibility of classes, methods, and variables. They define the scope of the class members and control how and where they can be accessed. Java provides four types of access modifiers: 1. Public The public modifier allows the member to be accessed from anywhere in the program, regardless of the package. 2. Private The private modifier restricts the member to be accessed only within the same class. It cannot be accessed from outside the class, even by subclass objects. ...

May 8, 2025 · 1 min · Rohan

Strings

📝 Strings in Java In Java, a String is a sequence of characters. It is a widely used class for manipulating text. Strings are immutable, meaning once a string is created, its value cannot be changed. 🧠 String Characteristics Immutable Once a string object is created, its value cannot be modified. Any operation on a string creates a new string object. String Pool Java maintains a pool of strings to optimize memory usage. If the same string is created multiple times, the JVM reuses the string from the pool instead of creating a new one. String Length ...

May 8, 2025 · 2 min · Rohan

Types of Arrays

📊 Types of Arrays in Java Arrays in Java can be classified into two types: Single-Dimensional Arrays and Multidimensional Arrays. Both types store elements of the same data type, but their structure differs. 🧑‍🏫 1. Single-Dimensional Arrays A single-dimensional array is a linear list of elements, where each element is accessed via its index. 🔹 Example int[] numbers = {10, 20, 30, 40, 50}; System.out.println("Element at index 2: " + numbers[2]); // Output: 30 🔹 Declaring and Initializing int[] arr = new int[5]; // Declaring with fixed size arr[0] = 1; // Assigning values 📐 2. Multidimensional Arrays Multidimensional arrays are arrays of arrays. A 2D array is the most common, where each element is itself an array. ...

May 8, 2025 · 2 min · Rohan