JavaRush /Java Blog /Random EN /Java. Interview Reference Guide - Part 1
dah4uk
Level 23

Java. Interview Reference Guide - Part 1

Published in the Random EN group
The original article is located at: http://www.javacodegeeks.com/2014/02/java-interview-reference-guide-part-1.html Posted by: Nitin Kumar, in Core Java, February 3, 2014

Java. Object-oriented concept.

Java is based on an object oriented concept which gives a higher level of abstraction to solve any problem in a real way. The object-oriented approach conceptualizes the solution to a problem in real-world objects that are easier to reuse in applications. For example: chair, fan, dog, computer, etc. In Java, a class is a documentation, template, or prototype that defines the general behavior of objects of the same kind. An instance is an implementation of a particular class, and all instances of a class have the same properties as presented in the class description. For example, you can define a class called House with the number of rooms as an attribute and create objects: a house with 2 rooms, a house with 3 rooms, and so on.
Advantages:
Listed below are several advantages of developing object-oriented software:
  • Less maintenance costs, mainly due to modularity.
  • Code is easier to reuse through features such as inheritance, which speeds up development as a result.
  • Increased reliability and flexibility of the code.
  • The code is easier to understand, this is due to the simulation of the real world.
  • Better abstraction at the object level.
  • Reducing the complexity of the transition from one stage of development to another.
There are four basic concepts of OOP:
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction
Encapsulation:
Encapsulation is a rule for other objects that specifies which elements are hidden and which are exposed to other objects. In Java, we use the private access modifier to hide a method and restrict access to a variable from the outside. Java also provides various access modifiers such as public which is the default, protected and private which are used to restrict visibility at various levels, but the ultimate goal of encapsulation is to hide those elements that should not be changed. In practice, it's best when the class has only one reason to change, and the encapsulation implements the design principles of that "one reason". In practice, Encapsulation is supposed to hide methods to avoid breaking other classes.
Advantages:
Here are a few benefits of encapsulation:
  • You can protect the internal state of an object by hiding its attribute.
  • This increases the modularity of the code by preventing interactions between objects in unexpected ways.
  • Ease of use is increased.
  • Object-specific conditions are supported.
  • Encapsulation makes software easier to maintain
  • Code changes can be independent
Polymorphism:
Polymorphism is the ability (in programming) to present the same interface for different underlying forms (data types). This means that the classes have different functionality despite sharing a common interface and can be called dynamically through a special class reference. A classic example is the Shape class (figure), and all classes that can be inherited from it (square, circle, dodecahedron, irregular polygon, sign, etc.). In this example, each class will have its own Draw() function and client code can do the following: Shape shape = new Square(); Shape.area() чтобы получить корректное поведение для любой формы. The beauty of polymorphism is that the code works with different classes and there is no need to know which class uses it, because. they all work on the same principle. The process used by Object Oriented Programming Languages ​​to implement dynamic polymorphism is called dynamic binding. Note: Polymorphism is the ability to select more specialized methods depending on the execution of the invoked object. Polymorphism can also be used without the involvement of any abstract classes.
Advantages:
  • Reusable code is created: this means that once classes are created, implemented and tested, they can be easily reused without worrying about what is written in the class.
  • This provides more general and loosely coupled code.
  • Compilation time is greatly reduced and development is faster.
  • Dynamic binding: The same interface can be used to create methods with different implementations.
  • The full implementation can be replaced with a method signature.
Overriding a method to achieve Polymorphism: Overriding interfaces with two methods: One in the parent class and one in the child class with the same names and signatures. Overriding allows you to define the same operation differently for different data types. For example: while(it.hasNext()) { Shape s = (Shape) it.next(); totalArea += s.area(dim); //полиморфический вызов метода. Будет вызван правильный an object. } Java.  Interview Reference Guide - Part 1. (Article Translation) - 1
Method Overloading or Special Polymorphism or Static Polymorphism:
Overloading interfaces with multiple methods in the same class with the same name but a different method body. Method overloading allows you to define the same operation differently for different data. It was called static polymorphism for a while, but it's not really polymorphism. A method overload is no more than two methods, with the same name but different argument lists. It has nothing to do with inheritance and polymorphism. An overloaded method is not the same as an overridden method. [Chapter One Java]
Parametric polymorphism using generics in Java:
When declaring a class, the field name can be associated with different types, and the method name can also be associated with different parameters and return types. Java supports parametric polymorphism using generics. An example is a list that can take a data type containing generics. List list = new ArrayList ();
Why can't we override a static method in Java?
The override depends on the existence of an instance of the class. The meaning of polymorphism is that you can create a subclass of a class and an object implements this subclass, there will be different behavior of the same methods defined in the superclass (or overridden in the subclass). A static method is not associated with any instance of the class, so this concept does not apply to it. Previously, two main ideas were put forward, the development of development in Java, which influenced it. One of them is the attitude towards performance: there was a lot of criticism from Smalltalk that the JVM was too slow (due to garbage collection and polymorphic calls that were part of the reason) and the Java developers were forced to fix it. The other was that the target audience for Java was C++ developers. Calling static methods is familiar to C++ programmers and works just as fast because there is no way up the class hierarchy, and to figure out which method to call, you go straight to the class and call the specified method. [stack overflow]
Inheritance:
It includes the behaviors (i.e. methods) and states (i.e. variables) of the base class into the derived class so that they are available in that derived class. The key benefit of inheritance is that it provides a formal mechanism for code reuse and avoids duplication. An inherited class extends the application's functionality by reusing parent behavior and adding new functionality. This will keep the design tightly coupled, because if you want to change the superclass, you must know all the details of the subclasses to avoid breaking the application. This is a form of software reuse when a new class (subclass) is created from an already existing class (superclass) and extends its functionality while using some of the properties of the superclass. So,
Advantages:
  • Easier to reuse code
  • The logical relation "Is someone" is established. For example: A dog is an animal.
  • Code becomes modular
  • Avoids duplication
Flaws:
  • Tight coupling: A subclass depends on the implementation of the parent class, making the code tightly coupled.
Abstraction:
Abstraction is the development of a class from the conditions of interfaces and their functionality, without considering the details of their implementation. An abstract class includes interfaces without an actual implementation. It separates the implementation of an object from the behavior or implementation. Abstraction simplifies development by hiding non-essential details.
Advantages:
  • When using abstraction, we can isolate objects that can be grouped into another type.
  • Often property or method changes can be grouped into a single type, leaving the main type unchanged. This reinforces the principle of Object-Oriented Analysis and Design - "Code should be open to extension, but closed to modification".
  • Simplifies the representation of domain models.
Differences Between Abstraction and Encapsulation
Encapsulation is a strategy used within an abstraction. Encapsulation refers to the state of objects - objects encapsulate their state and hide it from outside access; From the outside, users of a class can interact with its methods, but cannot directly access the members of the class. In this way, a class abstracts away the implementation details related to its state. Abstraction is a more general term; it can also be achieved through the use of subclasses (among others). For example, a list of classes in the standard library is an abstraction for a sequence of elements indexed by their position, concrete examples of a List would be ArrayList or LinkedList. The code that interacts with the list of abstractions is more detailed than the kind of list used. [Stack overflow] Abstraction is often impossible,
What is abstract class and abstract method?
In design, you only want the base class to provide an interface to its derived classes. This means you don't want anyone to instantiate an object of that class. You only want there to be a cast (an implicit cast to the base type that gives you polymorphic behavior), so that interface can be used. This is achieved by making the class abstract, using the abstract keyword. There are some restrictions: you cannot create an instance of an abstract class, you should use only the class that implements the abstract methods. And provides polymorphism. An abstract class can contain both abstract methods and concrete methods. In a class, if one method is declared abstract, the class must be declared abstract. However, the reverse is not always true. If a class is declared abstract, it may not have abstract methods in it. If a method does not provide an actual implementation, but does provide a method signature, it is called an abstract method. The actual implementation rests with subclasses that extend the abstract class. An abstract method cannot be implemented; only another class can inherit from it.
When is an abstract class used?
Abstract classes help define some default behaviors and provide subclasses with some specific behavior. For example: List is an interface, while AbstractList implements List's default behavior, which can be used as is, or can be implemented in a subclass, such as ArrayList.
What is an Interface?
The interface keyword takes the concept of an abstract class further, preventing any method or function from being implemented for everyone. You can only declare a method or function, not provide an implementation. The class that implements the interface must provide the actual implementation. The interface is a very powerful and widely used aspect of OO design because it provides a separation between interface and implementation, and allows you to:
Interface advantages:
  • Multiple Inheritance
  • Loose pair-defined operation abstractions, as a separate implementation type, can be anything: JDBC, JPA, JTA, etc.
  • The program interface is not implemented.
  • Polymorphism with dynamic binding - reveals the object of the programming interface without exposing its actual implementation.
  • Abstract Level: Problems of Separation
Difference between interface and abstract class:
  • An interface is a convention by which classes that implement an interface are asked how they define that interface. This is an empty wrapper with a method declaration.
  • An abstract class defines some common behavior and gives a subclass the property to define rare or specific behavior for that class.
  • Methods and members of an abstract class can be defined with any type of visibility, while all interface methods must be defined as public.
  • When inheriting from an abstract class, the child class must define the abstract methods, while an interface may extend another interface and the methods must not be defined.
  • A child class can only inherit from one abstract (or any other) class, while an interface or class can inherit from several other interfaces.
  • A child class may implement abstract methods with the same or less restrictive visibility level, while a class that implements an interface must define methods with the same visibility level.
  • An interface cannot contain constructors, unlike an Abstract class.
  • Variables declared in a Java interface have the final access modifier by default. An abstract class can contain variables not only with the final modifier.
  • Interface methods in Java have the public access modifier by default. An abstract class in Java can have the usual access modifiers like private, protected, etc.
Composition:
Code reusability can be achieved by implementing inheritance or composition, but the composition approach to code reuse provides stronger encapsulation than inheritance because changing the back-end class should not break code that relies only on the front-end class. Composition is a design technique for implementing existing relationships in classes. We can use Java inheritance or composition to reuse code. Composition expresses the connections between objects. For example, imagine a chair. The chair has a seat. The chair has a back. And has a set of legs. The phrase "has" implies a relationship where the chair owns something, or at least it uses another object. It is these relationships of the type "Has"
Advantages:
No. Composition (to have) inheritance (to be)
1 Supports polymorphism and code reuse Supports polymorphism and code reuse
2 Object is created at run time Object is created dynamically at compile time
3 Implementation can be changed at runtime Implementation can be changed at runtime
4 The subclass does not depend on the parent class, which contributes to the weakening of ties (in particular, in the control interface) The subclass depends on the implementation of the parent class, so they are tightly coupled
5 It is used, for example, when the House has a Bathroom. It is incorrect to say that the House is the Bathroom. Inheritance is unidirectional. For example: A house is a building. But a Building is not necessarily a House.
Note: Don't use inheritance just to be able to reuse code. If there is no "to be" relationship between classes, then you need to use composition to reuse code.
Differences between Composition and Aggregation in Object Relationships
Aggregation: An aggregation is a union in which one class belongs to a collection. It is part of the whole in a relationship where the part can exist without the whole. This is a weak relationship. There is no cyclic dependency. For example: order and product. Composition: A composition is a union in which one class belongs to a collection. It is part of the whole, where the part cannot exist without the whole. If the whole is removed, then all parts will also be removed. These relationships are stronger. Examples are: Polygon and points, order and order order.
Links:
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION