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

18)Is constructor inherited?

No, constructor is not inherited.

@javaCode☕️
#Java_Interview_Question

19) Can you make a constructor final?

No, constructor can't be final.

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

#Abstract_Factory_Design_Pattern

Example

The purpose of the Abstract Factory is to provide an interface for creating families of related objects, without specifying concrete classes. This pattern is found in the sheet metal stamping equipment used in the manufacture of Japanese automobiles. The stamping equipment is an Abstract Factory which creates auto body parts. The same machinery is used to stamp right hand doors, left hand doors, right front fenders, left front fenders, hoods, etc. for different models of cars. Through the use of rollers to change the stamping dies, the concrete classes produced by the machinery can be changed within three minutes.

@javaCode☕️
#Java_Interview_Question

20) What is static variable?

static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
static variable gets memory only once in class area at the time of class loading.

@javaCode☕️
#Java_Interview_Question

📜More Details:

Java static keyword

▪️The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.

▪️The static can be:

variable (also known as class variable)
method (also known as class method)
block
nested class

▪️Advantage of static variable

It makes your program memory efficient (i.e it saves memory).

Java static property is shared to all objects.

@javaCode☕️
#Design_Patterns

#Abstract_Factory_Design_Pattern

Check list

1️⃣Decide if "platform independence" and creation services are the current source of pain.
2️⃣Map out a matrix of "platforms" versus "products".
3️⃣Define a factory interface that consists of a factory method per product.
4️⃣Define a factory derived class for each platform that encapsulates all references to the new operator.
5️⃣The client should retire all references to new, and use the factory methods to create the product objects.

@javaCode☕️
#Java_Interview_Question

21) What is static method?

A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.
static method can access static data member and can change the value of it.

@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 21) What is static method? A static method belongs to the class rather than object of a class. A static method can be invoked without the need for creating an instance of a class. static method can access static data member and…
More details:

Restrictions for static method

There are two main restrictions for the static method.
They are:
1️⃣The static method can not use non static data member or call non-static method directly.
2️⃣this and super cannot be used in static context.

@javaCode☕️
#Java_Interview_Question

22) Why main method is static?

because object is not required to call static method if It were non-static method,jvm creats object first then call main() method that will lead to the problem of extra memory allocation.

@javaCode☕️
#Design_Patterns

#Abstract_Factory_Design_Pattern

👉Rules of thumb

▪️Sometimes creational patterns are competitors: there are cases when either Prototype or Abstract Factory could be used profitably. At other times they are complementary: Abstract Factory might store a set of Prototypes from which to clone and return product objects, Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementation.

▪️Abstract Factory, Builder, and Prototype define a factory object that's responsible for knowing and creating the class of product objects, and make it a parameter of the system. Abstract Factory has the factory object producing objects of several classes. Builder has the factory object building a complex product incrementally using a correspondingly complex protocol. Prototype has the factory object (aka prototype) building a product by copying a prototype object.

▪️Abstract Factory classes are often implemented with Factory Methods, but they can also be implemented using Prototype.

▪️Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.

▪️Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.

▪️Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.

@javaCode☕️
#Java_Interview_Question

23) What is static block?

Is used to initialize the static data member.
It is excuted before main method at the time of classloading.

@javaCode☕️
#Java_Interview_Question

24) Can we execute a program without main() method?

Ans) Yes, one of the way is static block.

@javaCode☕️
#Java_Interview_Question

25) What if the static modifier is removed from the signature of the main method?

Program compiles. But at runtime throws an error "NoSuchMethodError".

@javaCode☕️
#Design_Patterns

#Builder_Design_Pattern

👉Intent

▪️Separate the construction of a complex object from its representation so that the same construction process can create different representations.

▪️Parse a complex representation, create one of several targets.

@javaCode☕️
#Design_Patterns

#Builder_Design_Pattern

👉Problem

An application needs to create the elements of a complex aggregate. The specification for the aggregate exists on secondary storage and one of many representations needs to be built in primary storage.

@javaCode☕️
#Java_Interview_Question

26) What is difference between static (class) method and instance method?


1️⃣A method i.e. declared as static is known as static method. A method i.e. not declared as static is known as instance method.

2️⃣Object is not required to call static method. Object is required to call instance methods.

3️⃣Non-static (instance) members cannot be accessed in static context (static method, static block and static nested class) directly. static and non-static variables both can be accessed in instance methods.

4️⃣For example: public static int cube(int n){ return n*n*n;} For example: public void msg(){...}.

@javaCode☕️
#Java_Interview_Question

27) What is this in java?

It is a keyword that that refers to the current object.

@javaCode☕️
#Java_Interview_Question

More Details:

👉Usage of java this keyword

Here is given the 6 usage of java this keyword.

1️⃣this can be used to refer current class instance variable

2️⃣this can be used to invoke current class method (implicitly)

3️⃣this() can be used to invoke current class constructor.

4️⃣this can be passed as an argument in the method call.

5️⃣this can be passed as argument in the constructor call.

6️⃣this can be used to return the current class instance from the method.

▪️Suggestion: If you are beginner to java, lookup only three usage of this keyword.

@javaCode☕️
#Design_Patterns

#Builder_Design_Pattern

👉Discussion

Separate the algorithm for interpreting (i.e. reading and parsing) a stored persistence mechanism (e.g. RTF files) from the algorithm for building and representing one of many target products (e.g. ASCII, TeX, text widget). The focus/distinction is on creating complex aggregates.

The "director" invokes "builder" services as it interprets the external format. The "builder" creates part of the complex object each time it is called and maintains all intermediate state. When the product is finished, the client retrieves the result from the "builder".

Affords finer control over the construction process. Unlike creational patterns that construct products in one shot, the Builder pattern constructs the product step by step under the control of the "director".

@javaCode☕️