1. class
A template for creating objects (like a blueprint).
class Car {
String color;
}
2. instance
An actual object created from a class.
Car myCar = new Car(); // myCar is on instance of Car
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
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!
5. getter
Method to get a private variable's value.
class Person {
private String name;
public String getName() {
return this.name;
}
}
6. setter
Method to set a private variable's value.
class Person {
private String name;
public void setName(String n) {
this.name = n;
}
}
7. try-catch statement
Handles errors (exceptions).
try {
int a = 10 / 0;
} catch (Exception e) {
System.out.println("Error occurred");
}
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;
}
9. package
Groups related classes together.
package myapp.utils;
10. import
Brings other classes into your file.
import java.util.Scanner;
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;
}
}
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");
}
}
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");
14. enum
Fixed set of constants.
enum Day { MONDAY, TUESDAY, WEDNESDAY }
Day day = Day.MONDAY;
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");
}
}
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");
}
}
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();
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();
Top comments (0)