☕️JAVA Language Community
2.91K subscribers
144 photos
7 videos
31 files
42 links
☕️ Software, IT, Java, news
💻 IT highlights
🎯 AI update
🖥⌨️🖱
Download Telegram
#Design_Patterns

#Creational_patterns

#Prototype_Design_Pattern

👉Intent

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Co-opt one instance of a class for use as a breeder of all future instances.
The new operator considered harmful.

@javaCode☕️
#Java_Interview_Question

75) What is the base class for Error and Exception?

Throwable.

@javaCode☕️
#Java_Interview_Question

76) Is it necessary that each try block must be followed by a catch block?

It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

@javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Problem

Application "hard wires" the class of object to create in each "new" expression.

@javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Discussion

Declare an abstract base class that specifies a pure virtual "clone" method, and, maintains a dictionary of all "cloneable" concrete derived classes. Any class that needs a "polymorphic constructor" capability: derives itself from the abstract base class, registers its prototypical instance, and implements the clone() operation.

The client then, instead of writing code that invokes the "new" operator on a hard-wired class name, calls a "clone" operation on the abstract base class, supplying a string or enumerated data type that designates the particular concrete derived class desired.

@javaCode☕️
#Prototype_Design_Pattern

👉Structure

The Factory knows how to find the correct Prototype, and each Product knows how to spawn new instances of itself.

@javaCode☕️
#Java_Interview_Question

77) What is finally block?

finally block is a block that is always executed.

@javaCode☕️
#Java_Interview_Question

78) Can finally block be used without catch?

Yes, by try block. finally must be followed by either try or catch.

@javaCode☕️
#Java_Interview_Question

79) Is there any case when finally will not be executed?

finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).

@javaCode☕️
#Java_Interview_Question

80) What is difference between throw and throws?


1)throw is used to explicitly throw an exception.

throws is used to declare an exception.


2)checked exceptions can not be propagated with throw only.

checked exception can be propagated with throws.


3)throw is followed by an instance.

throws is followed by class.


4)throw is used within the method.

throws is used with the method signature.


5)You cannot throw multiple exception

@javaCode☕️
☕️JAVA Language Community
#Prototype_Design_Pattern @javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Example

The Prototype pattern specifies the kind of objects to create using a prototypical instance. Prototypes of new products are often built prior to full production, but in this example, the prototype is passive and does not participate in copying itself. The mitotic division of a cell - resulting in two identical cells - is an example of a prototype that plays an active role in copying itself and thus, demonstrates the Prototype pattern. When a cell splits, two cells of identical genotype result. In other words, the cell clones itself.

@javaCode☕️
#Java_Interview_Question

81) Can an exception be rethrown?

Yes.

@javaCode☕️
#Java_Interview_Question

82) Can subclass overriding method declare an exception if parent class method doesn't throw an exception ?

Yes but only unchecked exception not checked.

@javaCode☕️
#Java_Interview_Question
More details:

👉ExceptionHandling with MethodOverriding in Java

There are many rules if we talk about methodoverriding with exception handling.

The Rules are as follows:

1️⃣If the superclass method does not declare an exception

If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.


2️⃣If the superclass method declares an exception

If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

@javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Check list

1️⃣Add a clone() method to the existing "product" hierarchy.

2️⃣Design a "registry" that maintains a cache of prototypical objects. The registry could be encapsulated in a new Factory class, or in the base class of the "product" hierarchy.

3️⃣Design a factory method that: may (or may not) accept arguments, finds the correct prototype object, calls clone() on that object, and returns the result.

4️⃣The client replaces all references to the new operator with calls to the factory method.

@javaCode☕️
#Java_Interview_Question

83) What is exception propagation ?

Forwarding the exception object to the invoking method is known as exception propagation.

@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 83) What is exception propagation ? Forwarding the exception object to the invoking method is known as exception propagation. @javaCode☕️
More details:

👉Java Exception propagation

An exception is first thrown from the top of the stack and if it is not caught, it drops down the call stack to the previous method,If not caught there, the exception again drops down to the previous method, and so on until they are caught or until they reach the very bottom of the call stack.This is called exception propagation.

▪️Rule: By default Unchecked Exceptions are forwarded in calling chain (propagated).

Exception can be handled in any method in call stack either in main() method,p() method,n() method or m() method.

▪️Rule: By default, Checked Exceptions are not forwarded in calling chain (propagated).

@javaCode☕️