Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
- Bu sahifa navigatsiya:
- 5-13. Defining a Template for Classes to Extend Problem You want to define a template that generates objects containing similar functionality. Solution
How It Works
Object inheritance is a fundamental technique in any object-oriented language. Inheriting from a base class adds value because it allows code to become reusable in multiple places. This helps to make code management much easier. If a change is made in the base class, it automatically inherits the child. On the other hand, if you had duplicate functionality scattered throughout your application, one minor change could mean that you would have to change code in many places. Object inheritance also makes it easy to designate a base class to one or more subclasses so that each class can contain similar fields and functionality. The Java language allows a class to extend only one other class. This differs in concept from other languages such as C++, which contain multiple inheritance. Although some look at single class inheritance as a hindrance to the language, it was designed to add safety and ease of use to the language. When a subclass contains multiple superclasses, confusion can ensue. 5-13. Defining a Template for Classes to Extend Problem You want to define a template that generates objects containing similar functionality. Solution Define an abstract class that contains fields and functionality that can be used in other classes. The abstract class can also include unimplemented methods, known as abstract methods, which need to be implemented by a subclass of the abstract class. The following example demonstrates the concept of an abstract class. Chapter 5 ObjeCt-Oriented java 198 The abstract class in the example represents a team schedule, and it includes some basic field declarations and functionality that every team’s schedule needs to use. The Schedule class is then extended by the TeamSchedule class, which implements specific functionality for each team. First, let’s take a look at the abstract Schedule class. public abstract class Schedule { public String scheduleYear; public String teamName; public List public Map public Schedule(){} public Schedule(String teamName){ this.teamName = teamName; } abstract void calculateDaysPlayed(int month); } Next, the TeamSchedule extends the functionality of the abstract class. public class TeamSchedule extends Schedule { public TeamSchedule(String teamName) { super(teamName); } @Override void calculateDaysPlayed(int month) { int totalGamesPlayedInMonth = 0; for (Map.Entry if (entry.getKey().equals(teamName) && entry.getValue().getMonth().equals(month)) { totalGamesPlayedInMonth++; } } Chapter 5 ObjeCt-Oriented java 199 System.out.println("Games played in specified month: " + totalGamesPlayedInMonth); } } As you can see, the TeamSchedule class can use all the fields and methods contained within the abstract Schedule class. It also implements the abstract method that is contained within the Schedule class. Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling