Task 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Object-Oriented Programming (OOP)

1. Classes and Objects: Creating classes, defining attributes and methods,


understanding constructors.
public class Main {
// Class attribute
static String species = "Canis familiaris";
// Instance attributes
String name;
int age;
// Constructor
public Main(String name, int age) {
this.name = name;
this.age = age;
}
// Instance method
public String description() {
return name + " is " + age + " years old";
}
// Another instance method
public String speak(String sound) {
return name + " says " + sound;
}
public static void main(String[] args) {
// Creating an object of the Dog class
Main myDog = new Main("Buddy", 3);
// Accessing class attributes
System.out.println(Main.species); // Output: Canis familiaris
// Accessing instance attributes
System.out.println(myDog.name);
System.out.println(myDog.age);
// Calling instance methods
System.out.println(myDog.description()); // Output: Buddy is 3 years old
System.out.println(myDog.speak("Woof Woof")); // Output: Buddy says Woof Woof
}
}

OUTPUT

2. Inheritance: Extending classes, method overriding, and understanding


superclass-subclass relationships.
class Animal {
String name;
public Animal(String name) {
this.name = name;}
public void eat() {
System.out.println(name + " is eating");}
public void makeSound() {
System.out.println(name + " is making a sound");
}
}
//extends class
class Dog extends Animal {
String breed;
public Dog(String name, String breed) {
super(name); // Calling the superclass constructor
this.breed = breed; }
@Override
public void makeSound() {
super.makeSound(); // Calling the superclass method
System.out.println(name + " says Woof Woof");}
public void bark() {
System.out.println(name + " is barking");
}
}
public class Main {
public static void main(String[] args) {
// Creating an object of the Dog class
Dog myDog = new Dog("Buddy", "Golden Retriever");
// Accessing superclass methods
myDog.eat();
// Accessing subclass methods
myDog.bark();
// Overridden method
myDog.makeSound();
}
}
OUTPUT
Polymorphism: Implementing polymorphic behavior using interfaces and
method overriding.
// Interface definition
interface Animal {
void makeSound();
}
// Abstract class implementing the interface
abstract class AbstractAnimal implements Animal {
String name;
AbstractAnimal(String name) {
this.name = name;
}
// Concrete method
public void eat() {
System.out.println(name + " is eating");
}
}
// Dog class extending AbstractAnimal and implementing makeSound method
class Dog extends AbstractAnimal {
Dog(String name) {
super(name);
}
@Override
public void makeSound() {
System.out.println(name + " says Woof Woof");
}
}
// Cat class extending AbstractAnimal and implementing makeSound method
class Cat extends AbstractAnimal {
Cat(String name) {
super(name);
}
@Override
public void makeSound() {
System.out.println(name + " says Meow Meow");
}
}
// Main class to test polymorphic behavior
public class Main {
public static void main(String[] args) {
// Creating instances of Dog and Cat
Animal myDog = new Dog("Buddy");
Animal myCat = new Cat("Whiskers");
// Accessing methods through interface reference
myDog.makeSound();
myCat.makeSound();
// Accessing methods through abstract class reference
AbstractAnimal abstractDog = (AbstractAnimal) myDog;
AbstractAnimal abstractCat = (AbstractAnimal) myCat;
abstractDog.eat();
abstractCat.eat();
}
}
OUTPUT
Encapsulation and Abstraction: Applying principles to design classes that
are modular and reusable
// Abstract class for Employees
abstract class Employee {
private String name;
private int id;
// Constructor
public Employee(String name, int id) {
this.name = name;
this.id = id;}
// Getter for name
public String getName() {
return name; }
// Getter for id
public int getId() {
return id;}
// Abstract method to be implemented by subclasses
public abstract void work();
}
// Concrete subclass for Managers
class Manager extends Employee {
private int teamSize;
// Constructor
public Manager(String name, int id, int teamSize) {
super(name, id);
this.teamSize = teamSize;
}
// Getter for team size
public int getTeamSize() {
return teamSize;
}
// Implementing the abstract method
@Override
public void work() {
System.out.println(getName() + " is managing a team of " + teamSize + " people.");
}
}
// Main class to test the program
public class Main {
public static void main(String[] args) {
// Creating an instance of Manager
Manager manager = new Manager("Alice", 101, 5);
// Accessing properties and methods
System.out.println("Manager Name: " + manager.getName());
System.out.println("Manager ID: " + manager.getId());
System.out.println("Manager Team Size: " + manager.getTeamSize());
// Calling the work method
manager.work(); // Output: Alice is managing a team of 5 people.
}
}
OUTPUT

You might also like