0% found this document useful (0 votes)
10 views

Java Questions

Uploaded by

Siddartha Datta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java Questions

Uploaded by

Siddartha Datta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

1.

Write a program where a subclass constructor explicitly calls the parent class
constructor using super(). In the subclass, override a method from the parent class.
Instantiate the subclass and demonstrate how super() can affect the method call order
when the parent and child classes have methods with the same name.
2. Create an abstract class and two interfaces. Implement a class that inherits the abstract
class and implements both interfaces. The class should provide implementations for
methods declared in the abstract class and interfaces.
3. Write a Java program that demonstrates constructor chaining between classes using
this() and super().
4. Create a class where static blocks and constructors are both used for initialization
purposes. Demonstrate when each block of code is executed and explain the
difference between static initialization and instance initialization through constructors.
5. Create a base class and a derived class. Define a field and a method in the base class
and redeclare the same field and method in the derived class (i.e., field hiding and
method overriding). Write a program demonstrating how Java resolves access to these
fields and methods when using base class and subclass references.
6. Create an abstract class with a constructor and define a non-abstract subclass that
extends it. Write a program to demonstrate the order in which constructors are called
when you create an instance of the subclass.
7. Create a base class Vehicle with a parameterized constructor that accepts a string
(vehicleType) and prints "Vehicle Type: " followed by the value of vehicleType.
Now, create a subclass Car that inherits Vehicle and has its own parameterized
constructor that accepts two strings: vehicleType and model. In the Car constructor,
call the base class constructor using super() to initialize vehicleType and print "Car
Model: " followed by the value of model. Demonstrate constructor chaining by
creating an object of the Car class.

You might also like