UOP PRG421 Week 3 Discussion Latest 2020
PRG421 Java Programming II
Week 3 Discussion
DQ1 Benefits of Generics
The purpose of this discussion is to describe the difference between implementing a class as an abstract class (i.e., a
class from which concrete classes can be derived) and a generic class (i.e., a class from which different types of
classes can be derived).
Discuss the benefits of using a generic class.
Share an example of a real-life situation that would benefit from being modeled as a generic class.
Instructions
DQ2 Abstract Methods and Classes
The purpose of this activity is to understand the syntax necessary to declare and implement abstract methods and classes.
A method declared without any body within an abstract class is called an abstract method. The body of an abstract method must be defined by its subclass. Abstract methods can never be declared final or static, and any class that extends an abstract class must implement all the abstract methods declared by the super class. Abstract methods are useful in situations when two or more subclasses are expected to do a similar thing in different ways, extending the same abstract class by providing different implementations of the same abstract methods.
For this activity, you will practice defining an abstract method in one class (class A) and implementing that method in a derived class (class B).
Copy and paste the following code into a JAVA source file in NetBeans; note the abstract and extends keywords in the source:
abstract class A {
abstract void demoabst();
}
class B extends A { // subclass B derives from A
void demoabst () {
System.out.println("This is a code demo.");
}
public static void main(String[] args) {
B b = new B(); // class B reference and object
b. demoabst (); // reference demoabst object
}}
Run and debug the JAVA file in NetBeans
-
Rating:
/5
Solution: UOP PRG421 Week 3 Discussion Latest 2020