213k views
4 votes
This Java program below is used to Demonstrate Hierarchical Inheritance. Correct the program in java

//Start of the program
class Students {
void Play() {
System.out.println("We are proud students of NUST-);
}
}
class FT extends Student {
void FTStudy() {
System.out.println("Mr Steven-I am in FT programme");
}
}
class PT extends Student {
void PTStudy() {
System.out.println("Ms Jane-I am in PT programme");
}
}
public class Lab08_A {
public static void main(String args[]) {
FT FT = new FT();
PT PT = new PT();
//FT.Play();
FT.FTStudy();
PT.PTStudy();
}
}
Sample output:
Demonstration of Hierarchical inheritance:
We are proud students of NUST-
Mr Steven-I am in FT programme
Ms Jane-I am in PT programme

User Marcha
by
7.9k points

1 Answer

4 votes

Final answer:

The Java program contains errors in class naming and syntax which prevent it from demonstrating hierarchical inheritance. Once corrected, it will showcase the concept as expected by outputting the designated statements for each class.

Step-by-step explanation:

The given Java program aims to demonstrate hierarchical inheritance but contains some errors that prevent it from running correctly. The errors in the program are: The class Student is incorrectly referred to as Students; the method Play() needs to have its string properly closed with a quote; and the classes FT and PT should extend Student, not 'Students'. Upon fixing these, the program should run and demonstrate hierarchical inheritance as expected.

Here is the corrected version of the program:

class Student {
void Play() {
System.out.println("We are proud students of NUST");
}
}

class FT extends Student {
void FTStudy() {
System.out.println("Mr Steven - I am in FT programme");
}
}

class PT extends Student {
void PTStudy() {
System.out.println("Ms Jane - I am in PT programme");
}
}

public class Lab08_A {
public static void main(String args[]) {
FT ft = new FT();
PT pt = new PT();
ft.Play(); // Now uncommented to match the expected output
ft.FTStudy();
pt.PTStudy();
}
}
User Blazej SLEBODA
by
9.3k points