Cs DJ
Cs DJ
Example (Przykład)
// Constructor (Konstruktor)
public Car(String brand, int speed) {
this.brand = brand;
this.speed = speed;
}
2. Encapsulation (Enkapsulacja)
Example (Przykład)
// Constructor (Konstruktor)
public BankAccount(String accountHolder, double balance) {
this.accountHolder = accountHolder;
this.balance = balance;
}
(Prywatne pola mogą być dostępne lub modyfikowane tylko za pomocą metod publicznych.)
3. Inheritance (Dziedziczenie)
Inheritance allows a class to acquire properties and methods from another class using the
extends keyword.
(Dziedziczenie pozwala klasie na przejęcie właściwości i metod innej klasy za pomocą słowa
kluczowego extends.)
Example (Przykład)
4. Polymorphism (Polimorfizm)
(Polimorfizm pozwala jednej klasie lub interfejsowi reprezentować różne typy obiektów.)
Example (Przykład)
(Polimorfizm pozwala tej samej metodzie zachowywać się inaczej w zależności od obiektu.)
• Abstract Class: A class that cannot be instantiated and may contain abstract
methods.
(Klasa abstrakcyjna: Klasa, której nie można zainicjalizować i może zawierać metody
abstrakcyjne.)
(Interfejs: Zbiór metod abstrakcyjnych, które muszą być zaimplementowane przez klasę
używającą słowa implements.)
Example (Przykład)
// Interface (Interfejs)
interface Flyable {
void fly();
}
@Override
public void fly() {
System.out.println("Airplane is flying.");
}
}
(Metody abstrakcyjne nie mają ciała i muszą być zaimplementowane przez klasę
podrzędną.)
1. Design a class representing a Library Book with attributes like title, author,
and status (borrowed/available). Implement methods to borrow and return the book.
(Zaprojektuj klasę reprezentującą książkę biblioteczną z atrybutami jak tytuł, autor i status.
Zaimplementuj metody do wypożyczania i zwracania książki.)
2. Create a parent class called Employee and derive subclasses for Manager
and Developer. Implement methods to calculate salary using different logic for each role.
(Stwórz klasę nadrzędną o nazwie Employee i utwórz podklasy dla Manager i Developer.
Zaimplementuj metody do obliczania pensji z różną logiką dla każdej roli.)
Let me know if you’d like to see detailed solutions for any of these!