Learn Java ANONYMOUS CLASSES in 5 minutes!

6 Views
Published
#java #javatutorial #javacourse

public class Main {
public static void main(String[] args) {

// Anonymous class = A class that doesn't have a name. Cannot be reused.
// Add custom behavior without having to create a new class.
// Often used for one time uses (TimerTask, Runnable, callbacks)

Dog dog1 = new Dog();
Dog dog2 = new Dog(){
@Override
void speak(){
System.out.println("Scooby Doo says *Ruh Roh*");
}
};

dog1.speak();
dog2.speak();
}
}

public class Dog {

void speak(){
System.out.println("The dog goes *woof*");
}
}
Category
Bro Code
Tags
java tutorial, java course, java programming
Be the first to comment