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...
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...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a doubleannual sales amount. Methods include a constructor that requires values for both data fields, as well as get and set methods for each of the data fields. Write an application named DemoSalesperson that declares an array of 10 Salesperson objects. Set each ID number to 9999 and each sales value to zero. Display the 10 Salesperson objects. public class DemoSalesperson { public static void...
java Define the Circle2D class that contains: • Two double data fields named x and y...
java Define the Circle2D class that contains: • Two double data fields named x and y that specify the center of the circle with get methods. A data field radius with a get method. • A data field radius with a get method. • A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius. • A constructor that creates a circle with the specified x, y, and radius. • A method getArea()...
Define the circle2d class that contains: Two double data fields named x and y that specify...
Define the circle2d class that contains: Two double data fields named x and y that specify the center of the circle with getter methods A data field radius with a getter method A no arg constructor that creates a default circle with 0,0 for x,y and 1 for the radius A constructor that creates a circle with the specified x,y of the the circle A method getArea() that returns the area of the circle A method Contains(Double x, Double y)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT