Understanding Polymorphism in Java

Creation Code 39 Extended in Java Understanding Polymorphism

Understanding Polymorphism
Recognizing Code 3 Of 9 In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
Painting USS Code 39 In Java
Using Barcode maker for Java Control to generate, create Code39 image in Java applications.
InSIDE ThE ExAM
Code 39 Full ASCII Decoder In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
Painting Barcode In Java
Using Barcode creator for Java Control to generate, create bar code image in Java applications.
unidirectional Polymorphism
Scanning Bar Code In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
Make Code-39 In Visual C#.NET
Using Barcode printer for .NET Control to generate, create Code39 image in VS .NET applications.
The SCJA exam may try to present the test taker with a polymorphism question where the more general object behaves as the more specific one. Remember, polymorphism only works in one direction. Only specific objects can behave as more general ones.
Generate USS Code 39 In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create Code39 image in ASP.NET applications.
Generate Code 3/9 In .NET Framework
Using Barcode creator for .NET Control to generate, create ANSI/AIM Code 39 image in .NET applications.
has been reflected in the fact that they both implement the Describable interface. Classes that implement this interface are required to then implement the getDescription() method. Below is the Describable interface:
Printing Code 39 In Visual Basic .NET
Using Barcode creator for .NET Control to generate, create Code 39 Full ASCII image in .NET applications.
Creating Code 39 Extended In Java
Using Barcode encoder for Java Control to generate, create Code39 image in Java applications.
public interface Describable { public String getDescription(); }
Barcode Creator In Java
Using Barcode encoder for Java Control to generate, create bar code image in Java applications.
Data Matrix ECC200 Maker In Java
Using Barcode generator for Java Control to generate, create DataMatrix image in Java applications.
This interface only has one method. The getDescription() method is used to return a description about the object. Any class that implements this interface is stating it has a method that can be used to get its description. The Goat class is shown next.
Painting ISSN In Java
Using Barcode generator for Java Control to generate, create ISSN - 10 image in Java applications.
Code 39 Drawer In Objective-C
Using Barcode generator for iPhone Control to generate, create Code 39 Full ASCII image in iPhone applications.
public class Goat implements Describable { private String description; public Goat(String name){ description = "A goat named " + name; } public String getDescription() { return description; } /* * Implement other methods for a goat */ }
GTIN - 13 Creator In Java
Using Barcode maker for Eclipse BIRT Control to generate, create GTIN - 13 image in BIRT applications.
Paint Code 128A In Java
Using Barcode printer for BIRT reports Control to generate, create Code 128 Code Set A image in Eclipse BIRT applications.
Practical Examples of Polymorphism
Making DataMatrix In Java
Using Barcode creation for BIRT reports Control to generate, create Data Matrix image in BIRT reports applications.
Code 39 Full ASCII Creator In Visual Studio .NET
Using Barcode encoder for Reporting Service Control to generate, create Code 3/9 image in Reporting Service applications.
The Goat class is a simple class that can be used to represent a goat. This class implements the Describable interface and therefore is required to implement the getDescription() method. The constructor of the Goat class has one parameter that it uses to place the name of the goat in the description string. The next class in this example is the Box class. It is listed below:
Encode Code 128A In Java
Using Barcode printer for Android Control to generate, create Code 128 Code Set C image in Android applications.
Recognize UPCA In .NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
public class Box implements Describable { private private private private String description; int height; int width; int length;
public Box(int height, int width, int length) { this.height = height; this.width = width; this.length = length; this.description = "A box that is " + height + " high, " + length + " long and " + width + " wide "; } public String getDescription() { return description; } /* * Implement other methods for a box */ }
The Box class is designed to model a box. Its constructor requires that the dimensions of the box be used as arguments. The constructor also creates the description text that is returned in the getDescription() method. Similar to the Goat class, this class also implements the Describable interface. The final class is the Tester class. This class is used to demonstrate the concept of polymorphism with interfaces.
public class Tester { public static void main(String[] args) { new Tester(); }
8:
Understanding Polymorphism
public Tester() { Goat goat = new Goat("Bob"); Box box = new Box(3, 5, 3); System.out.println(description(goat)); System.out.println(description(box)); } private String description(Describable d){ return d.getDescription(); } }
The Tester class contains the main() method that starts the execution of the program. This calls the Tester() constructor where a Goat object and Box object are both created. The description() method is then used to print to standard out the description of each object. The description() method requires a Describable object. It is impossible to have a true Describable object since it is an interface. However, classes that implement this interface are declaring that they have the functionality of Describable. These objects can then polymorphically act as if they were of type Describable. Below is the output of this program:
A goat named Bob A box that is 3 high, 3 long and 5 wide
ExERCISE 8-1 Add Functionality to the Describable Example
This exercise will use the preceding example. The goal of the exercise is to compile and run the above example and add a class that implements the Describable interface. 1. Copy the example into the text editor or IDE of your choice. 2. Compile and run the example to ensure the code has been copied correctly. Add a new class that implements the Describable interface. 3. Compile and run the application.
Practical Examples of Polymorphism
Examples of Programming to an Interface
This example will demonstrate the concept of programming to an interface. This concept allows a developer to define the functionality that is required instead of defining an actual object type. This creates more flexible code that adheres to the object-oriented design principle of creating reusable code. A developer may create a class that is used for creating log files. This class is responsible for creating and managing the log file on the file system, and then appending the log messages to it. This class can be called Logger. The Logger class has a method called appendToLog() that accepts one object as an argument and then appends a message about it in the log. The developer could overload this method with every possible data type that the program uses. While this would work, it would be very inefficient. If the program to an interface concept is used, the developer would instead create an interface that defines the required method for a logable class. This interface could be called Logable. The appendToLog() method would then use the Logable interface as its argument. Any class that required logging could implement this interface and then be used polymorphically with the appendToLog() method. The following is the Logable interface:
public interface Logable { public String getInitInfo(); public String getLogableEvent(); }
The Logable interface is a basic interface that defines the methods required to work with the appendToLog() method in the Logger class. The appendToLog() method is not concerned with the details of an object other than what pertains to logging. By using this interface the developer has defined a functionality requirement as opposed to a strict object data type. This is what is meant when the term programming to an interface is used. The Logger class is displayed next.
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class Logger { private BufferedWriter out; public Logger() throws IOException { out = new BufferedWriter(new FileWriter("logfile.txt")); }
8:
Copyright © OnBarcode.com . All rights reserved.