ar
Feedback
Tech Jargon - Decoded

Tech Jargon - Decoded

الذهاب إلى القناة على Telegram

Confused by tech terms? Don’t worry, we’ve got you 🤝 We make things simple, one concept at a time. Learn daily Easy & clear Turn Confusion into clarity. #tech #it #softwareengineer #cs #development

إظهار المزيد
2 018
المشتركون
لا توجد بيانات24 ساعات
-77 أيام
-4030 أيام
أرشيف المشاركات
💻 Create Custom Package
// Save this file as MyPackageClass.java inside a directory named "mypackage"

package mypackage;

public class MyPackageClass {

    public void displayMessage(String message) {
        System.out.println("Message from mypackage: " + message);
    }
}
'''

// Save this file as MainClass.java in the same directory as "mypackage" or in a directory that has "mypackage" as a subdirectory

import mypackage.MyPackageClass;

public class MainClass {

    public static void main(String[] args) {
        MyPackageClass obj = new MyPackageClass();
        obj.displayMessage("Hello from MainClass!");
    }
}
📤 Output:
Message from mypackage: Hello from MainClass!

OOP - Packages

💻 Marker Interfaces
import java.io.*;

interface Serializable {
}

class Student implements Serializable {
    String name;
    int rollNumber;

    public Student(String name, int rollNumber) {
        this.name = name;
        this.rollNumber = rollNumber;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", rollNumber=" + rollNumber +
                '}';
    }
}

public class MarkerInterfaceExample {
    public static void main(String[] args) {
        Student student = new Student("Raju", 10);

        // Check if the object is Serializable
        if (student instanceof Serializable) {
            System.out.println("The Student object is Serializable.");
            // Here, you might perform serialization operations
            try (FileOutputStream fileOut = new FileOutputStream("student.ser");
                 ObjectOutputStream objectOut = new ObjectOutputStream(fileOut)) {
                objectOut.writeObject(student);
                System.out.println("Serialized data is saved in student.ser");
            } catch (IOException i) {
                i.printStackTrace();
            }
        } else {
            System.out.println("The Student object is not Serializable.");
        }
    }
}
📤 Output:
The Student object is Serializable.
Serialized data is saved in student.ser

💻 Nested Interfaces
import java.util.Scanner;

public class NestedInterfacesExample {

    interface OuterInterface {
        void outerMethod();

        interface InnerInterface {
            void innerMethod();
        }
    }

    static class NestedClass implements OuterInterface.InnerInterface, OuterInterface {

        @Override
        public void innerMethod() {
            System.out.println("Inner method implemented.");
        }

        @Override
        public void outerMethod() {
            System.out.println("Outer method implemented.");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // Scanner object
        NestedClass nestedObject = new NestedClass();

        nestedObject.outerMethod();
        nestedObject.innerMethod();

        scanner.close(); // closing resource
    }
}
📤 Output:
Outer method implemented.
Inner method implemented.

💻 Functional Interfaces
import java.util.Scanner;

public class FunctionalInterfaceExample {

    @FunctionalInterface
    interface StringOperation {
        String operate(String str);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String inputString = scanner.nextLine();

        // Define a functional interface implementation for converting to uppercase
        StringOperation toUpperCase = String::toUpperCase;
        String upperCaseResult = toUpperCase.operate(inputString);
        System.out.println("Uppercase: " + upperCaseResult);

        // Define a functional interface implementation for adding '!' at the end
        StringOperation addExclamation = str -> str + "!";
        String exclamationResult = addExclamation.operate(inputString);
        System.out.println("With exclamation: " + exclamationResult);

        scanner.close();
    }
}
📤 Output:
Input: hello
Output: Uppercase: HELLO
Output: With exclamation: hello!

💻 Private Methods in Interfaces
import java.util.Scanner;

interface MyInterface {
    default void publicMethod() {
        System.out.println("Public method called.");
        privateMethod(); // Calling the private method
    }

    private void privateMethod() {
        System.out.println("Private method implementation inside interface.");
    }
}

class InterfaceExample implements MyInterface {

}

public class PrivateInterfaceMethods {
    public static void main(String[] args) {
        InterfaceExample obj = new InterfaceExample();
        obj.publicMethod(); // Calling the public method, which in turn calls the private method.
    }
}
📤 Output:
Public method called.
Private method implementation inside interface.

💻 Static Methods in Interfaces
import java.util.Scanner;

public class StaticMethodsInInterface {

    interface MyInterface {
        static int add(int a, int b) {
            return a + b;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter first number: ");
        int num1 = scanner.nextInt();

        System.out.print("Enter second number: ");
        int num2 = scanner.nextInt();

        int sum = MyInterface.add(num1, num2);
        System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 3
Output: Enter first number: Enter second number: Sum of 5 and 3 is: 8

💻 Default Methods in Interfaces
import java.util.Scanner;

interface MyInterface {
    void regularMethod(String message);

    default void defaultMethod() {
        System.out.println("Hello from the default method!");
    }
}

class MyClass implements MyInterface {
    @Override
    public void regularMethod(String message) {
        System.out.println("Message received: " + message);
    }
}

public class DefaultMethodsExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a message: ");
        String inputMessage = scanner.nextLine();

        MyClass myObject = new MyClass();
        myObject.regularMethod(inputMessage);
        myObject.defaultMethod();

        scanner.close();
    }
}
📤 Output:
Input: Hello, world!
Output: Enter a message: Message received: Hello, world!
Hello from the default method!

💻 Abstract vs Interface Comparison
import java.util.Scanner;

public class AbstractVsInterfaceComparison {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Abstract Class vs Interface in Java:");
        System.out.println("nAbstract Class:");
        System.out.println("1. Can have abstract and non-abstract methods.");
        System.out.println("2. Can have instance variables.");
        System.out.println("3. Can provide a common base implementation.");
        System.out.println("4. Uses 'extends' keyword for inheritance.");
        System.out.println("5. Supports multiple inheritance of type (through interfaces).");

        System.out.println("nInterface:");
        System.out.println("1. Can only have abstract methods (before Java 8).");
        System.out.println("2. Cannot have instance variables (only constants).");
        System.out.println("3. Defines a contract that classes must adhere to.");
        System.out.println("4. Uses 'implements' keyword for implementation.");
        System.out.println("5. Supports multiple inheritance of type.");

        System.out.println("nKey Differences:");
        System.out.println("1. Abstract class can have state (variables), interface cannot.");
        System.out.println("2. A class can extend only one abstract class, but can implement multiple interfaces.");
        System.out.println("3. Abstract class can have constructors, interface cannot.");

        scanner.close();
    }
}
📤 Output:
// Code not available

💻 Multiple Interface Implementation
import java.util.Scanner;

interface Printable {
    void printDetails();
}

interface CalculateArea {
    double calculateArea();
}

class Rectangle implements Printable, CalculateArea {
    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public void printDetails() {
        System.out.println("Rectangle Details:");
        System.out.println("Length: " + length);
        System.out.println("Width: " + width);
    }

    @Override
    public double calculateArea() {
        return length * width;
    }
}

public class MultipleInterfaceImplementation {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter length of rectangle: ");
        double length = scanner.nextDouble();

        System.out.print("Enter width of rectangle: ");
        double width = scanner.nextDouble();

        Rectangle rectangle = new Rectangle(length, width);

        rectangle.printDetails();
        System.out.println("Area of rectangle: " + rectangle.calculateArea());

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 10
Output: Rectangle Details:
Output: Length: 5.0
Output: Width: 10.0
Output: Area of rectangle: 50.0

💻 Interface Implementation
import java.util.Scanner;

interface Shape {
    double getArea();
    double getPerimeter();
}

class Circle implements Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }

    @Override
    public double getPerimeter() {
        return 2 * Math.PI * radius;
    }
}

class Rectangle implements Shape {
    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public double getArea() {
        return length * width;
    }

    @Override
    public double getPerimeter() {
        return 2 * (length + width);
    }
}

public class InterfaceImplementation {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter radius of the circle: ");
        double radius = scanner.nextDouble();
        Circle circle = new Circle(radius);

        System.out.println("Area of the circle: " + circle.getArea());
        System.out.println("Perimeter of the circle: " + circle.getPerimeter());

        System.out.print("Enter length of the rectangle: ");
        double length = scanner.nextDouble();
        System.out.print("Enter width of the rectangle: ");
        double width = scanner.nextDouble();
        Rectangle rectangle = new Rectangle(length, width);

        System.out.println("Area of the rectangle: " + rectangle.getArea());
        System.out.println("Perimeter of the rectangle: " + rectangle.getPerimeter());

        scanner.close();
    }
}
📤 Output:
Input: 5
Output: Enter radius of the circle: Area of the circle: 78.53981633974483
Perimeter of the circle: 31.41592653589793
Enter length of the rectangle: 4
Enter width of the rectangle: 6
Area of the rectangle: 24.0
Perimeter of the rectangle: 20.0

💻 Abstract Class Implementation
import java.util.Scanner;

abstract class Shape {
    // Abstract method to calculate area
    abstract double calculateArea();

    // Concrete method (not abstract)
    void display() {
        System.out.println("This is a shape.");
    }
}

class Circle extends Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    double calculateArea() {
        return Math.PI * radius * radius;
    }
}

class Rectangle extends Shape {
    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    double calculateArea() {
        return length * width;
    }
}

public class AbstractClassImplementation {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter radius of the circle: ");
        double circleRadius = scanner.nextDouble();
        Circle circle = new Circle(circleRadius);

        System.out.print("Enter length of the rectangle: ");
        double rectangleLength = scanner.nextDouble();
        System.out.print("Enter width of the rectangle: ");
        double rectangleWidth = scanner.nextDouble();
        Rectangle rectangle = new Rectangle(rectangleLength, rectangleWidth);

        System.out.println("Area of circle: " + circle.calculateArea());
        System.out.println("Area of rectangle: " + rectangle.calculateArea());

        circle.display(); // Calling the concrete method
        rectangle.display();

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 4
Input: 6
Output: Enter radius of the circle: Enter length of the rectangle: Enter width of the rectangle: Area of circle: 78.53981633974483
Output: Area of rectangle: 24.0
Output: This is a shape.
Output: This is a shape.

OOP - Abstraction

💻 Encapsulation with Validation
import java.util.Scanner;

public class Account {
    private String accountHolderName;
    private double balance;

    public Account(String accountHolderName, double initialBalance) {
        this.accountHolderName = accountHolderName;
        this.balance = initialBalance;
    }

    public String getAccountHolderName() {
        return accountHolderName;
    }

    public void setAccountHolderName(String accountHolderName) {
        this.accountHolderName = accountHolderName;
    }

    public double getBalance() {
        return balance;
    }

    public void deposit(double amount) {
        if (amount > 0) {
            this.balance += amount;
            System.out.println("Deposit successful. New balance: " + this.balance);
        } else {
            System.out.println("Invalid deposit amount.");
        }
    }

    public void withdraw(double amount) {
        if (amount > 0 && amount <= this.balance) {
            this.balance -= amount;
            System.out.println("Withdrawal successful. New balance: " + this.balance);
        } else {
            System.out.println("Insufficient balance or invalid withdrawal amount.");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter account holder name: ");
        String name = scanner.nextLine();

        System.out.print("Enter initial balance: ");
        double initialBalance = scanner.nextDouble();
        scanner.nextLine(); // Consume newline

        Account myAccount = new Account(name, initialBalance);

        System.out.println("Account created for " + myAccount.getAccountHolderName() + " with balance: " + myAccount.getBalance());

        System.out.print("Enter deposit amount: ");
        double depositAmount = scanner.nextDouble();
        scanner.nextLine(); // Consume newline
        myAccount.deposit(depositAmount);

        System.out.print("Enter withdrawal amount: ");
        double withdrawalAmount = scanner.nextDouble();
        scanner.nextLine(); // Consume newline
        myAccount.withdraw(withdrawalAmount);

        System.out.println("Final balance for " + myAccount.getAccountHolderName() + ": " + myAccount.getBalance());

        scanner.close();
    }
}
📤 Output:
Input: John Doe
Input: 1000
Output: Account created for John Doe with balance: 1000.0
Input: 500
Output: Deposit successful. New balance: 1500.0
Input: 2000
Output: Insufficient balance or invalid withdrawal amount.
Output: Final balance for John Doe: 1500.0

💻 Package-private Access
// Package-private Access Example
import java.util.Scanner;

class PackagePrivateExample {
    String defaultVariable = "This is a package-private variable";

    void defaultMethod() {
        System.out.println("This is a package-private method");
    }
}

public class Main {
    public static void main(String[] args) {
        PackagePrivateExample obj = new PackagePrivateExample();

        System.out.println(obj.defaultVariable);
        obj.defaultMethod();
    }
}
📤 Output:
This is a package-private variable
This is a package-private method

💻 Public, Private, Protected Access
import java.util.Scanner;

class AccessModifiersExample {

    public String publicVariable = "This is public";
    private String privateVariable = "This is private";
    protected String protectedVariable = "This is protected";

    public void publicMethod() {
        System.out.println("Public method can be accessed from anywhere.");
        System.out.println(privateVariable); // Accessing private variable within the class
    }

    private void privateMethod() {
        System.out.println("Private method can only be accessed within the class.");
    }

    protected void protectedMethod() {
        System.out.println("Protected method can be accessed within the class and subclass.");
    }

    public void accessPrivateMethod() {
        privateMethod(); // Accessing private method within the class
    }
}

class SubClass extends AccessModifiersExample {
    public void accessProtected() {
        System.out.println("Accessing protected variable from subclass: " + protectedVariable);
        protectedMethod(); // Accessing protected method from subclass
    }
}

public class EncapsulationDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        AccessModifiersExample obj = new AccessModifiersExample();

        System.out.println("Accessing public variable: " + obj.publicVariable);
        obj.publicMethod();

        SubClass subObj = new SubClass();
        subObj.accessProtected();

        obj.accessPrivateMethod();
        scanner.close();
    }
}
📤 Output:
Accessing public variable: This is public
Public method can be accessed from anywhere.
This is private
Accessing protected variable from subclass: This is protected
Protected method can be accessed within the class and subclass.
Private method can only be accessed within the class.

💻 Package and Access Modifiers
package com.example.oop;

import java.util.Scanner;

public class PackageAndAccessModifiers {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Let's explore packages and access modifiers!");

        // Example using a class from the same package
        Student student1 = new Student("Rohan", 20);
        System.out.println("Student Name: " + student1.getName());
        student1.setName("Rohan Sharma");
        System.out.println("Updated Student Name: " + student1.getName());

        //Example using a class with default access modifier
        Book book1 = new Book("The Secret Garden", "Frances Hodgson Burnett");
        book1.displayBookDetails();

        scanner.close();
    }
}

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Book { // Class with default access modifier
    String title;
    String author;

    Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    void displayBookDetails() { // Method with default access modifier
        System.out.println("Book Title: " + title);
        System.out.println("Book Author: " + author);
    }
}
📤 Output:
Let's explore packages and access modifiers!
Student Name: Rohan
Updated Student Name: Rohan Sharma
Book Title: The Secret Garden
Book Author: Frances Hodgson Burnett

💻 Immutable Class Implementation
import java.util.Scanner;

public class ImmutableStudent {

    private final String name;
    private final int rollNumber;

    public ImmutableStudent(String name, int rollNumber) {
        this.name = name;
        this.rollNumber = rollNumber;
    }

    public String getName() {
        return name;
    }

    public int getRollNumber() {
        return rollNumber;
    }

    @Override
    public String toString() {
        return "Student Name: " + name + ", Roll Number: " + rollNumber;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter student name: ");
        String studentName = scanner.nextLine();

        System.out.print("Enter roll number: ");
        int studentRollNumber = scanner.nextInt();

        ImmutableStudent student = new ImmutableStudent(studentName, studentRollNumber);

        System.out.println(student);

        scanner.close();
    }
}
📤 Output:
Input: John Doe
Input: 123
Output: Student Name: John Doe, Roll Number: 123

💻 Bean Class Implementation
import java.util.Scanner;

public class EmployeeBean {

    private int employeeId;
    private String employeeName;
    private double employeeSalary;

    public EmployeeBean() {
        // Default constructor
    }

    public int getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public double getEmployeeSalary() {
        return employeeSalary;
    }

    public void setEmployeeSalary(double employeeSalary) {
        this.employeeSalary = employeeSalary;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        EmployeeBean employee = new EmployeeBean();

        System.out.print("Enter employee ID: ");
        employee.setEmployeeId(scanner.nextInt());
        scanner.nextLine(); // Consume newline

        System.out.print("Enter employee name: ");
        employee.setEmployeeName(scanner.nextLine());

        System.out.print("Enter employee salary: ");
        employee.setEmployeeSalary(scanner.nextDouble());

        System.out.println("nEmployee Details:");
        System.out.println("ID: " + employee.getEmployeeId());
        System.out.println("Name: " + employee.getEmployeeName());
        System.out.println("Salary: " + employee.getEmployeeSalary());

        scanner.close();
    }
}
📤 Output:
Input: 123
Input: John Doe
Input: 50000.00
Output: Enter employee ID: Enter employee name: Enter employee salary:
Employee Details:
ID: 123
Name: John Doe
Salary: 50000.0

💻 Write-only Class Implementation
import java.util.Scanner;

public class WriteOnlyCounter {

    private int counter;

    public WriteOnlyCounter() {
        this.counter = 0;
    }

    public void incrementCounter(int value) {
        if (value > 0) {
            this.counter += value;
            System.out.println("Counter incremented by " + value);
        } else {
            System.out.println("Invalid increment value. Please enter a positive value.");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        WriteOnlyCounter myCounter = new WriteOnlyCounter();

        System.out.print("Enter the value to increment the counter: ");
        int incrementValue = scanner.nextInt();

        myCounter.incrementCounter(incrementValue);

        scanner.close();
    }
}
📤 Output:
Input: 5
Output: Counter incremented by 5

Input: -2
Output: Invalid increment value. Please enter a positive value.