OOP Viva
OOP Viva
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.
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.
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.
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.
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.
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.
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.
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.
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 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
Implementing Subclasses
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 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.
An abstract class cannot be instantiated, and it can contain abstract methods which must be
implemented by subclasses.
Interfaces define a contract for classes without providing implementation. A class can implement
multiple interfaces.
Nested interfaces are interfaces declared within another interface or class. Enums define a set of
named constants.
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
Importing Packages
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 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 finally block is used to execute important code such as closing resources, whether or not
an exception is thrown.
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.