In: Computer Science
Define a class named University with data members such
as campus_name,
courses_offered and no_of_faculty. Include appropriate methods and
illustrate
the following object oriented concepts.
i. Polymorphism.
ii. Static methods
Polymorphism : It's means having many in order forms to explain in very simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.
Static methods: static method is the method which belongs to the class instead / rather than an instance of a class. The static method is accessible to every instance of a class, but methods defined in an instance are only able to be accessed by that only member's of the class not other's members of the another class.
Below is the Explanation of the polymorphism and statuc method with the example of the java code:
polymorphism is mainly two type as the following:
1. Compile time Polymorphism : This type of polymorphism is achieved by function overloading also called static polymorphism.
Method Overloading: When there are more than one functions with same name but different parameters then these functions are said to be Method overloaded and functions can be overloaded by change in number of arguments or change in type of arguments which passed to the method.
class University {
//Declaration of the String variable wrt to the University data
String campus_name = " ";
String courses_offered = " " ;
String no_of_faculty = " " ;
// class constructor to initialise the University data
public University(){
this.campus_name = "xyz_campus";
this.courses_offered = "xyz_course";
this.no_of_faculty = "xyz_faculty";
}
// Method with the same name but1 string parameter
static String get_record(String get_campus_name)
{
return this.campus_name;
}
// Method with the same name but 2 string parameter
static String get_record(String get_course_offered, String get_no_of_faculty)
{
return this.courses_offered +"and" + this.no_of_faculty;
}
}
class Main {
public static void main(String[] args)
{
//creating obj using new operator
record = new University();
//Printing the campus name by passing 1 string parameter
System.out.println(record.get_record("campus_name");
//Printing the campus name by passing 2 string parameter
System.out.println(record.get_record("course_name","no_of_faculty);
//Method overloading achieve based on the difference of the parameter passed
//to the same name of method
}
}
2. Runtime Polymorphism : This type of polymorphism is a process in which a function call to the overridden method is resolved at Runtime and also known as Dynamic Method Dispatch.
Method overriding: This Method occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
class University {
String get_all()
{
return "all";
}
}
//Class inheriting Parent class
class SubUniversity extends University {
//Declaring all the Given University data
String campus_name = " ";
String courses_offered = " " ;
String no_of_faculty = " " ;
// Using Constructor initialise all the variable to some dummy data
public SubUniversity(){
this.campus_name = "xyz_campus";
this.courses_offered = "xyz_course";
this.no_of_faculty = "xyz_faculty";
}
// Method overridden from Parent class during runtime which is
//returning all data as string
String get_all ()
{
return this.campus_name +" "+ this.courses_offered + " " + this.no_of_faculty;
}
}
class Main {
public static void main(String[] args)
{
University data;
// Creating object with respect to the Sub class to achieve the
// method overridden with the sam name
data = new SubUniversity();
// Printing the all the info , here parent class method get_all got
//overridden from base class
System.out.println(data.get_all());
}
}
Note: We can not use static method for the method overidden, We can declare static methods with same signature in subclass, but it is not considered overriding as there won't be any run-time polymorphism.