



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




class Person { private String name; // private field // Getter method for name public String getName() { return name; } // Setter method for name public void setName(String name) { this.name = name; }
abstract class Animal { // Abstract method (no implementation) public abstract void sound(); // Regular method with implementation public void breathe() { System.out.println("Breathing..."); } } class Dog extends Animal { public void sound() { System.out.println("Woof"); } }
interface Animal { void sound(); // abstract method } class Dog implements Animal { public void sound() { System.out.println("Woof"); } }
class Animal { void eat() { System.out.println("Eating..."); }
public class Main { public static void main(String[] args) { Animal myAnimal = new Animal(); Animal myDog = new Dog(); myAnimal.sound(); // Outputs: Animal sound myDog.sound(); // Outputs: Woof (method overriding) } }
class Car { String model; // Default constructor Car() { model = "Unknown"; } // Parameterized constructor Car(String model) { this.model = model; } } public class Main { public static void main(String[] args) { Car car1 = new Car(); // Default constructor Car car2 = new Car("Tesla"); // Parameterized constructor System.out.println(car1.model); // Outputs: Unknown System.out.println(car2.model); // Outputs: Tesla
class Display { // Method Overloading void show(int a) { System.out.println("Integer: " + a); } void show(String b) { System.out.println("String: " + b); } // Constructor Overloading Display() { System.out.println("No-argument constructor"); } Display(String message) { System.out.println("Message: " + message); } }
class Example { static int count = 0; Example() { count++; } static void showCount() { System.out.println("Count: " + count); } } public class Main { public static void main(String[] args) { new Example(); new Example(); Example.showCount(); // Outputs: Count: 2