5 Lecture5
5 Lecture5
// Constructor
public Shape(String color) {
this.color = color; Assigns the passed color to
} the class's color field.
// Concrete method
public String getColor() { In this example, Shape is an abstract class with an
return color; abstract method area() and a concrete method
} getColor().
} باقي المثال في نهاية شرح
abstract
• They are functions that are defined using the keyword abstract
within abstract classes.
• They contain only the header of the function, and cannot
contain the body of the function. Their definition ends with a
semicolon. Just as an abstract class must have at least one
child class, an abstract function must It must be duplicated in
the abstract class (the parent class) and the child class derived
from it, so that it is in abstract form in the abstract class (the
parent class) and in non-abstract form in the non-abstract class
(the child class).
Abstract Class with Abstract Method
// Concrete method
public void sleep() {
System.out.println("This animal is sleeping.");
}
} In this example, Animal is an abstract class
with an abstract method makeSound() and a
concrete method sleep().
Subclass Implementing the Abstract Method
class Dog extends Animal {
// Implementing the abstract method
@Override
public void makeSound() {
System.out.println("Woof");
}
}
// Constructor
public Shape(String color) {
this.color = color; Assigns the passed color to
} the class's color field.
// Concrete method
public String getColor() {
return color;
In this example, Shape is an abstract class with an
} abstract method area() and a concrete method
} getColor().
create subclasses that provide implementations for the area() method
public class Circle extends Shape { public class Rectangle extends Shape {
double radius; double width;
double height;
Circle هي دالة بناءconstructor
Rectangle هي دالة بناءconstructor
public Circle(String color, double radius) public Rectangle(String color, double
{ width, double height) {
super(color); // Call the constructor of super(color); // Call the constructor of
the Shape class the Shape class
this.radius = radius; this.width = width;
} this.height = height;
}
@Override @Override
public double area() { public double area() {
return Math.PI * radius * radius; return width * height;
} }
}
}
Main
public class Main {
public static void main(String[] args) {
Shape circle = new Circle("Red", 5);
System.out.println("Circle color: " + circle.getColor());
System.out.println("Circle area: " + circle.area());
This line declares a variable num of type int and initializes it with the
result of the method call Integer.parseInt(str).
Integer is a wrapper class in Java that provides utility methods for
working with int values.
parseInt is a static method of the Integer class that converts a String to
an int.
str is passed as an argument to Integer.parseInt, which converts the
string "123" to the integer 123.
The result of Integer.parseInt(str) is assigned to the num variable, so
num now holds the integer value 123.
Cont…
3. Constants:
o Wrapper classes provide useful constants like
MAX_VALUE, MIN_VALUE to represent the maximum
and minimum values a primitive type can hold.
o Example:
• int maxInt = Integer.MAX_VALUE;
4. Immutability:
o Wrapper class objects are immutable, meaning once
they are created, their value cannot be changed.
5. Type Conversion: Cont..
o Wrapper classes provide methods to convert one type to another.
o Example:
double d = 10.5;
Double dObj = Double.valueOf(d);
int intValue = dObj.intValue();
Exploitation:
Double dObj = Double.valueOf(d);
Here, we are using the Double wrapper class to create an object dObj from
the primitive double value d.
Double.valueOf(d) is a method that returns a Double object holding the
value of the primitive double d.
This is an example of boxing, where a primitive type is converted into its
corresponding wrapper class object.
int intValue = dObj.intValue();
This line calls the intValue() method on the Double object dObj to convert
it back to a primitive int.
The intValue() method returns the value of the Double object as an int,
effectively performing an unboxing operation.
Since 10.5 is not an integer, this conversion will truncate the decimal part,
resulting in intValue being 10.
practical example that demonstrates the usage of wrapper classes:
import java.util.ArrayList;
// Output values
System.out.println("Primitive int: " + primitiveInt);
System.out.println("Wrapped Integer: " + wrappedInt);
System.out.println("Unboxed int: " + unboxedInt);
System.out.println("Primitive double: " + primitiveDouble);
System.out.println("Wrapped Double: " + wrappedDouble);
Output:
System.out.println("Unboxed double: " + unboxedDouble); Primitive int: 5
} Wrapped Integer: 5
} Unboxed int: 5
Primitive double: 5.65
Wrapped Double: 5.65
Unboxed double: 5.65
لتوضيحexplain
Integer wrappedInt = Integer.valueOf(primitiveInt);:
توضيح معنى كل كلمة
1. Integer: This is a class in Java. It is part of the java.lang package and is
used to wrap a value of the primitive type int in an object. An object of type
Integer contains a single field whose type is int.
2. wrappedInt: This is a variable name. In this case, it is a reference
variable that will hold a reference to an Integer object. The name is chosen to
indicate that it will hold a wrapped primitive int value.
3. =: This is the assignment operator. It assigns the value on its right to the
variable on its left.
4. Integer.valueOf: This is a static method of the Integer class. The valueOf
method returns an Integer instance representing the specified int value. This
method is often used for converting a primitive int to an Integer object.
5. primitiveInt: This is a variable name that holds a primitive int value. It is
used as an argument to the valueOf method to create an Integer object that
represents this primitive int value.
Recursion in Java