Java_Programs_Examples
Java_Programs_Examples
3. Method Overloading
class OverloadExample {
void show(int a) {
System.out.println("Int: " + a);
}
void show(String a) {
System.out.println("String: " + a);
}
4. Method Overriding
class Parent {
void show() {
System.out.println("Parent's show()");
}
}
Java Programming Examples
class Child extends Parent {
void show() {
System.out.println("Child's show()");
}
public static void main(String[] args) {
Parent obj = new Child();
obj.show();
}
}
// Output: Child's show()
interface Vehicle {
void drive();
}
class Engine {
void start() {
System.out.println("Engine Started");
}
}
class ThisExample {
int a;
ThisExample(int a) {
this.a = a;
}
void display() {
System.out.println("Value: " + this.a);
}
Java Programming Examples
7. Exception Handling
8. Thread Synchronization
class Counter {
int count = 0;
SyncThread(Counter c) {
this.counter = c;
}
class Shared {
boolean flag = false;
class Person {
String name;
int age;
Java Programming Examples
Person(String name) {
this.name = name;
this.age = 18; // default
}
void show() {
System.out.println(name + " - " + age);
}