☕️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

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☕️
#Java_Interview_Question

28)What is Inheritance?

Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding.

@javaCode☕️
👍1
#Java_Interview_Question

More Details:
👉Inheritance in Java

▪️Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

▪️The idea behind inheritance in java is that you can create new classes that are built upon existing classes.

▪️When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.

▪️Inheritance represents the IS-A relationship, also known as parent-child relationship.

👉Why use inheritance in java

▪️For Method Overriding (so runtime polymorphism can be achieved).

▪️For Code Reusability.

@javaCode☕️
👍1
#Java_Interview_Question

29) Which class is the superclass for every class.

Object class.

@javaCode☕️
#Java_Interview_Question

30) Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in java in case of class.

@javaCode☕️
#Java_Interview_Question

31) What is composition?

Holding the reference of the other class within some other class is known as composition.

@javaCode☕️
#Java_Interview_Question

32) What is difference between aggregation and composition?

Aggregation represents weak relationship whereas composition represents strong relationship. For example: bike has an indicator (aggregation) but bike has an engine (compostion).

@javaCode☕️
#Design_Patterns
#Builder_Design_Pattern

👉Structure

The Reader encapsulates the parsing of the common input. The Builder hierarchy makes possible the polymorphic creation of many peculiar representations or targets.

@javaCode☕️
#Java_Interview_Question

33) Why Java does not support pointers?

Pointer is a variable that refers to the memory address. They are not used in java because they are unsafe(unsecured) and complex to understand.

@javaCode☕️
#Java_Interview_Question

34) What is super in java?

It is a keyword that refers to the immediate parent class object.

@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 34) What is super in java? It is a keyword that refers to the immediate parent class object. @javaCode☕️
▪️More details:

👉super keyword in java

The super keyword in java is a reference variable which is used to refer immediate parent class object.

Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.

👉Usage of java super Keyword

1️⃣super can be used to refer immediate parent class instance variable.

2️⃣super can be used to invoke immediate parent class method.

3️⃣super() can be used to invoke immediate parent class constructor.

@javaCode☕️
#Design_Patterns

#Builder_Design_Pattern

👉Example

The Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. This pattern is used by fast food restaurants to construct children's meals. Children's meals typically consist of a main item, a side item, a drink, and a toy (e.g., a hamburger, fries, Coke, and toy dinosaur). Note that there can be variation in the content of the children's meal, but the construction process is the same. Whether a customer orders a hamburger, cheeseburger, or chicken, the process is the same. The employee at the counter directs the crew to assemble a main item, side item, and toy. These items are then placed in a bag. The drink is placed in a cup and remains outside of the bag. This same process is used at competing restaurants.

@javaCode☕️