Part 3-Support Java 17 Certif - Inheritance and Records
Part 3-Support Java 17 Certif - Inheritance and Records
Modules
Inheritance Exception Handling
Generics
JDBC API
Class, Object Annotations
Interfaces Java Security
Java IO API
Java Concurrency & Multi Threading
Lambda Expressions Java Streams API
Mohamed Youssfi, Enseignant Chercheur ENSET Mohammedia, Université Hassan II de Casablanca Consultant R&D Ingénierie Logicielle
Reuse Parent Class Code through Inheritance
The purpose of inheritance is to reuse generic class behaviors and
state in subclass
• Superclass represents more generic, parent type
• Superclass define common attributes and behaviors
• Subclass represents a more specific, child type and extends the
parent type
• Subclass inherit all attributes and behaviors from their parents
• Subclass may define subtype-specific attributes and behaviors
System.out.println(p1);
System.out.println(p2); Product{id=101, name='Tea', price=1.99, rating=☆☆☆☆☆}
System.out.println(p3); Drink{id=102, name='Coffee', price=1.88, rating=★★★☆☆}
System.out.println(p4); Food{id=103, name='Cake', price=1.12, rating=★★☆☆☆}, { bestBefore=2023-09-13}
System.out.println(p5); Product{id=0, name='no name', price=0, rating=☆☆☆☆☆}
Product{id=103, name='Cake', price=1.12, rating=★★★★☆}
Practice for Lesson 6 (6.3)
false
Product p6 = new Drink(102, "Chocolate", BigDecimal.valueOf(1.88),
Rating.THREE_STAR);
@Override In Product Class Product p7 = new Food(102, "Chocolate", BigDecimal.valueOf(1.88),
public boolean equals(Object o) { Rating.TWO_STAR, LocalDate.now().plusDays(2));
if (this == o) return true; Product p8 = new Food(102, "Chocolate", BigDecimal.valueOf(1.88),
if (o == null || getClass() != o.getClass()) return false; Rating.TWO_STAR, LocalDate.now().plusDays(2));
Product product = (Product) o; System.out.println(p6.equals(p7));
return id == product.id && Objects.equals(name, product.name); System.out.println(p7.equals(p8));
}
false
true
@Override
public int hashCode() {
return Objects.hash(id, name);
}
false
Practice for Lesson 6 (6.3) : public class Drink extends Product {
//
public abstract class Product { @Override
public abstract Product applyRating(Rating newRating); public Product applyRating(Rating newRating) {
public LocalDate getBestBefore() { return new Drink(getId(), getName(), getPrice(), newRating);
return LocalDate.now(); }
} //
@Override }
public String toString() {
return this.getClass().getSimpleName()+"{" + public class Food extends Product {
id + " " + name + " " + price + @Override
" " + rating.getStars() + public BigDecimal getDiscount() {
" "+getBestBefore()+ LocalTime now = LocalTime.now();
'}’; return (now.isAfter(LocalTime.of(17,30)) && now.isBefore(LocalTime.of(18,30)))
} ? super.getDiscount() : BigDecimal.ZERO;
} }
@Override
public Product applyRating(Rating newRating) {
return new Food(getId(), getName(), getPrice(), newRating, bestBefore );
}
// @Override
// public String toString() {
// return super.toString()+", { bestBefore="+bestBefore+"}";
// }
}
Practice for Lesson 6 (6.3) :