Java OOP Compiler
Object-Oriented Programming is the heart of Java. These runnable examples cover the four pillars — edit them in the browser to see polymorphism and encapsulation in action.
Run Java OOP Compiler examples in the Java compiler
Examples included
Practical tips
- Single Java file can have multiple classes — only one is public, and it must match the filename. Our compiler uses Main as the public class.
- Use @Override on methods you mean to override. The compiler will catch typos.
- Prefer composition over inheritance when behavior, not identity, is being shared.
Common questions
Can I define multiple classes in one Java file?
Yes. Only the public class must match the filename. Non-public classes can live alongside Main.
What's the difference between abstract class and interface?
Interfaces only declare contracts (and default methods). Abstract classes can hold state and constructors.
Practice related Java exercises · Browse Java programs with output