Int

Four Pillars of OOP

  1. Encapsulation: This principle restricts direct access to some of an object's components and can prevent the accidental modification of data. It is achieved by using access modifiers (private, protected, public).

    • Example:
      java
      1class BankAccount { 2 private double balance; 3 4 public void deposit(double amount) { 5 if (amount > 0) { 6 balance += amount; 7 } 8 } 9 10 public double getBalance() { 11 return balance; 12 } 13}
  2. Abstraction: This involves hiding complex implementation details and showing only the essential features of the object. It simplifies interaction with objects.

    • Example:
      java
      1abstract class Animal { 2 abstract void sound(); 3} 4 5class Dog extends Animal { 6 void sound() { 7 System.out.println("Woof"); 8 } 9}
  3. Inheritance: This allows a new class to inherit properties and methods from an existing class. It promotes code reusability.

    • Example:
      java
      1class Vehicle { 2 void start() { 3 System.out.println("Vehicle started"); 4 } 5} 6 7class Car extends Vehicle { 8 void honk() { 9 System.out.println("Car honked"); 10 } 11}
  4. Polymorphism: This allows methods to do different things based on the object it is acting upon. It can be achieved through method overriding and method overloading.

    • Example:
      java
      1class Animal { 2 void sound() { 3 System.out.println("Animal makes a sound"); 4 } 5} 6 7class Cat extends Animal { 8 void sound() { 9 System.out.println("Meow"); 10 } 11} 12 13// Compile-time polymorphism 14class MathUtil { 15 int add(int a, int b) { 16 return a + b; 17 } 18 double add(double a, double b) { 19 return a + b; 20 } 21}

Class and Object in OOP

  • Class: A blueprint for creating objects, defining properties and methods.

    • Example:
      java
      1class Dog { 2 String name; 3 void bark() { 4 System.out.println("Woof"); 5 } 6}
  • Object: An instance of a class.

    • Example:
      java
      1Dog myDog = new Dog(); 2myDog.name = "Buddy"; 3myDog.bark(); // Outputs: Woof

Inheritance in OOP

Inheritance allows one class to inherit fields and methods from another, promoting code reusability and establishing a relationship between classes.

  • Example:
    java
    1class Animal { } 2class Dog extends Animal { } // Dog inherits from Animal

Polymorphism

  • Compile-time polymorphism (Method Overloading): Same method name with different parameters.

    • Example:
      java
      1class MathUtil { 2 int add(int a, int b) { return a + b; } 3 double add(double a, double b) { return a + b; } 4}
  • Runtime polymorphism (Method Overriding): A subclass provides a specific implementation of a method that is already defined in its superclass.

    • Example:
      java
      1Animal myAnimal = new Dog(); // Dog overrides sound() 2myAnimal.sound(); // Outputs: Woof

Abstract Class in OOP

An abstract class cannot be instantiated and can contain abstract methods (without body) that must be implemented by subclasses.

  • Example:
    java
    1abstract class Shape { 2 abstract void draw(); 3} 4 5class Circle extends Shape { 6 void draw() { 7 System.out.println("Drawing Circle"); 8 } 9}

Constructor Overloading in OOP

Constructor overloading allows a class to have more than one constructor with different parameters.

  • Example:
    java
    1class Rectangle { 2 int length, width; 3 4 Rectangle() { 5 length = 1; 6 width = 1; 7 } 8 9 Rectangle(int l, int w) { 10 length = l; 11 width = w; 12 } 13}

Destructor in OOP

A destructor is a method that is called when an object is destroyed. It is used to free resources.

  • Example (C++):
    cpp
    1class MyClass { 2public: 3 ~MyClass() { 4 // Cleanup code 5 } 6};

Purpose of the “this” Keyword in OOP


Purpose of the “this” Keyword in OOP

The this keyword refers to the current object instance within a class. It is often used to differentiate between instance variables and parameters with the same name.

  • Example:
    java
    1class Person { 2 String name; 3 4 Person(String name) { 5 this.name = name; // 'this.name' refers to the instance variable 6 } 7 8 void display() { 9 System.out.println("Name: " + this.name); 10 } 11}

Significance of the “final” Keyword in OOP

The final keyword is used to declare constants, prevent method overriding, and prevent inheritance.

  • Example:
    java
    1final int MAX_VALUE = 100; // Constant 2class Base { 3 final void show() { // Cannot be overridden 4 System.out.println("Base class show method"); 5 } 6} 7 8class Derived extends Base { 9 // void show() { } // This would cause a compile-time error 10}

What is a Data Structure?

A data structure is a specialized format for organizing, processing, and storing data. It is important in computer science because it allows for efficient data management and manipulation, impacting the performance of algorithms.

  • Example: Arrays, Linked Lists, Trees, Graphs, Hash Tables, etc.

Difference Between an Array and a Linked List

  • Array: A collection of elements stored in contiguous memory locations. It has a fixed size and allows for fast access to elements using indices.

    • Pros: Fast access, simple to use.
    • Cons: Fixed size, costly insertions/deletions.
  • Linked List: A collection of nodes where each node contains data and a reference (or pointer) to the next node. It can grow and shrink dynamically.

    • Pros: Dynamic size, efficient insertions/deletions.
    • Cons: Slower access time, more memory overhead due to pointers.

Difference Between a Stack and a Queue

  • Stack: A Last In, First Out (LIFO) data structure where the last element added is the first to be removed.
    • Example: Undo functionality in text editors.
  • Queue: A First In, First Out (FIFO) data structure where the first element added is the first to be removed.
    • Example: Print queue in printers.

Traversing a Binary Tree

  • Inorder Traversal: Left, Root, Right

    • Example:
      java
      1void inorder(Node node) { 2 if (node != null) { 3 inorder(node.left); 4 System.out.print(node.data + " "); 5 inorder(node.right); 6 } 7}
  • Preorder Traversal: Root, Left, Right

    • Example:
      java
      1void preorder(Node node) { 2 if (node != null) { 3 System.out.print(node.data + " "); 4 preorder(node.left); 5 preorder(node.right); 6 } 7}
  • Postorder Traversal: Left, Right, Root

    • Example:
      java
      1void postorder(Node node) { 2 if (node != null) { 3 postorder(node.left); 4 postorder(node.right); 5 System.out.print(node.data + " "); 6 } 7}

Difference Between Breadth-First Search (BFS) and Depth-First Search (DFS)

  • BFS: Explores all neighbors at the present depth before moving on to nodes at the next depth level. It uses a queue.

    • Example: Finding the shortest path in an unweighted graph.
  • DFS: Explores as far as possible along each branch before backtracking. It uses a stack (or recursion).

    • Example: Solving puzzles with only one solution (like mazes).

Concept of Hashing

Hashing is a technique used to uniquely identify a specific object from a group of similar objects. It uses a hash function to convert a given key into an index in a hash table.

  • Applications: Hash tables, data retrieval, checksums, and cryptography.

Difference Between Linear Search and Binary Search Algorithms

  • Linear Search: Checks each element in a list until the desired element is found or the list ends.

    • Time Complexity: O(n)
  • Binary Search: Divides the sorted list into halves and eliminates one half from consideration.

    • Time Complexity: O(log n)

Comments

Popular posts from this blog

Class File

Dependencies