Classes & Objects (Encapsulation)
Basic introduction to encapsulation using real examples.
Inheritance Basics
Covers UML structure, extends, super, and type casting concepts.
Static & Dynamic Polymorphism
Explains compile-time and runtime polymorphism with examples.
Abstract Class & Interface
practice code
| Capability \ Animal | Human | Bird | Fish | Frog |
|---|---|---|---|---|
| Breath | ✅ | ✅ | ✅ | ✅ |
| Eat | ✅ | ✅ | ✅ | ✅ |
| Sleep | ✅ | ✅ | ✅ | ✅ |
| Walk | ✅ | ✅ | ☐ | ✅ |
| Fly | ☐ | ✅ | ☐ | ☐ |
| Swim | ☐ | ☐ | ✅ | ✅ |
Codes
// Main.java
class Main {
static void makeFly(IFly f){
f.fly();
}
public static void main(String[] args) {
Bird bird = new Bird();
bird.eat();
makeFly(bird);
IFly d = new Drone();
makeFly(d);
}
}
// Animal.java
abstract class Animal {
public abstract void eat();
public void sleep() {
System.out.println("Sleeping...");
}
}
// Bird.java
class Bird extends Animal implements IWalkable, IFly {
@Override
public void eat(){
System.out.println("Bird is eating insect.....");
}
@Override
public void walk() {
System.out.println("bird is walking slowly...");
}
@Override
public void fly() {
System.out.println("bird is flying fast...");
}
}
// Human.java
class Human extends Animal implements IWalkable {
@Override
public void eat(){
System.out.println("Human is eating biriany.....");
}
@Override
public void walk() {
System.out.println("Human walks");
}
}
Generic
Animal.java
abstract class Animal {
String name;
int age;
Animal(String name, int age) {
this.name = name;
this.age = age;
}
public abstract void eat();
public void sleep() {
System.out.println("Sleeping...");
}
}
Bird.java
class Bird extends Animal implements IWalkable, IFly {
Bird(String name, int age) {
super(name,age);
}
@Override
public void eat(){
System.out.println("Bird is eating insect.....");
}
@Override
public void walk() {
System.out.println("bird is walking slowly...");
}
@Override
public void fly() {
System.out.println("bird is flying fast...");
}
}
Capabilites.java
interface IWalkable {
void walk();
}
interface IFly {
void fly();
}
interface ISwims {
void swim();
}
Human.java
class Human extends Animal implements IWalkable {
Human(String name, int age) {
super(name, age);
}
@Override
public void eat(){
System.out.println("Human is eating biriany.....");
}
@Override
public void walk() {
System.out.println("Human walks");
}
}
Main.java
public class Main {
public static void main(String[] args) {
Bird d = new Bird("parrot", 3);
Parser<Bird> p = new Parser<>(d);
System.out.println(p.toMap());
}
}
Parser.java
import java.util.Map;
import java.util.HashMap;
public class Parser<T extends Animal> {
private T value;
public Parser(T value) {
this.value = value;
}
public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("name", value.name);
map.put("age", value.age);
return map;
}
}
Response.java
public class Response<T> {
private int statusCode;
private String message;
private T data;
public Response(int statusCode, String message, T data) {
this.statusCode = statusCode;
this.message = message;
this.data = data;
}
}
Zoo.java
import java.util.ArrayList;
import java.util.List;
public class Zoo<T extends Animal> {
private List<T> animals = new ArrayList<>();
public void add(T animal) {
animals.add(animal);
}
public List<T> getAll() {
return animals;
}
}
Collection - ArrayList, HashSet & HashMap
Main.java
import java.util.*;
public class Main {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(101, "Alice");
map.put(102, "Rahim");
map.put(101, "karim");
map.put(104, "karim");
map.remove(104);
map.containsValue("Rahim");
map.containsKey(101);
for (Integer id : map.keySet()) {
System.out.println("id:" + id + " name:" + map.get(id));
}
}
}