0% found this document useful (0 votes)
2 views5 pages

OOP Viva

The document provides an overview of Object-Oriented Programming (OOP) principles, including encapsulation, abstraction, inheritance, and polymorphism. It explains how these concepts are implemented in Java, detailing the use of keywords like 'this' and 'super', as well as the significance of constructors, method overloading, and exception handling. Additionally, it covers topics such as static methods, abstract classes, interfaces, and database connectivity in Java.

Uploaded by

Kainat Jamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

OOP Viva

The document provides an overview of Object-Oriented Programming (OOP) principles, including encapsulation, abstraction, inheritance, and polymorphism. It explains how these concepts are implemented in Java, detailing the use of keywords like 'this' and 'super', as well as the significance of constructors, method overloading, and exception handling. Additionally, it covers topics such as static methods, abstract classes, interfaces, and database connectivity in Java.

Uploaded by

Kainat Jamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

 What are the four pillars of Object-Oriented Programming?

 Encapsulation, Abstraction, Inheritance, and Polymorphism.

 Can you describe how encapsulation is used in your application?

 Encapsulation is used by declaring class fields as private and providing public getter and
setter methods to access and modify these fields. For example, in the Dashboard class,
member variables like mAuth, user_db, and userdb_ref are private, and their access is
controlled within the class.

 What is the role of inheritance in your application?

 Inheritance allows classes to inherit properties and methods from other classes. In the
application, the Dashboard class inherits from AppCompatActivity, which provides
essential functionality for an Android activity, such as lifecycle management and UI
components.

 How do you implement polymorphism in your project?

 Polymorphism is implemented through method overriding, where methods like onCreate,


onStart, and onResume are overridden in the Dashboard class to provide specific
behavior. Additionally, fragments like HomeView and BloodInfo override methods from
the Fragment class to customize their behavior.

 What is the significance of the this and super keywords in Java?

 The this keyword refers to the current instance of the class. For example, in pd = new
ProgressDialog(this);, this refers to the current instance of Dashboard. The super
keyword is used to call the parent class's constructor or methods. In the Dashboard class,
calling super.onCreate(savedInstanceState); ensures that the parent class's onCreate
method is executed.

What is Object-Oriented Programming (OOP)?

Answer: Object-Oriented Programming (OOP) is a programming paradigm based on the concept


of objects, which can contain data and code. Data is represented as attributes (fields or
properties), and code is represented as methods (functions or procedures).

2. How is OOP different from procedural programming?

Answer: OOP is based on objects and classes, promoting modularity and reusability. Procedural
programming is based on procedures or routines, focusing on a sequence of actions or commands
to be performed.

3. Explain Encapsulation with an example.


Answer: Encapsulation is the bundling of data and methods that operate on that data within a
single unit (class). It restricts direct access to some of an object's components, which is useful for
preventing accidental interference and misuse. For example:
What is Inheritance in Java?
Answer: Inheritance is a mechanism in Java where one class acquires the properties and
behaviors (methods) of another class. The class that inherits is called the subclass (or derived
class), and the class being inherited from is called the superclass (or base class).
Explain Polymorphism with an example.
Answer: Polymorphism allows methods to do different things based on the object it is acting
upon, even if they share the same name. There are two types: compile-time (method overloading)
and runtime (method overriding).

What is an Array in Java?

Answer: An array is a container object that holds a fixed number of values of a single type. The
length of an array is established when the array is created. After creation, its length is fixed.

How do you declare a multi-dimensional array in Java?

Answer: A multi-dimensional array is declared by specifying an array of arrays.

Explain the use of the this keyword in Java.

Answer: The this keyword in Java is a reference variable that refers to the current object. It can
be used to refer to the current class instance variable, invoke the current class method, or invoke
the current class constructor.

What is method overloading?

Answer: Method overloading is a feature that allows a class to have more than one method
having the same name, if their parameter lists are different. It is used to increase the readability
of the program.

Method Overriding in Java

Method overriding is a feature in Java that allows a subclass to provide a specific


implementation of a method that is already defined in its superclass. The purpose of overriding is
to modify or extend the behavior of a method in the parent class.

What is a constructor in Java, and why is it used?

Answer: A constructor in Java is a block of code that initializes a newly created object. It has the
same name as the class and no return type. Constructors are used to set initial values for object
attributes.
Parameters in a Method

Definition

Parameters are variables that are passed to a method when it is called. They allow you to pass
information or arguments into methods, enabling the methods to perform operations with those
values.

Using Objects as Parameters

Objects can be passed to methods just like any other data type. When an object is passed as a
parameter, the method receives a reference to the actual object.

Argument Passing

In Java, arguments are always passed by value. When passing an object, the value of the
reference is passed, not the object itself. This means changes made to the object within the
method affect the original object.

Returning Objects

Methods can also return objects. The return type of the method should be the class type of the
object being returned.

Static Variables and Methods

Static variables and methods belong to the class rather than any instance of the class. They can
be accessed without creating an object for the class.

Using super

The super keyword refers to the superclass (parent) object and is used to access superclass
methods and constructors.

Inheritance Hierarchies

Inheritance hierarchies represent the relationship between classes in terms of inheritance.

Implementing Subclasses

Creating subclasses by extending the parent class.

The instanceof Operator

The instanceof operator is used to test whether an object is an instance of a specific class or
subclass.
Static Use in Inheritance

Static methods are inherited, but they are not overridden. They are hidden when redefined in a
subclass.

Dynamic Method Lookup and Implicit Parameter

Dynamic method lookup allows the method call to be resolved at runtime based on the actual
object. The implicit parameter is the object on which the method is invoked.

Abstract Classes and Methods

An abstract class cannot be instantiated, and it can contain abstract methods which must be
implemented by subclasses.

Characteristics of Abstract Classes

 Cannot be instantiated: You cannot create an instance of an abstract class.


 Can have abstract methods: These methods do not have a body and must be
implemented by subclasses.
 Can have concrete methods: These are regular methods with a body.
 Can have fields: Abstract classes can have fields (variables).
 Can have constructors: Although you cannot instantiate an abstract class, it can have
constructors, which are called when a subclass is instantiated.
 Can be inherited: Other classes can extend an abstract class.

Creating and Using Interfaces

Interfaces define a contract for classes without providing implementation. A class can implement
multiple interfaces.

Nested Interfaces and Enums

Nested interfaces are interfaces declared within another interface or class. Enums define a set of
named constants.

Final Methods and Classes

Final methods cannot be overridden, and final classes cannot be extended.

Static Methods in Interfaces

Static methods in interfaces are defined with the static keyword and can be called on the
interface itself.

Default Methods
Default methods in interfaces provide a default implementation and can be overridden by
implementing classes.

Defining a Package

Packages group related classes and interfaces together.

Importing Packages

To use classes from a package, you need to import the package.

Exception Handling in Java

Exception handling is a mechanism in Java to handle runtime errors, allowing the program to
continue its normal flow of execution. Java provides a robust and flexible framework to handle
exceptions.

Throwing Exceptions

You can throw exceptions in Java using the throw keyword. You can throw predefined
exceptions or create your own exceptions.

Catching Exceptions

To catch exceptions, you use the try-catch block. The code that might throw an exception is
placed inside the try block, and the exception handling code is placed inside the catch block.

Checked and Unchecked Exceptions

 Checked Exceptions: These are exceptions that are checked at compile-time. Examples
include IOException, SQLException.
 Unchecked Exceptions: These are exceptions that are checked at runtime. Examples
include ArithmeticException, NullPointerException.

The try/finally Statement

The finally block is used to execute important code such as closing resources, whether or not
an exception is thrown.

Database Connectivity in Java

Java Database Connectivity (JDBC) is an API that allows Java applications to interact with
databases. It provides methods to query and update data in a database.

You might also like