Ads

Sunday, December 27, 2020

Create a class "Degree" having a method "getDegree()" that prints "I got a degree". It has two subclasses namely "Undergraduate" and "Postgraduate" each having a method with the same name that prints "I am an Undergraduate" and "I am a Postgraduate" respectively. Call the method by creating an object of each of the three classes.

 

Here is the code to implement the scenario described:


Source Code.


package degree;

 //@author Java_Programs

public class Degree {

    public static void main(String[] args) {

        Degree d = new Degree();

        d.getDegree();

      

        Undergraduate u = new Undergraduate();

        u.getDegree();

        

        Postgraduate p = new Postgraduate();

        p.getDegree();

    }

    void getDegree(){

        System.out.println("I got a Degree: ");

    }    

}


class Undergraduate extends Degree{


        void getDegree(){

        System.out.println("I am an Undergraduate: ");

    }

}

class Postgraduate extends Degree{

        void getDegree(){

        System.out.println("I am a POstgraduate: ");

    }

}


Code explanation:

The program has a main class named "Degree" and two subclasses "Undergraduate" and "Postgraduate", which extends the main class "Degree".

The main class has a method named "getDegree()" which simply prints "I got a Degree".

Both subclasses have a method with the same name "getDegree()", which overrides the method from the parent class. The "Undergraduate" class prints "I am an Undergraduate", and the "Postgraduate" class prints "I am a Postgraduate".

In the main method, we create an object of the "Degree" class, call the "getDegree()" method, which will print "I got a Degree". Then we create an object of the "Undergraduate" class and call the same method, which now prints "I am an Undergraduate". Similarly, we create an object of the "Postgraduate" class and call the same method, which now prints "I am a Postgraduate".

The main concept used in this program is inheritance and method overriding, where the subclasses inherit the behavior of the parent class and override its methods to provide their own implementation.

 

Here are some additional details that you may find useful:

 

Inheritance: The code demonstrates the concept of inheritance, where the subclasses (Undergraduate and Postgraduate) inherit the methods and properties of the parent class (Degree). This allows for code reusability and enables the subclasses to add new functionalities while maintaining the existing ones.

Polymorphism: The code also showcases the principle of polymorphism, where the same method "getDegree()" is used by different objects and produces different outputs based on the class to which the object belongs. This allows for flexibility and dynamic behavior in the code.

Overriding: The methods "getDegree()" in the subclasses override the same method in the parent class. This is achieved by using the @Override annotation, which helps to ensure that the method being overridden is actually present in the parent class.

Method Overloading vs. Method Overriding: It's worth noting that method overloading and method overriding are different concepts. Method overloading occurs when a class has multiple methods with the same name but different parameters, whereas method overriding occurs when a subclass defines a method with the same name and parameters as the method in the parent class.

Access Modifiers: The access modifiers used in the code (public and default) determine the accessibility of the methods and properties. Public methods can be accessed by any class, whereas default methods are only accessible within the same package.

Overall, the code demonstrates some key concepts in object-oriented programming and showcases how inheritance and polymorphism can be used to create flexible and reusable code.


No comments:

Post a Comment