0% found this document useful (0 votes)
23 views25 pages

Java-Reporter-1 20241207 093027 0000

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views25 pages

Java-Reporter-1 20241207 093027 0000

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Group 1

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

1. *Cannot be instantiated*: You cannot create objects directly


from an abstract class.
2. *Must be inherited*: Abstract classes are designed to be
inherited by concrete classes.
3. *Can have abstract methods*: Methods declared but not
implemented.
4. *Can have concrete methods*: Fully implemented methods.
5. *Can have properties and fields*: Like regular classes.
Purpose

1. *Provide a blueprint*: Abstract classes define


a common structure for related classes.
2. *Encapsulate shared behavior*: Implement
common methods and properties.
3. *Promote code reuse*: Inherited classes can
build upon the abstract class's implementation.
4. *Define an interface*: Abstract classes can
specify methods that must be implemented by
derived classes.
class Shape(ABC):
def __init__(self, color):
self.color = color

@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

# Instantiating abstract class raises error


try:
shape = Shape("red")
except TypeError as e:
print(e)

# Instantiating concrete classes


circle = Circle(5, "blue")
print(circle.area())
circle.display_color()

rectangle = Rectangle(4, 6, "green")


print(rectangle.area())
rectangle.display_color()
Interfaces

An interface in the Java programming language is


an abstract type that is used to declare a behavior
that classes must implement. They are similar to
protocols. Interfaces are declared using the
interface keyword, and may only contain method
signature and constant declarations.
// Interface
interface Animal {
public void animalSound(); // interface

Example:
method (does not have a body)
public void sleep(); // interface method
(does not have a body)
}

// Pig "implements" the Animal interface


class Pig implements Animal {
public void animalSound() {
// The body of animalSound() is provided
here
System.out.println("The pig says: wee
wee");
}
public void sleep() {
// The body of sleep() is provided here
System.out.println("Zzz");
}
}

class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig
object
myPig.animalSound();
}
myPig.sleep();

}
Class casting

Class casting is a fundamental concept in object-oriented


programming, particularly in languages like Java and C++. It involves
changing the type of a reference variable to a different type within the
class hierarchy. This process allows for greater flexibility and code
reusability by enabling objects to be treated as instances of their
parent or child classes.
There are two primary types of casting in
object-oriented programming:

3. Implicit Casting (Upcasting): This happens


automatically by the compiler when a subclass
object is assigned to a superclass reference.

Types of It's a seamless process that ensures


compatibility within the inheritance hierarchy.
casting 4. Explicit Casting (Downcasting): This
requires the programmer to explicitly use a
cast operator to convert a superclass
reference to a subclass reference. It's crucial
to use the instanceof operator to verify the
object's type before downcasting to prevent
runtime errors.
Comparison

In Java, comparison refers to


evaluating two values, objects, or
data structures to determine their
equality or relational order. There
are several ways to perform
comparisons in Java, depending
on the type of data being
compared.
Here's an
overview:
1. Primitive Data Types

Use relational operators (==, !=, <, >, <=,


>=) for comparing numeric or boolean
values.

Example:

int a = 5;
int b = 10;
System.out.println(a == b); // false
System.out.println(a < b); // true
2. Strings

Use the .equals() method to compare string content.

Use .compareTo() for lexicographical order.

Example:

String str1 = "Hello";


String str2 = "World";

System.out.println(str1.equals(str2)); // false
System.out.println(str1.compareTo(str2)); // Negative value (str1 < str2)
3. Objects

Use .equals() to compare the contents (if overridden in the class).

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;
}
}

Point p1 = new Point(1, 2);


Point p2 = new Point(1, 2);

System.out.println(p1 == p2); // false (different memory addresses)


System.out.println(p1.equals(p2)); // false (unless overridden)
4. Custom Objects (Override equals and compareTo)

Implement equals() for logical equality.

Implement compareTo() (from Comparable interface) for sorting order.

Example:

class Person implements Comparable<Person> {


String name;
int age;

Person(String name, int age) {


this.name = name;
this.age = age;
}

@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

Use Collections.sort() with a comparator or natural ordering.

Example:

List<Integer> numbers = Arrays.asList(5, 2, 8, 1);


Collections.sort(numbers);
System.out.println(numbers); // [1, 2, 5, 8]
Java
Conversion
can refer to:
# Data Type Conversions

1. *Primitive conversions*: byte, short, int,


long, float, double, boolean, char.
2. *Wrapper class conversions*: Integer,
Double, Boolean, etc.
3. *String conversions*: parsing (e.g.,
Integer.parseInt()).
# Programming Conversions

1. *Java to other languages*: C#, Python,


JavaScript, etc.
2. *Other languages to Java*: migration,
porting.
3. *Legacy system conversions*: upgrading
outdated Java versions.
# File Format Conversions

1. *JSON to Java objects*: Jackson, Gson libraries.


2. *XML to Java objects*: JAXB, SAX.
3. *CSV, Excel to Java*: Apache POI, OpenCSV.
# Tools and Techniques

1. *Java Converters*: online tools.


2. *IDE plugins*: Eclipse, IntelliJ.
3. *Command-line utilities*: javap, java.
Group 1
Reporter

Emerson John B. Villena


Gardson R. Calunsag
Rheyniel P. Roxas
Lovely A. Balasa
Arlyn C.Monsulller
Grace Quiño

You might also like