java Flashcards

1
Q

how negative numbers are stored in java

A

in 2’s compliment plus 1 form. first bit is always sign bit(whether number is positive or negative

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

difference between constructor and setter method

A

using a constructor ensures that mandatory fields are set, preventing the object from being in an incomplete state. Setters are often used for optional fields or for fields that need to change after object creation.

A constructor is used once per object creation, but setters can be used multiple times on the same object.

Constructors (including parameterized ones) are used for initializing objects, while setters are used for modifying the state of an already initialized object.

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

why do i even need constuctors?

A

so constructor initializes the object at the time of creation

why is it needed? for the following reasons
1) initialize an object to a valid state right when it is created. This is important because it prevents the object from being in an undefined or invalid state. eg: NullPointerException, undefined etc
2) data integrity by ensuring that all necessary fields are initialized and that the object cannot be left in an incomplete or inconsistent state.
3) Constructors can provide default values for fields if no specific values are provided
4) can have complex initializing logic (eg database connection ) ensures everything is properly set from the start

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

diamond problem

A

java doesnt support multiple inheritance, but a class can implement multiple interfaces
A
/ \
B C
\ /
D

If A has a method doSomething(), and B and C do not override it, the ambiguity arises: which version of doSomething() should D inherit

Java resolves the diamond problem by requiring explicit method overrides in case of conflicts

In class implementing the interface,
@Override
public void greet() { => greet is the signature which is in conflict. so mention the interface name
// Resolving the conflict by explicitly choosing which default method to use
InterfaceA.super.greet();
InterfaceB.super.greet();
}

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

how abstraction is achieved in the interface?

A

there are two classes. on which implemets the intefrace methods, another another which call the interface method along with the desired implemented class- interace user.

now the interface user doent need to know how the function is implemented in the class. instead it is just creatg an object of it and calling the interface method.

public class PaymentProcessor {
public void processPayment(Payment payment, double amount) {
payment.pay(amount);
}

public static void main(String[] args) {
    PaymentProcessor processor = new PaymentProcessor();

    Payment creditCardPayment = new CreditCardPayment();
    Payment paypalPayment = new PayPalPayment();
    Payment bitcoinPayment = new BitcoinPayment();

    processor.processPayment(creditCardPayment, 100.0);
    processor.processPayment(paypalPayment, 200.0);
    processor.processPayment(bitcoinPayment, 300.0);
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what are default methods in interface?

A

Problem: Before Java 8, adding a new method to an existing interface would break all classes that implement that interface, as they would be forced to implement the new method.

Solution: Default methods provide a way to add new methods to an interface with a default implementation, ensuring that existing classes remain unaffected and do not break.

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

how multiple inheritance behaviour is achieved?

A

it is achieved through default methods in interfaces. begore java 8, it can only have abstract methods with out implemetation. with default methods, you can also add implementation, which means classes implemenying this interface is inheriting this behaviour(implemetation of default method) also

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

super keyword

A

The super keyword is used to refer to the superclass. It can be used to call the superclass’s constructor, methods, or access its fields.

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

advantages of inheritance

A

Code Reusability: By inheriting from an existing class, a new class can reuse code from the superclass, reducing redundancy.

Method Overriding: Subclasses can override methods to provide specific implementations while still retaining the interface defined by the superclass.

Polymorphism: Inheritance allows for polymorphism, where a subclass instance can be treated as an instance of its superclass. This enables flexible and interchangeable code design.

Hierarchy: Inheritance helps in creating a class hierarchy, which can simplify the organization and understanding of code.

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

types of polymorphism

A

Runtime(dynamic) and compiletime(static)

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

how Runtime(dynamic) polymorphism is achieved?

A

Achieved through inheritance and method overriding.
Achieved through interfaces and method implementation.

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

how compiletime(static) polymorphism is achieved?

A

Achieved through method overloading.
defining multiple methods with the same name but different parameters within the same class.

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