OOPS
Here are some problem statements covering Object-Oriented Programming (OOP) concepts in Java from basic to advanced:
Class and Object Creation: Create a class named
Car
with attributes likemake
,model
, andyear
, and then create an object of this class.Encapsulation: Define a class
Employee
with private attributes likename
,age
, andsalary
, and provide public methods to access and modify these attributes.Inheritance: Create a class
Vehicle
with attributes likemake
andmodel
, and then create a subclassCar
that inherits fromVehicle
.Method Overriding: Define a method
drive()
in bothVehicle
andCar
classes, and override it in theCar
class to provide a different implementation.Polymorphism: Create an array of type
Vehicle
and store objects of bothVehicle
andCar
classes in it, then call thedrive()
method on each object.Abstraction: Create an abstract class
Shape
with an abstract methodcalculateArea()
, and then create concrete subclasses likeCircle
andRectangle
that implement this method.Interface Implementation: Define an interface
Drawable
with a methoddraw()
, and then implement this interface in classes likeCircle
andRectangle
.Composition: Create a class
Engine
with attributes likehorsepower
andfuelType
, and then include an instance of this class as an attribute in theCar
class.Aggregation: Create a class
Department
with attributes likename
andlocation
, and then include a list ofEmployee
objects as an attribute in theDepartment
class.Association: Create classes
Student
andTeacher
, and establish a bi-directional association between them (i.e., a student can have multiple teachers, and a teacher can have multiple students).Dependency: Define a class
Logger
with a methodlog()
that takes a message as parameter, and then use this class in other classes likeCar
andEmployee
to log messages.Package Creation: Create packages like
com.company.model
,com.company.service
, andcom.company.ui
, and then define appropriate classes in each package.Access Modifiers: Define attributes with different access modifiers (private, protected, default, public) in a class, and demonstrate their visibility from other classes.
Static Keyword: Create a static method
calculateArea()
in a classMathUtil
that calculates the area of a circle, and then call this method without creating an object ofMathUtil
.Final Keyword: Declare a final variable
PI
with a value of 3.14 in a class, and demonstrate that its value cannot be changed.Abstract Classes: Define an abstract class
Animal
with an abstract methodmakeSound()
, and then create concrete subclasses likeDog
andCat
that implement this method.Interfaces: Create an interface
Playable
with a methodplay()
, and then implement this interface in classes likeGuitar
andPiano
.Marker Interfaces: Create a marker interface
Serializable
with no methods, and then implement it in classes likeBook
andCustomer
to indicate that they can be serialized.Nested Classes: Define a nested class
Inner
within a classOuter
, and demonstrate how to access members of the outer class from the inner class.Anonymous Classes: Create an anonymous class that implements an interface or extends a class, and instantiate it inline without giving it a name.
Local Classes: Define a local class within a method of another class, and demonstrate how to instantiate it and call its methods.
Method References: Define methods like
printMessage()
andcalculateArea()
in a class, and then create method references to these methods and pass them as arguments to other methods.Default Methods: Define an interface
Resizable
with a default methodresize()
, and then implement this interface in classes likeCircle
andRectangle
.Static Methods in Interfaces: Define an interface
MathOperations
with static methods likeadd()
andsubtract()
, and then call these methods without implementing the interface.Object Cloning: Implement the
Cloneable
interface in a class likePerson
, and then demonstrate how to create a shallow copy of an object using theclone()
method.Object Serialization: Implement the
Serializable
interface in a class likeBook
, and then demonstrate how to serialize and deserialize objects usingObjectOutputStream
andObjectInputStream
.Method Overloading: Create multiple versions of a method like
calculateArea()
in a classShape
that accept different types or numbers of parameters.Method Overriding: Override methods like
toString()
andequals()
in a class likeEmployee
to provide custom implementations.Constructor Overloading: Define multiple constructors in a class like
Person
that accept different numbers or types of parameters.
These problem statements cover a wide range of OOP concepts in Java, from basic class and object creation to more advanced topics like nested classes, lambda expressions, and object serialization.
Last updated