Assignment # 05
Name: Muhammad Faizan
Subject: Intro to Computer Programming with Java
Supervisor: Miss Rimsha Jan
Department: DAE SWT (B-25)
Date: 26 February,2026
Q1. Define a method in Java. Explain the different types of methods with
examples.
Ans. A method in Java is a block of code that performs a specific task. It promotes
code reusability and modular programming. A method can accept parameters and
may return a value.
Types of Methods:
• Predefined Methods (e.g., [Link]())
• User-defined Methods
• Static Methods
• Instance Methods
Example(Code):
Q2. What is Object-Oriented Programming (OOP)? List its main features.
Ans. Object-Oriented Programming (OOP) is a programming paradigm based on the
concept of objects and classes. It organizes software design around data rather than
functions.
Main Features:
• Encapsulation
• Inheritance
• Polymorphism
• Abstraction
Q3. What is encapsulation? How do methods help in achieving
encapsulation?
Ans. Encapsulation:
Encapsulation is the process of wrapping data and methods together into a single
unit (class) and restricting direct access to data using private access modifiers.
Getter and setter methods allow controlled access and modification of private data
members.
Q4. Explain the concept of inheritance in Java. Give a simple example.
Ans. Inheritance:
Inheritance allows one class to acquire the properties and behaviors of another
class using the 'extends' keyword.
Example(Code):
Q5. What is polymorphism? Describe the two types with examples.
Ans. Polymorphism:
Polymorphism means 'many forms'. It allows a method to behave differently
depending on the object.
Types:
• Compile-time Polymorphism (Method Overloading)
• Runtime Polymorphism (Method Overriding)
Q6. Explain method overloading and method overriding with examples.
Ans. Method Overloading:
Method Overloading occurs when multiple methods share the same name but differ
in parameters.
Method Overriding:
Method Overloading occurs when a subclass provides a specific implementation of a
method already defined in its parent class.
Q7. Write a Java program that defines a method addNumbers which
takes two integers as parameters and returns their sum.
Ans.
Q8. Create a class BankAccount demonstrating encapsulation.
Ans.
Q9. Demonstrate inheritance using Animal and its subclasses.
Ans.
Q10. Demonstrate method overloading for calculating area.
Ans.
------THE END------