Java-Reporter-1 20241207 093027 0000
Java-Reporter-1 20241207 093027 0000
Report
Agenda
KEY TOPICS DISCUSSED IN
THIS PRESENTATION
Polymorphism
Introduction
Abstracts classes
Interfaces
Class casting, comparison , and conversion
Polymorphism
Polymorphism is an important concept in object-oriented programming (OOP) that enables interfaces to
allow objects of different classes to be regarded as objects of the same class. The term "polymorphism"
originates from the Greeks and can be translated as "many shapes." It refers to the ability of a single
function, method, or operator to work in multiple ways depending on the situation. There exist, broadly, two
categories of polymorphism: compile-time polymorphism (also called method overloading) and runtime
polymorphism (also referred to as method overriding.
With polymorphism, it complexity developers introduce more reusable and flexible code providing the
ability to process objects of different types through the same interface, thus enhancing productivity and
maintainability in large systems. In short, polymorphism is often used to refer to dynamic method binding,
which determines the method that will actually be invoked at runtime, making it an important concept when
designing scalable and modular
With polymorphism, it complexity developers introduce more reusable and flexible code providing the
ability to process objects of different types through the same interface, thus enhancing productivity and
maintainability in large systems. In short, polymorphism is often used to refer to dynamic method binding,
which determines the method that will actually be invoked at runtime, making it an important concept when
designing scalable and modular
Example:
Abstract classes
Definition
-An abstract class is a class that cannot be
instantiated on its own and is designed to
be inherited by other classes. It provides a
partial implementation of a class, with
some methods fully defined and others
declared but not implemented.
Characteristics
@abstractmethod
def area(self):
pass
def display_color(self):
print(f"Color: {self.color}")
class Circle(Shape):
def __init__(self, radius, color):
super().__init__(color)
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
Example:
class Rectangle(Shape):
def __init__(self, width, height, color):
super().__init__(color)
self.width = width
self.height = height
def area(self):
return self.width * self.height
Example:
method (does not have a body)
public void sleep(); // interface method
(does not have a body)
}
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig
object
myPig.animalSound();
}
myPig.sleep();
}
Class casting
Example:
int a = 5;
int b = 10;
System.out.println(a == b); // false
System.out.println(a < b); // true
2. Strings
Example:
System.out.println(str1.equals(str2)); // false
System.out.println(str1.compareTo(str2)); // Negative value (str1 < str2)
3. Objects
Use == to compare references (whether they point to the same memory address).
Example:
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Example:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && name.equals(person.name);
}
@Override
public int compareTo(Person other) {
return Integer.compare(this.age, other.age);
}
}
5. Collections
Example: