DEV Community

Shelner
Shelner

Posted on

Java basic knowledge

1. class

A template for creating objects (like a blueprint).

class Car {
    String color;
}
Enter fullscreen mode Exit fullscreen mode

2. instance

An actual object created from a class.

Car myCar = new Car(); // myCar is on instance of Car
Enter fullscreen mode Exit fullscreen mode

3. polymorphism

Same method name works differently based on object type.

class Animal {
    void sound() {
        System.out.println("Some sound");
    }
}
class Dog extends Animal {
    void sound() {
        System.out.println("Bark");
    }
}
Animal a = new Dog();
a.sound(); // Outputs "Bark" -> polymorphism
Enter fullscreen mode Exit fullscreen mode

4. constructor

Special method that runs when an object is created.

class Car {
    Car() {
        System.out.println("Car created!");
    }
}
Car car = new Car(); // Outputs: Car created!
Enter fullscreen mode Exit fullscreen mode

5. getter

Method to get a private variable's value.

class Person {
    private String name;
    public String getName() {
        return this.name;
    }
}
Enter fullscreen mode Exit fullscreen mode

6. setter

Method to set a private variable's value.

class Person {
    private String name;
    public void setName(String n) {
        this.name = n;
    }
}
Enter fullscreen mode Exit fullscreen mode

7. try-catch statement

Handles errors (exceptions).

try {
    int a = 10 / 0;
} catch (Exception e) {
    System.out.println("Error occurred");
}
Enter fullscreen mode Exit fullscreen mode

8. switch statement

Runs different code based on value.

int day = 1;
switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    default:
        System.out.println("Sunday");
        break;
}
Enter fullscreen mode Exit fullscreen mode

9. package

Groups related classes together.

package myapp.utils;
Enter fullscreen mode Exit fullscreen mode

10. import

Brings other classes into your file.

import java.util.Scanner;
Enter fullscreen mode Exit fullscreen mode

11. overloading

Same method name with different parameters.

class Math {
    int add(int a, int b) {
        return a + b;
    }

    double add(double a, double b) {
        return a + b;
    }
}
Enter fullscreen mode Exit fullscreen mode

12. overriding

Subclass changes the parent class's method.

class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}
class Cat extends Animal {
    void sound() {
        System.out.println("Meow");
    }
}
Enter fullscreen mode Exit fullscreen mode

13. generics

Write code that works with any data type.

class Box<T> {
    T value;
    void set(T val) {
        value = val;
    }
    T get() {
        return value;
    }
}
Box<String> box = new Box<>();
box.set("Hello");
Enter fullscreen mode Exit fullscreen mode

14. enum

Fixed set of constants.

enum Day { MONDAY, TUESDAY, WEDNESDAY }
Day day = Day.MONDAY;
Enter fullscreen mode Exit fullscreen mode

15. interface

Like a contract. Classes must implement its methods.

interface Animal {
    void sound();
}
class Dog implements Animal {
    public void sound() {
        System.out.println("Bark");
    }
}
Enter fullscreen mode Exit fullscreen mode

16. abstract

Abstract class can't be directly used.
It has abstract (incomplete) methods.

abstract class Animal {
    abstract void sound();
}
class Cow extends Animal {
    void sound() {
        System.out.println("Moo");
    }
}
Enter fullscreen mode Exit fullscreen mode

17. thread execution

Runs code in parallel (at the same time).

class MyThread extends Thread {
    public void run() {
        System.out.println("Running thread");
    }
}
new MyThread().start();
Enter fullscreen mode Exit fullscreen mode

18. lambda expression

Short way to write a method (mainly for interfaces with one method).

interface Greeting {
    void sayHello();
}
Greeting g = () -> System.out.pringln("Hello");
g.sayHello();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)