In Java, a superclass is a class from which other classes, called subclasses (or child classes) and inherit properties and behaviors. This concept is central to object-oriented programming, specifically the principle of inheritance. The superclass is also known as a parent class or base class.

Using a superclass is very helpful because:
Output:
The Employee is doing their work. The Developer is debugging code. The Employee is doing their work. The Designer is creating a design.
Explanation
The above Java program shows object-oriented programming with use of inheritance. The superclass Employee has a method called work() that prints a simple message "Employee is doing their work." Two subclasses Developer and Designer extend the Employee class. This means that they inherit the work() method and they can also define their own methods.
Now, the Developer class includes a method called writeCode() that prints "Developer is writing code" while the Designer class has a method named createDesign() that prints "Designer is creating a design." In the main method, objects of both the methods Developer and Designer are created. These objects call both the inherited work() method. This program shows how subclasses can inherit the properties and functionality from the parent class and also add their own functionality.
Using a superclass in Java helps to organize and reuse code by writing common features once and sharing them with subclasses. In the example, Employee is the superclass with a work() method which is inherited by Developer and Designer. Each subclass adds its own method avoiding repetition of code.
It makes the program cleaner and easier to maintain, and also supports polymorphism. However, inheritance should be used wisely, as if we overuse the inheritance, it makes code harder to manage in large projects. When used properly, it helps build efficient and scalable Java programs.
1. In Java, which class is the superclass of all other classes?
Answer: B
Explanation: In Java, Object is the root superclass of all classes. Every class automatically inherits from it and gets its common methods like toString() and equals().
2. Which of the following is an advantage of using a superclass?
Answer: C
Explanation: A superclass groups shared features in one place making the program more structured and easier to manage.
3. In the example, what is the function of the Employee class?
Answer: C
Explanation: Employee is the superclass that defines the work() method, which is shared by both the Developer and Designer classes.
4. Which one of the following is disadvantage of using inheritance incorrectly?
Answer: C
Explanation: Overusing or misusing inheritance can tightly couple classes, making the code harder to understand and manage, especially if we are working with large projects.
5. What is polymorphism in Java?
Answer: C
Explanation: Polymorphism allows inherits from a superclass reference to point to objects of its subclasses it makes code flexible and reusable.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India