ch
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
帖子存档
💻 Interface Implementation
import java.util.Scanner;

interface Drawable {
    void draw();
}

class Circle implements Drawable {
    private int radius;

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

    @Override
    public void draw() {
        System.out.println("Drawing a circle with radius: " + radius);
    }
}

class Rectangle implements Drawable {
    private int length;
    private int width;

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

    @Override
    public void draw() {
        System.out.println("Drawing a rectangle with length: " + length + " and width: " + width);
    }
}

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

        System.out.print("Enter the radius of the circle: ");
        int circleRadius = scanner.nextInt();
        Drawable circle = new Circle(circleRadius);
        circle.draw();

        System.out.print("Enter the length of the rectangle: ");
        int rectangleLength = scanner.nextInt();
        System.out.print("Enter the width of the rectangle: ");
        int rectangleWidth = scanner.nextInt();
        Drawable rectangle = new Rectangle(rectangleLength, rectangleWidth);
        rectangle.draw();

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

💻 Abstract Class Implementation
import java.util.Scanner;

abstract class Shape {
    protected String color;

    public Shape(String color) {
        this.color = color;
    }

    // Abstract method to calculate area
    public abstract double calculateArea();

    public String getColor() {
        return color;
    }
}

class Circle extends Shape {
    private double radius;

    public Circle(String color, double radius) {
        super(color);
        this.radius = radius;
    }

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

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

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

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

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

        System.out.print("Enter the color of the circle: ");
        String circleColor = scanner.next();
        System.out.print("Enter the radius of the circle: ");
        double circleRadius = scanner.nextDouble();

        Circle circle = new Circle(circleColor, circleRadius);
        System.out.println("Circle Area: " + circle.calculateArea());
        System.out.println("Circle Color: " + circle.getColor());

        System.out.print("Enter the color of the rectangle: ");
        String rectangleColor = scanner.next();
        System.out.print("Enter the length of the rectangle: ");
        double rectangleLength = scanner.nextDouble();
        System.out.print("Enter the width of the rectangle: ");
        double rectangleWidth = scanner.nextDouble();

        Rectangle rectangle = new Rectangle(rectangleColor, rectangleLength, rectangleWidth);
        System.out.println("Rectangle Area: " + rectangle.calculateArea());
        System.out.println("Rectangle Color: " + rectangle.getColor());

        scanner.close();
    }
}
📤 Output:
Input: red
Input: 5
Input: blue
Input: 4
Input: 6
Output: Enter the color of the circle: Enter the radius of the circle: Circle Area: 78.53981633974483
Output: Circle Color: red
Output: Enter the color of the rectangle: Enter the length of the rectangle: Enter the width of the rectangle: Rectangle Area: 24.0
Output: Rectangle Color: blue

💻 Final Keyword with Inheritance
import java.util.Scanner;

class FinalKeywordInheritance {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Demonstrating final keyword with inheritance");

        // Example 1: Final class
        // If a class is declared as final, it cannot be inherited.
        // We will show a class that is not final, and then demonstrate a final class.

        System.out.println("nExample 1: Non-final and final classes");

        class Animal {
            public void makeSound() {
                System.out.println("Generic animal sound");
            }
        }

        class Dog extends Animal {
            @Override
            public void makeSound() {
                System.out.println("Woof!");
            }
        }

        final class Cat extends Animal {
            @Override
            public void makeSound() {
                System.out.println("Meow!");
            }
        }

        Animal myAnimal = new Animal();
        Dog myDog = new Dog();
        Cat myCat = new Cat();

        myAnimal.makeSound();
        myDog.makeSound();
        myCat.makeSound();

        //The following code will give error as Cat class is final
        //class Kitten extends Cat{}

        // Example 2: Final method
        // If a method is declared as final, it cannot be overridden in subclasses.

        System.out.println("nExample 2: Final method");

        class Vehicle {
            public final void startEngine() {
                System.out.println("Engine started");
            }
        }

        class Car extends Vehicle {
            // Attempting to override startEngine() will result in a compilation error
            // @Override
            // public void startEngine() {
            //     System.out.println("Car engine started"); // This will cause an error
            // }

            public void drive() {
                startEngine(); // Calling the final method from the superclass
                System.out.println("Driving the car");
            }
        }

        Car myCar = new Car();
        myCar.drive();

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

💻 Super Keyword Usage
import java.util.Scanner;

class Animal {
    String colour = "White";

    void eat() {
        System.out.println("Animal is eating.");
    }
}

class Dog extends Animal {
    String colour = "Black";

    void printColour() {
        System.out.println("Dog colour: " + colour);
        System.out.println("Animal colour: " + super.colour); // Accessing parent class's colour using super
    }

    void eat() {
        super.eat(); // Calling parent class's eat method using super
        System.out.println("Dog is eating.");
    }
}

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

        Dog dog = new Dog();
        dog.printColour();
        dog.eat();

        scanner.close();
    }
}
📤 Output:
Dog colour: Black
Animal colour: White
Animal is eating.
Dog is eating.

💻 Method Overriding
import java.util.Scanner;

class Animal {
    public void makeSound() {
        System.out.println("Generic animal sound");
    }
}

class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Woof!");
    }
}

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Meow!");
    }
}

public class MethodOverridingExample {
    public static void main(String[] args) {
        Animal animal = new Animal();
        Dog dog = new Dog();
        Cat cat = new Cat();

        animal.makeSound();
        dog.makeSound();
        cat.makeSound();
    }
}
📤 Output:
Generic animal sound
Woof!
Meow!

💻 Hierarchical Inheritance Implementation
import java.util.Scanner;

class Animal {
    String name;

    public void eat() {
        System.out.println("Animal is eating");
    }
}

class Dog extends Animal {
    public void bark() {
        System.out.println("Dog is barking");
    }
}

class Cat extends Animal {
    public void meow() {
        System.out.println("Cat is meowing");
    }
}

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

        Dog myDog = new Dog();
        myDog.name = "Tommy";
        myDog.eat();
        myDog.bark();

        Cat myCat = new Cat();
        myCat.name = "Whiskers";
        myCat.eat();
        myCat.meow();

        scanner.close();
    }
}
📤 Output:
Animal is eating
Dog is barking
Animal is eating
Cat is meowing

💻 Multilevel Inheritance Implementation
import java.util.Scanner;

class Animal {
    String name;

    public void eat() {
        System.out.println("Animal is eating.");
    }
}

class Dog extends Animal {
    String breed;

    public void bark() {
        System.out.println("Dog is barking.");
    }
}

class Labrador extends Dog {
    String color;

    public void displayDetails() {
        System.out.println("Breed: " + breed);
        System.out.println("Color: " + color);
    }
}

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

        Labrador myLabrador = new Labrador();

        System.out.print("Enter breed of labrador: ");
        myLabrador.breed = scanner.nextLine();

        System.out.print("Enter color of labrador: ");
        myLabrador.color = scanner.nextLine();

        myLabrador.eat();
        myLabrador.bark();
        myLabrador.displayDetails();

        scanner.close();
    }
}
📤 Output:
Input: Golden Retriever
Input: Golden

Animal is eating.
Dog is barking.
Breed: Golden Retriever
Color: Golden

💻 Single Inheritance Implementation
import java.util.Scanner;

class Animal {
    String name;

    public void eat() {
        System.out.println("Animal is eating.");
    }
}

class Dog extends Animal {
    String breed;

    public void bark() {
        System.out.println("Woof!");
    }
}

public class SingleInheritanceExample {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.name = "Tommy";
        myDog.breed = "Labrador";

        System.out.println("Dog's name: " + myDog.name);
        System.out.println("Dog's breed: " + myDog.breed);

        myDog.eat(); // Inherited from Animal class
        myDog.bark();
    }
}
📤 Output:
Dog's name: Tommy
Dog's breed: Labrador
Animal is eating.
Woof!

OOP - Inheritance

💻 Constructor vs Method Comparison
import java.util.Scanner;

public class ConstructorVsMethod {

    String name;

    // Constructor
    public ConstructorVsMethod() {
        name = "Default Name";
        System.out.println("Constructor is called. Name is initialized.");
    }

    // Method
    public void setName(String newName) {
        name = newName;
        System.out.println("setName method is called. Name is updated.");
    }

    public String getName() {
        return name;
    }

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

        ConstructorVsMethod obj = new ConstructorVsMethod(); // Constructor called here

        System.out.println("Current name: " + obj.getName());

        System.out.print("Enter new name: ");
        String newName = scanner.nextLine();

        obj.setName(newName); // Method called here

        System.out.println("Updated name: " + obj.getName());

        scanner.close();
    }
}
📤 Output:
Constructor is called. Name is initialized.
Current name: Default Name
Input: John Doe
Output: setName method is called. Name is updated.
Updated name: John Doe

💻 Object Initialization through Constructors
import java.util.Scanner;

public class StudentConstructor {
    String studentName;
    int studentAge;
    String studentCourse;

    // Default constructor (no arguments)
    public StudentConstructor() {
        studentName = "Unknown";
        studentAge = 0;
        studentCourse = "Not Assigned";
        System.out.println("Default constructor called.");
    }

    // Parameterized constructor (with arguments)
    public StudentConstructor(String name, int age, String course) {
        studentName = name;
        studentAge = age;
        studentCourse = course;
        System.out.println("Parameterized constructor called.");
    }

    public void displayStudentDetails() {
        System.out.println("Student Name: " + studentName);
        System.out.println("Student Age: " + studentAge);
        System.out.println("Student Course: " + studentCourse);
    }

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

        // Creating objects using constructors
        StudentConstructor student1 = new StudentConstructor(); // Using default constructor
        System.out.println("Student 1 Details:");
        student1.displayStudentDetails();
        System.out.println();

        System.out.print("Enter Student 2 Name: ");
        String name = scanner.nextLine();
        System.out.print("Enter Student 2 Age: ");
        int age = scanner.nextInt();
        scanner.nextLine(); // Consume newline character
        System.out.print("Enter Student 2 Course: ");
        String course = scanner.nextLine();

        StudentConstructor student2 = new StudentConstructor(name, age, course); // Using parameterized constructor
        System.out.println("Student 2 Details:");
        student2.displayStudentDetails();

        scanner.close();
    }
}
📤 Output:
Default constructor called.
Student 1 Details:
Student Name: Unknown
Student Age: 0
Student Course: Not Assigned

Input: John Doe
Input: 20
Input: Computer Science
Parameterized constructor called.
Student 2 Details:
Student Name: John Doe
Student Age: 20
Student Course: Computer Science

💻 Constructor with Default Values
import java.util.Scanner;

public class Product {

    private String name;
    private double price;
    private int quantity;

    public Product() {
        // Default values
        this.name = "Unknown";
        this.price = 0.0;
        this.quantity = 0;
    }

    public Product(String name, double price, int quantity) {
        this.name = name;
        this.price = price;
        this.quantity = quantity;
    }

    public void displayProductDetails() {
        System.out.println("Product Name: " + this.name);
        System.out.println("Price: " + this.price);
        System.out.println("Quantity: " + this.quantity);
    }

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

        // Creating a product using the default constructor
        Product defaultProduct = new Product();
        System.out.println("Default Product Details:");
        defaultProduct.displayProductDetails();
        System.out.println();

        // Creating a product using the parameterized constructor
        System.out.print("Enter product name: ");
        String productName = scanner.nextLine();
        System.out.print("Enter product price: ");
        double productPrice = scanner.nextDouble();
        System.out.print("Enter product quantity: ");
        int productQuantity = scanner.nextInt();

        Product customProduct = new Product(productName, productPrice, productQuantity);
        System.out.println("nCustom Product Details:");
        customProduct.displayProductDetails();

        scanner.close();
    }
}
📤 Output:
Default Product Details:
Product Name: Unknown
Price: 0.0
Quantity: 0

Enter product name: Apple
Enter product price: 1.0
Enter product quantity: 10

Custom Product Details:
Product Name: Apple
Price: 1.0
Quantity: 10

💻 Singleton Pattern using Private Constructor
import java.util.Scanner;

public class SingletonWithPrivateConstructor {

    private static SingletonWithPrivateConstructor instance;
    private String data;

    private SingletonWithPrivateConstructor(String data) {
        this.data = data;
    }

    public static SingletonWithPrivateConstructor getInstance(String data) {
        if (instance == null) {
            instance = new SingletonWithPrivateConstructor(data);
        }
        return instance;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

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

        System.out.print("Enter some data for the Singleton: ");
        String inputData = scanner.nextLine();

        SingletonWithPrivateConstructor singleton1 = SingletonWithPrivateConstructor.getInstance(inputData);
        System.out.println("Singleton 1 Data: " + singleton1.getData());

        System.out.print("Enter some new data to update: ");
        String newData = scanner.nextLine();
        singleton1.setData(newData);

        SingletonWithPrivateConstructor singleton2 = SingletonWithPrivateConstructor.getInstance("This will not be used");
        System.out.println("Singleton 2 Data: " + singleton2.getData());

        scanner.close();
    }
}
📤 Output:
Input: Initial Data
Output: Enter some data for the Singleton: Singleton 1 Data: Initial Data
Input: Updated Data
Output: Enter some new data to update: Singleton 2 Data: Updated Data

💻 Private Constructor Implementation
import java.util.Scanner;

class PrivateConstructorExample {

    private static PrivateConstructorExample instance;
    private String message;

    private PrivateConstructorExample(String message) {
        this.message = message;
    }

    public static PrivateConstructorExample getInstance(String message) {
        if (instance == null) {
            instance = new PrivateConstructorExample(message);
        }
        return instance;
    }

    public String getMessage() {
        return message;
    }
}

public class PrivateConstructor {

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

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

        // Getting the instance using the static method
        PrivateConstructorExample obj = PrivateConstructorExample.getInstance(inputMessage);

        System.out.println("Message: " + obj.getMessage());

        // Trying to create another instance will return the same object.
        PrivateConstructorExample obj2 = PrivateConstructorExample.getInstance("Different message");
        System.out.println("Message from second object: " + obj2.getMessage()); // Will still print the first message.

        scanner.close();
    }
}
📤 Output:
Input: Hello, World!
Output: Enter a message: Hello, World!
Message: Hello, World!
Message from second object: Hello, World!

💻 Constructor Chaining
import java.util.Scanner;

public class ConstructorChainingExample {

    private String name;
    private int age;
    private String city;

    public ConstructorChainingExample() {
        this("Unknown", 0, "Unknown");
    }

    public ConstructorChainingExample(String name) {
        this(name, 0, "Unknown");
    }

    public ConstructorChainingExample(String name, int age) {
        this(name, age, "Unknown");
    }

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

    public void displayDetails() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("City: " + city);
    }

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

        System.out.println("Creating object using default constructor:");
        ConstructorChainingExample obj1 = new ConstructorChainingExample();
        obj1.displayDetails();
        System.out.println();

        System.out.println("Creating object using constructor with name:");
        ConstructorChainingExample obj2 = new ConstructorChainingExample("Raju");
        obj2.displayDetails();
        System.out.println();

        System.out.println("Creating object using constructor with name and age:");
        ConstructorChainingExample obj3 = new ConstructorChainingExample("Seeta", 25);
        obj3.displayDetails();
        System.out.println();

        System.out.println("Creating object using constructor with name, age, and city:");
        ConstructorChainingExample obj4 = new ConstructorChainingExample("Geeta", 30, "Delhi");
        obj4.displayDetails();

        scanner.close();
    }
}
📤 Output:
Creating object using default constructor:
Name: Unknown
Age: 0
City: Unknown

Creating object using constructor with name:
Name: Raju
Age: 0
City: Unknown

Creating object using constructor with name and age:
Name: Seeta
Age: 25
City: Unknown

Creating object using constructor with name, age, and city:
Name: Geeta
Age: 30
City: Delhi

💻 Constructor Overloading
import java.util.Scanner;

public class ConstructorOverloadingExample {

    private String name;
    private int age;

    public ConstructorOverloadingExample() {
        name = "Unknown";
        age = 0;
    }

    public ConstructorOverloadingExample(String name) {
        this.name = name;
        this.age = 0;
    }

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

    public void displayDetails() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }

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

        ConstructorOverloadingExample person1 = new ConstructorOverloadingExample();
        System.out.println("Person 1 Details:");
        person1.displayDetails();
        System.out.println();

        ConstructorOverloadingExample person2 = new ConstructorOverloadingExample("Rohan");
        System.out.println("Person 2 Details:");
        person2.displayDetails();
        System.out.println();

        ConstructorOverloadingExample person3 = new ConstructorOverloadingExample("Priya", 25);
        System.out.println("Person 3 Details:");
        person3.displayDetails();

        scanner.close();
    }
}
📤 Output:
Person 1 Details:
Name: Unknown
Age: 0

Person 2 Details:
Name: Rohan
Age: 0

Person 3 Details:
Name: Priya
Age: 25

💻 Copy Constructor Implementation
import java.util.Scanner;

public class CopyConstructorImplementation {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

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

        System.out.print("Enter age: ");
        int age = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        Person originalPerson = new Person(name, age);

        // Create a copy using the copy constructor
        Person copiedPerson = new Person(originalPerson);

        System.out.println("Original Person: " + originalPerson.name + ", " + originalPerson.age);
        System.out.println("Copied Person: " + copiedPerson.name + ", " + copiedPerson.age);

        scanner.close();
    }
}

class Person {
    String name;
    int age;

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

    // Copy constructor
    public Person(Person other) {
        this.name = other.name;
        this.age = other.age;
    }
}
📤 Output:
Input: John Doe
Input: 30
Output: Enter name: Enter age: Original Person: John Doe, 30
Output: Copied Person: John Doe, 30

💻 Parameterized Constructor Implementation
import java.util.Scanner;

public class ParameterizedConstructorExample {

    String name;
    int age;

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

    public void displayDetails() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }

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

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

        System.out.print("Enter age: ");
        int age = scanner.nextInt();
        scanner.nextLine(); // Consume newline left-over

        ParameterizedConstructorExample person = new ParameterizedConstructorExample(name, age);

        person.displayDetails();

        scanner.close();
    }
}
📤 Output:
Input: John Doe
Input: 30
Output: Name: John Doe
Output: Age: 30

💻 Default Constructor Implementation
import java.util.Scanner;

public class DefaultConstructorExample {

    private String name;
    private int age;

    public DefaultConstructorExample() {
        name = "Unknown";
        age = 0;
        System.out.println("Default constructor called.");
    }

    public void displayDetails() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }

    public static void main(String[] args) {
        DefaultConstructorExample obj = new DefaultConstructorExample(); // Creates object using default constructor
        obj.displayDetails();
    }
}
📤 Output:
Default constructor called.
Name: Unknown
Age: 0

OOP - Constructors