Question

In: Computer Science

Define a class named University with data members such as campus_name, courses_offered and no_of_faculty. Include appropriate...

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

Solutions

Expert Solution

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.


Related Solutions

Define a class named University with data members such as campus_name, courses_offered and no_of_faculty. Include appropriate...
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
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
1. Define a class named Book that contains:  An int data field named pages that...
1. Define a class named Book that contains:  An int data field named pages that stores the number of pages in the book.  A String data field named title that represents the title of the book.  A constructor with parameters for initializing pages and title.  The getter and setter methods for all data fields.  A toString method that returns book information, including the book title and pages.  The equals method that returns true if...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Develop a java program with a class named friend with data members like name, phno and...
Develop a java program with a class named friend with data members like name, phno and hobby. Use Parameterized constructor to create two friend objects and invoke checkhobby method which takes only one parameter, to find whether they have same hobby or different hobbies
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
Write in C++ language. (Employee Record): Create a class named 'Staff' having the following members: Data...
Write in C++ language. (Employee Record): Create a class named 'Staff' having the following members: Data members - Id – Name - Phone number – Address - AgeIt also has a function named 'printSalary' which prints the salary of the staff.Two classes 'Employee' and 'Officer' inherits the 'Staff' class. The 'Employee' and 'Officer' classes have data members 'Top Skill' and 'department' respectively. Now, assign name, age, phone number, address and salary to an employee and a officer by making an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT