0% found this document useful (0 votes)
9 views2 pages

Java Course and OnlineCourse Class Guide

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Java Course and OnlineCourse Class Guide

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Sample task guideline

1. Class Structure:
 Define a superclass named Course that contains the following attributes and
methods:
 Attributes: courseCode (String), courseTitle (String), and courseInstructor
(String).
 Methods:
 Course(String courseCode, String courseTitle, String
courseInstructor): Constructor to initialize the attributes.
 void displayCourseInfo(): Method to display the course information
(course code, course title, and course instructor).
 Define a subclass named OnlineCourse that extends the Course class. This subclass
should include the following:
 Additional attribute: platform (String) representing the online platform used
for the course.
 Implement the constructor to initialize all attributes (courseCode,
courseTitle, courseInstructor, and platform).
 Override the displayCourseInfo() method to also display the online platform
of the course.
2. Object Creation:
 Create an object of the OnlineCourse class and initialize it with appropriate values
for all attributes.
 Display the information of the online course using the displayCourseInfo() method.

TIPS:
 Read and analyze the problem and conceptualize the possible output.
 If having hard time to understand problem try using diagram as your
lead.
 Practice coding using your own problem or inputs.
 Don’t memorize the whole codes instead familiarize the order of
algorithm, keywords and syntax.
VERY, VERY, VERY Basic -------> Sample Code

class Course{ //Class (Parent class)


String courseCode; //Attribute 1
String courseTitle; //Attribute 2
String courseInstructor; //Attribute 3

public Course(String courseCode, String courseTitle, //Constructor


method for parent class
String courseInstructor){
[Link]=courseCode;
[Link]=courseTitle;
[Link]=courseInstructor;
}

public void displayCourseInfo(){ //function method to display course


information
[Link]("COURSE INFORMATION");
[Link]("Course Code: "+courseCode+ "\nCourse Title: "
+ courseTitle +"\nCourse Instructor: "+courseInstructor);
}
}

class OnlineClass extends Course{ //Child class (single Inheritance)


String platform; // new attribute for the child class
public OnlineClass(String courseCode, String courseTitle,
//Constructor method for child class
// ** just copy the parameter from the
// parent class then add the new attribute
String courseInstructor, String platform){
super(courseCode, courseTitle, courseInstructor); //Super to copy
th
// constructor from the parent class
[Link]=platform; // Initialize also the new attribute for
child class
}
@Override // method overriding in runtime Polymorphism
public void displayCourseInfo(){
[Link]();
[Link]("Platform"+platform+"\n");
}
}
public class Main { //Main class
public static void main(String[]args){ //main method
OnlineClass onlineClass1 = new OnlineClass("123ab",
"Programming 2","Moses B. Gawang", "Google Class Room"); //
Object 1
OnlineClass onlineClass2 = new OnlineClass("123ac", "OS",
"Moses Gawang","Zoom"); // object 2
[Link](); // display the object 1 infos by
calling the method above
[Link]();// display the object 2 infos by
calling the method above
}
}
// Fix the output display for readbility..

//Thank you!!!!
//Good luck

Common questions

Powered by AI

The 'super' keyword in the OnlineClass constructor is used to invoke the constructor of the superclass, Course, to initialize inherited attributes courseCode, courseTitle, and courseInstructor. This ensures that the base attributes are properly initialized before adding any subclass-specific functionality such as the platform attribute .

Overriding the displayCourseInfo() method in the OnlineClass subclass is important because it allows the method to include additional subclass-specific information, namely the platform on which the course is hosted. This enhances the method to provide a complete representation of the online course, which is not achievable with the superclass’s method .

Inheritance is demonstrated by the OnlineClass subclass inheriting attributes and behaviors from the Course superclass. The OnlineClass extends Course, meaning it inherits the attributes courseCode, courseTitle, and courseInstructor, as well as the method displayCourseInfo() from Course. It also introduces a new attribute platform and overrides the displayCourseInfo() method to include additional information, showcasing runtime polymorphism .

The document suggests that practicing coding is more beneficial than memorizing code because it develops problem-solving skills and a deeper understanding of programming concepts. Familiarizing oneself with algorithm orders, syntax, and key principles rather than rote memorization empowers programmers to adapt to new problems and frameworks effectively .

Creating subclass objects and printing their information helps reinforce programming concepts such as inheritance, polymorphism, and the principle of encapsulation. By implementing and showcasing these concepts, learners can experience firsthand how attributes are passed down hierarchies, how methods can be overridden and extended, and how objects provide a structured way to manage and display data .

An OnlineClass object is instantiated by calling its constructor with appropriate parameters: courseCode, courseTitle, courseInstructor, and platform. Once created, the object’s information is displayed using the displayCourseInfo() method, which prints out the course information including the platform due to the overridden method implementation .

Encapsulation in the provided class structure is reflected through the use of private attributes courseCode, courseTitle, courseInstructor, and platform within their respective classes. These attributes are accessed and modified through public methods, such as constructors and displayCourseInfo(), maintaining controlled access and protection of data .

Polymorphism allows objects to be used as if they are instances of their parent class, even if they are actually instances of a child class. In this document, method overriding is used to achieve runtime polymorphism, where the OnlineClass overrides the displayCourseInfo() method from the Course class to include the platform attribute along with other course information .

Using diagrams can aid in understanding complex programming tasks by visually representing the relationships between classes and their attributes and methods. This visual aid helps in simplifying the conceptualization and planning stages, potentially making it easier to implement and debug code .

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. It is used in the document’s example with the displayCourseInfo() method. Method overloading, on the other hand, involves having multiple methods with the same name but different parameter lists within the same class, which is not implemented in this example .

You might also like