Chapter 3 -Methods Flashcards

1
Q

What is Method?

A

is a block of code that performs a specific task or set of tasks. Methods are used to organize code into reusable units, making the program easier to read, understand, and maintain. In Java, methods are associated with classes and objects and can be called to execute their defined functionality.

A method consists of a method signature, which includes the method’s name, return type, and parameters (if any). The return type specifies the type of value the method returns (void if the method doesn’t return anything), and the parameters are the inputs the method requires to perform its task.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain the syntax of a method declaration in Java

A

Access Modifier: This is an optional keyword that defines the visibility and accessibility of the method. It can be one of the following:

public: The method is accessible from anywhere.
protected: The method is accessible within its package and by subclasses.
private: The method is only accessible within the class.
Default (no modifier): The method is accessible within its package.

Return Type: This specifies the data type of the value that the method will return after its execution. It can be any valid Java data type, including primitive types (e.g., int, double) or reference types (e.g., String, custom classes).

Method Name: This is the name of the method, which is used to identify and call the method. Method names should follow Java naming conventions (e.g., using camelCase).

Parameter List: This is a comma-separated list of parameters enclosed in parentheses. Each parameter declaration consists of a data type and a parameter name. Parameters allow you to pass information into the method when it’s called.

Method Body: This is enclosed within curly braces {} and contains the actual code or statements that define what the method does. The method body is executed when the method is called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Method Signature?

A

In Java, a method signature is a combination of the following elements:

Method Name: The name of the method that uniquely identifies it within the class.

Parameter List: The list of parameters (if any) that the method accepts. Each parameter includes its data type and a parameter name. The order and data types of parameters must match when overriding a method (polymorphism).

Return Type: The data type of the value that the method returns (if any). If a method does not return a value, it has a return type of void.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is method overloading?

A

Method overloading is a feature in Java that allows you to define multiple methods in the same class with the same name but with different parameters. The key distinction among these overloaded methods is the number, type, or order of the parameters they accept. When you invoke an overloaded method, Java determines which version of the method to execute based on the arguments you pass during the method call.

Here are the key aspects of method overloading:

Same Method Name: In method overloading, all the methods have the same name. This name is used to identify the overloaded methods within the class.

Different Parameter Lists: Overloaded methods must have different parameter lists. This can include differences in the number of parameters, the data types of parameters, or the order of parameters.

Return Type: The return type of the method is not considered when determining method overloading. Two overloaded methods can have the same return type or different return types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can you override a method in Java? If yes, how?

A

Yes, you can override a method in Java. Method overriding is a fundamental concept in object-oriented programming and allows a subclass to provide a specific implementation for a method that is already defined in its superclass. This enables the subclass to customize the behavior of the inherited method to suit its needs.

Here’s how method overriding works in Java:

Inheritance: Method overriding is only applicable in the context of inheritance. You have a superclass (also known as the parent class) that contains a method, and a subclass (also known as the child class) that wants to provide a different implementation for that method.

Method Signature: To override a method, the subclass must define a method with the same name, return type, and parameter list as the method in the superclass. This means that the method signature must be identical.

@Override Annotation (Optional): While not required, it is a good practice to use the @Override annotation before the method in the subclass that is intended to override the superclass method. This annotation helps to catch errors at compile time if the method signature doesn’t match the superclass method.

Access Modifier: The overriding method in the subclass cannot have a lower access modifier than the method it is overriding in the superclass. It can have the same access modifier or a higher one (e.g., if the superclass method is protected, the subclass method can be protected or public, but not private).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the ‘void’ keyword used for in a method declaration?

A

No Return Value: When you see the void keyword as the return type of a method, it indicates that the method does not return any value or result. In other words, the method performs a task or action but does not produce any data that can be used in further computations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are method parameters, and how do you define them?

A

Method parameters, also known as method arguments, are variables or values that you pass to a method when you call it. These parameters provide input data to the method, allowing it to perform its operations based on the provided values. Parameters are defined in the method’s signature, and they serve as placeholders for the data that will be passed to the method when it is invoked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain the difference between pass-by-value and pass-by-reference in Java regarding method parameters.

A

Java uses pass-by-value for method parameters, but it behaves differently for primitive data types and objects (including references to objects).

Pass-by-Value for Primitive Data Types:

For primitive data types (e.g., int, double, char, boolean), the actual value is passed to the method. Any changes made to the parameter inside the method do not affect the original value outside the method.
In other words, a copy of the value is passed to the method, and modifications inside the method do not propagate back to the caller.

Pass-by-Value for Object References:

For objects (including arrays), what’s actually passed by value is the reference or memory address to the object, not the object itself.
Changes made to the object’s state (i.e., its fields) inside the method are reflected in the original object because both the caller and the method share a reference to the same object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is method chaining, and when would you use it?

A

Method chaining is a programming technique that involves calling multiple methods on an object in a single line of code. Each method returns an object (usually the same object on which the method was called), allowing you to chain subsequent method calls on the returned object. This results in a concise and readable code structure, where each method call modifies the state or behavior of the object and passes it on to the next method in the chain.

Car myCar = new Car()
.setMake(“Toyota”)
.setModel(“Camry”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

public methods are accessible from?

A

public: Methods declared as public are accessible from any other class or package. They have the widest visibility and can be called from anywhere.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Protected methods are accessible from?

A

Methods declared as protected are accessible within the same package and by subclasses, even if they are in different packages. This modifier is commonly used for creating methods that are meant to be accessed by subclasses for method overriding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Private MEthods are accessible from?

A

private: Methods declared as private are the most restricted in terms of visibility. They are accessible only within the same class and cannot be accessed or overridden by subclasses or other classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Default methods are accessible from?

A

default (Package-Private): Use when you want to restrict access to methods within the same package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Method recursion

A

Method recursion is a programming technique in which a method calls itself to solve a problem or perform a task. This is a fundamental concept in computer science and is commonly used in various programming languages, including Java. In recursive methods, a problem is broken down into smaller subproblems that are solved recursively until a base case is reached, at which point the recursion stops.

Here’s the basic structure of a recursive method:

Base Case: A condition that defines when the recursion should stop. It is the simplest form of the problem that can be solved directly.

Recursive Case: The method calls itself with modified parameters to solve a smaller or simpler subproblem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can methods have multiple return statements?

A

Yes, a method can have multiple return statements in Java. In fact, it’s quite common for methods to have multiple return statements, depending on different conditions or branches within the method. Each return statement specifies the value that the method should return when that particular condition is met.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are varargs (variable-length arguments) in Java methods?

A

Varargs (short for variable-length arguments) is a feature in Java that allows you to create methods that can accept a variable number of arguments of the same type. Defining a Varargs Parameter:

To declare a varargs parameter in a method, you use an ellipsis (…) followed by the type of the elements you want to accept. This parameter must be the last one in the method’s parameter list

17
Q

Difference between static and instance methods

A

Definition:

Static Method (Class Method): A static method is associated with the class itself, not with any specific instance of the class. It is defined using the static keyword.
Instance Method (Non-Static Method): An instance method is associated with instances (objects) of the class. It operates on the state and behavior of a specific object and is not associated with the class itself.
Access:

Static Method: Static methods can be called directly on the class itself without creating an instance of the class. For example, ClassName.staticMethod().
Instance Method: Instance methods are called on objects (instances) of the class. You first need to create an object and then call the method on that object, like objectName.instanceMethod().
Access to Class Members:

Static Method: Static methods cannot directly access or modify instance-specific (non-static) members (fields or methods) of the class. They can only access other static members.
Instance Method: Instance methods can access both static and instance members of the class. They have access to this to refer to the current object.
Use Cases:

Static Method: Static methods are typically used for utility functions, operations that don’t depend on instance-specific data, or when you want to perform an action at the class level (e.g., creating factory methods).
Instance Method: Instance methods are used to perform actions that involve the state of a particular object. They can access instance-specific fields and methods.
Polymorphism:

Static Method: Static methods cannot be overridden in subclasses because they are associated with the class itself, not with instances.
Instance Method: Instance methods can be overridden in subclasses to provide specialized behavior.
Memory Usage:

Static Method: Static methods do not have access to instance-specific data, so they do not contribute to the memory used by individual objects.
Instance Method: Instance methods can access and manipulate instance-specific data, contributing to the memory used by individual objects.

18
Q

How do you call a superclass’s method from a subclass in Java?

A

Two ways:
Method Overriding: First, ensure that the method you want to call in the superclass is overridden in the subclass. Method overriding is the process of providing a new implementation of a method in the subclass with the same name, return type, and parameter list as the superclass’s method.

Use the super Keyword: Inside the overridden method of the subclass, use the super keyword followed by a dot (.) to invoke the superclass’s method. You provide the method name and any required arguments as if you were calling any other method.

19
Q

What is method hiding?

A

Method Hiding:

Definition: Method hiding occurs when a subclass defines a static method with the same name and method signature as a static method in its superclass. This results in the subclass “hiding” the static method of the superclass.

Static Method: Method hiding is specific to static methods. It does not apply to instance (non-static) methods.

No Polymorphism: Method hiding does not involve polymorphism. When you call the hidden method, the method in the class where the call is made (the reference type) is the one that gets invoked, not the method in the object’s actual class (the runtime type).

No Inheritance: Method hiding does not represent a form of method inheritance or specialization. Subclasses can define their own static methods with the same name, but there is no relationship between the superclass’s static method and the subclass’s static method.

Parent.staticMethod(); // Calls Parent’s static method
Child.staticMethod(); // Calls Child’s static method
Parent p = new Child();
p.staticMethod(); // Calls Parent’s static method (method hiding)

20
Q

How can you prevent a method from being overridden in Java?

A

In Java, you can prevent a method from being overridden by using the final modifier or by declaring the entire class as final