Practice 70 Java Inheritance Programming Questions , TechnoVlogs

Practice 70 Java Inheritance Programming Questions


Q1. Write a Java program to demonstrate single inheritance by creating a class Animal and a class Dog that inherits from Animal.
  Expected Output:  
  Dog barks

Q2. Write a Java program to demonstrate multi-level inheritance by creating a class Vehicle, a class Car that extends Vehicle, and a class ElectricCar that extends Car.
  Expected Output:     
  Electric car is eco-friendly

Q3. Write a Java program to demonstrate method overriding with inheritance. Create a class Shape with a method area() and a subclass Circle that overrides the area() method.
  Input:  
  Radius: 5

  Expected Output:     
  Area of Circle: 78.54

Q4. Write a Java program to demonstrate the use of the super keyword in inheritance. Create a class Person with a constructor and a subclass Student that calls the Person constructor.
  Input:  
  Name: John
  Age: 20

  Expected Output:     
  Name: John, Age: 20

Q5. Write a Java program to demonstrate constructor chaining in inheritance. Create a class Vehicle with a constructor, and a class Bike that calls the Vehicle constructor.
  Input:  
  Vehicle Name: Yamaha, Bike Model: FZ

  Expected Output:     
  Vehicle Name: Yamaha, Bike Model: FZ

Q6. Write a Java program to demonstrate the use of super to access superclass methods. Create a class Animal with a method sound() and a subclass Dog that calls super.sound() in its own sound() method.
  Expected Output:  
  Animal makes a sound
  Dog barks

Q7. Write a Java program to demonstrate multiple inheritance through interfaces. Create two interfaces Animal and Vehicle and a class Hybrid that implements both interfaces.
  Expected Output:  
  Hybrid car and hybrid animal

Q8. Write a Java program to demonstrate method overriding with different access modifiers in the subclass. Create a class Person and subclass Student with a greet() method, where Student overrides greet() with a different access level.
  Expected Output:  
  Student greets

Q9. Write a Java program to demonstrate abstract classes and methods in inheritance. Create an abstract class Shape with an abstract method area() and a subclass Rectangle that implements area().
  Input:  
  Length: 10, Width: 5

  Expected Output:     
  Area of Rectangle: 50

Q10. Write a Java program to demonstrate the instanceof operator in inheritance. Create a class Animal and a subclass Cat. Check if an object of Cat is an instance of Animal using instanceof.
  Expected Output:  
  true

Q11. Write a Java program to demonstrate inheritance with a private member variable. Create a class Person with a private variable name and a subclass Employee that attempts to access it.
  Input:  
  Name: John

  Expected Output:     
  Cannot access private member variable name

Q12. Write a Java program to demonstrate inheritance and method hiding with static methods. Create a class Animal with a static method eat(), and a subclass Dog with its own static eat() method.
  Expected Output:  
  Animal eats
  Dog eats

Q13. Write a Java program to demonstrate inheritance with constructors in the subclass. Create a class Employee with a constructor, and a subclass Manager that calls the Employee constructor.
  Input:  
  Name: Jack, Age: 35

  Expected Output:     
  Name: Jack, Age: 35

Q14. Write a Java program to demonstrate the use of the this keyword in inheritance. Create a class Person with a method display() and a subclass Employee that uses this to call display().
  Expected Output:  
  Employee display

Q15. Write a Java program to demonstrate polymorphism in inheritance. Create a class Animal with a method sound(), and a subclass Dog and Cat, where each overrides sound().
  Expected Output:  
  Dog barks
  Cat meows

Q16. Write a Java program to demonstrate the use of the final keyword with inheritance. Create a class Car with a final method start(), and a subclass ElectricCar that attempts to override start().
  Expected Output:  
  Cannot override the final method start()

Q17. Write a Java program to demonstrate the use of super() to invoke superclass constructors in a multi-level inheritance hierarchy.
  Input:  
  Base: 10, Derived: 5

  Expected Output:     
  Base class constructor called
  Derived class constructor called

Q18. Write a Java program to demonstrate dynamic method dispatch in inheritance. Create a superclass Animal with a method speak() and a subclass Dog that overrides speak().
  Expected Output: 
  Dog speaks

Q19. Write a Java program to demonstrate inheritance by creating a class Person and a subclass Student that adds a studentId field.
  Input:  
  Name: Alice, Age: 22, Student ID: S12345

  Expected Output:     
  Name: Alice, Age: 22, Student ID: S12345

Q20. Write a Java program to demonstrate inheritance where the subclass calls a superclass method to perform an action.
  Expected Output:  
  Superclass method executed
  Subclass method executed

Q21. Write a Java program to demonstrate inheritance and method overloading in a subclass. Create a class Calculator with overloaded methods add() and a subclass AdvancedCalculator that adds extra functionality.
  Input:  
  Add: 2 + 3

  Expected Output:     
  Addition result: 5

Q22. Write a Java program to demonstrate inheritance where the subclass calls a superclass method with different parameters.
  Expected Output:  
  Parent class method called
  Child class method called

Q23. Write a Java program to demonstrate inheritance by creating a class Shape and a subclass Square that calculates the area of a square.
  Input:  
  Side: 4

  Expected Output:     
  Area of Square: 16

Q24. Write a Java program to demonstrate inheritance by creating a class Shape with a method draw(), and a subclass Circle that overrides the draw() method.
  Expected Output:  
  Drawing Circle

Q25. Write a Java program to demonstrate inheritance where the subclass has a method with the same name as the superclass method but does not override it.
  Expected Output:  
  Parent class method
  Subclass method

Q26. Write a Java program to demonstrate inheritance where the superclass method calls a subclass method.
  Expected Output:  
  Parent class method calls subclass method

Q27. Write a Java program to demonstrate inheritance where the subclass has access to the superclass methods and variables.
  Expected Output: 
  Superclass variable accessed in subclass

Q28. Write a Java program to demonstrate inheritance by creating a class Employee with a method work() and a subclass Manager that overrides work().
  Expected Output:  
  Manager works

Q29. Write a Java program to demonstrate inheritance by creating a class Library with a name field and a subclass Book that has additional fields author and genre.
  Input:  
  Name: Central Library, Author: J.K. Rowling, Genre: Fantasy

  Expected Output:     
  Library Name: Central Library, Book Author: J.K. Rowling, Genre: Fantasy

Q30. Write a Java program to demonstrate inheritance where a subclass adds new methods while still inheriting methods from the superclass.
  Expected Output:  
  Parent class method called
  Child class method called

Q31. Write a Java program to demonstrate inheritance where the subclass has a constructor and calls the superclass constructor using super().
  Expected Output:  
  Superclass constructor called
  Subclass constructor called

Q32. Write a Java program to demonstrate method overriding where a subclass overrides a method and invokes the superclass method using super.
  Expected Output:  
  Superclass method executed
  Subclass method executed

Q33. Write a Java program to demonstrate how a subclass can access the protected members of a superclass.
  Expected Output:  
  Accessing protected member from subclass

Q34. Write a Java program to demonstrate the use of super to access superclass members that are hidden in the subclass.
  Expected Output: 
  Superclass member accessed using super

Q35. Write a Java program to demonstrate the use of abstract methods in inheritance. Create an abstract class Shape and a subclass Triangle that implements the abstract method draw().
  Expected Output: 
  Drawing Triangle

Q36. Write a Java program to demonstrate inheritance where the subclass calls a superclass method with parameters.
  Input: 
  Parameter: 10

  Expected Output:     
  Superclass method with parameter: 10
  Subclass method executed

Q37. Write a Java program to demonstrate inheritance where the subclass uses a superclass method to perform an action and modifies the result.
  Expected Output:  
  Superclass method called
  Subclass method modifies result

Q38. Write a Java program to demonstrate inheritance with constructors, where the subclass constructor accepts parameters and passes them to the superclass constructor.
  Input:  
  Name: Bob, Age: 30

  Expected Output:     
  Name: Bob, Age: 30

Q39. Write a Java program to demonstrate inheritance with a final method in the superclass that cannot be overridden in the subclass.
  Expected Output:  
  Cannot override final method

Q40. Write a Java program to demonstrate how a subclass can inherit and modify the behavior of a superclass method.
  Expected Output:  
  Superclass method executed
  Subclass modifies behavior

Q41. Write a Java program to demonstrate a scenario where a subclass method has the same name as the superclass method but differs in parameters.
  Expected Output:  
  Superclass method called
  Subclass method with different parameters called

Q42. Write a Java program to demonstrate the use of multiple interfaces with inheritance. Create two interfaces Drivable and Flyable, and a class FlyingCar that implements both interfaces.
  Expected Output: 
  FlyingCar drives and flies

Q43. Write a Java program to demonstrate inheritance where a subclass is instantiated using the constructor of its superclass.
  Expected Output: 
  Superclass constructor executed

Q44. Write a Java program to demonstrate how method overriding works in Java when a subclass overrides the superclass method with additional functionality.
  Expected Output:  
  Superclass method executed
  Subclass method with extra functionality executed

Q45. Write a Java program to demonstrate inheritance where a subclass calls a method in its superclass to retrieve a value and use it.
  Expected Output: 
  Superclass method retrieves value: 100
  Subclass uses value: 100

Q46. Write a Java program to demonstrate the use of abstract class and concrete subclass implementation. Create an abstract class Animal with an abstract method speak(), and a concrete subclass Dog that implements speak().
  Expected Output:  
  Dog barks

Q47. Write a Java program to demonstrate inheritance where a subclass extends multiple classes (via interfaces).
  Expected Output:    
  Implemented method from Interface 1
  Implemented method from Interface 2

Q48. Write a Java program to demonstrate the use of a constructor in a subclass that calls a superclass constructor with parameters.
  Input:    
  Name: Alice, Age: 25

  Expected Output:     
  Name: Alice, Age: 25

Q49. Write a Java program to demonstrate the effect of method overriding where a subclass method hides the superclass method with a different return type.
  Expected Output:  
  Subclass method executed with a different return type

Q50. Write a Java program to demonstrate how a subclass can extend an abstract class and implement the abstract methods.
  Expected Output:  
  Concrete subclass method executed

Q51. Write a Java program to demonstrate inheritance where a superclass method is overridden by the subclass to modify its behavior.
  Expected Output:  
  Superclass method executed
  Subclass modifies behavior

Q52. Write a Java program to demonstrate method overloading in inheritance. Create a class Shape with an overloaded area() method, and a subclass Rectangle that calls area().
  Input:  
  Side: 5
  Length: 10, Width: 5

  Expected Output:     
  Area of Shape: 25
  Area of Rectangle: 50

Q53. Write a Java program to demonstrate inheritance with a final class, where no other class can extend the final class.
  Expected Output:  
  Cannot extend final class

Q54. Write a Java program to demonstrate inheritance where the subclass invokes a superclass method from the constructor.
  Expected Output:  
  Superclass constructor executed
  Subclass constructor executed

Q55. Write a Java program to demonstrate inheritance with a subclass accessing the superclass constructor with arguments using super.
  Input:  
  Name: John, Age: 45

  Expected Output:     
  Name: John, Age: 45

Q56. Write a Java program to demonstrate inheritance with a superclass method called in the subclass constructor.
  Expected Output:  
  Superclass constructor executed
  Subclass constructor executed

Q57. Write a Java program to demonstrate the inheritance of a final method that cannot be overridden.
  Expected Output:  
  Cannot override final method

Q58. Write a Java program to demonstrate the use of multiple constructors in inheritance, where the subclass calls different superclass constructors.
  Input:  
  Name: Tom, Age: 28

  Expected Output:     
  Name: Tom, Age: 28

Q59. Write a Java program to demonstrate inheritance where the superclass and subclass have methods with the same name but different parameters (method overloading).
  Expected Output:  
  Subclass method executed with parameter: 5
  Subclass method executed with parameter: 10

Q60. Write a Java program to demonstrate the use of inheritance where the subclass calls a superclass method with a different number of parameters.
  Input:  
  Parameter: 10

  Expected Output:     
  Superclass method executed with one parameter
  Subclass method executed with multiple parameters

Q61. Write a Java program to demonstrate inheritance where the superclass defines a toString() method and the subclass overrides it to provide a more specific representation.
  Expected Output:  
  Superclass toString()
  Subclass toString()

Q62. Write a Java program to demonstrate inheritance where a subclass constructor calls the superclass constructor to initialize fields.
  Input:  
  Name: Sarah, Age: 22

  Expected Output:     
  Name: Sarah, Age: 22

Q63. Write a Java program to demonstrate inheritance with a superclass method that returns a value, and the subclass modifies that value before returning it.
  Expected Output:  
  Superclass value: 100
  Subclass modified value: 200

Q64. Write a Java program to demonstrate inheritance where a subclass method invokes a superclass method to calculate a result, and the subclass modifies the result.
  Expected Output:  
  Superclass calculation result: 10
  Subclass modified result: 15

Q65. Write a Java program to demonstrate inheritance where the subclass adds additional functionality to a superclass method, but also calls the superclass method.
  Expected Output:  
  Superclass method executed
  Subclass additional functionality executed

Q66. Write a Java program to demonstrate polymorphism in inheritance where the superclass reference variable refers to a subclass object and calls the subclass method.
  Expected Output:  
  Subclass method executed

Q67. Write a Java program to demonstrate inheritance with a subclass overriding a method and calling the superclass method using super from within the overridden method.
  Expected Output:  
  Superclass method executed
  Subclass overridden method executed

Q68. Write a Java program to demonstrate inheritance where a subclass calls a method in its superclass to perform an action, and adds its own additional logic afterward.
  Expected Output:  
  Superclass method called
  Subclass adds its logic

Q69. Write a Java program to demonstrate inheritance where a subclass uses the super keyword to access a protected member from the superclass.
  Expected Output:  
  Accessing protected member: 50

Q70. Write a Java program to demonstrate inheritance where the subclass constructor invokes the superclass constructor using super() with parameters, and the superclass constructor displays the parameters.
  Input:  
  Name: Mark, Age: 35

  Expected Output:     
  Name: Mark, Age: 35

Share on Social Media