Core Java

What are Access Specifiers?

One of the techniques in object-oriented programming is encapsulation. It concerns the hiding of data in a class and making this class available only through methods. Java allows you to control access to classes, methods, and fields via so-called access specifiers..

What modifiers are allowed for methods in an Interface?

Only public and abstract modifiers are allowed for methods in interfaces.

What is an Interface?

An interface is a description of a set of methods that conforming implementing classes must have. Note:

  • You can’t mark an interface as final.
  • Interface variables must be static.
  • An Interface cannot extend anything but another interfaces.

What is super?

super is a keyword which is used to access the method or member variables from the superclass. If a method hides one of the member variables in its superclass, the method can refer to the hidden variable through the use of the super keyword. In the same way, if a method overrides one of the methods in its superclass, the method can invoke the overridden method through the use of the super keyword. Note:

  • You can only go back one level.
  • In the constructor, if you use super(), it must be the very first code, and you cannot access any this.xxx variables or More >

What is method overriding?

Method overriding occurs when sub class declares a method that has the same type arguments as a method declared by one of its superclass. The key benefit of overriding is the ability to define behavior that’s specific to a particular subclass type. Note: • The overriding method cannot have a more restrictive access modifier than the method being overridden (Ex: You can’t override a method marked public and make it protected). • You cannot override a method marked final • You cannot override a method marked static

What are the differences between method overloading and method overriding?

Overloaded Method Overridden Method Arguments Must change Must not change Return type Can change Can’t change except for covariant returns Exceptions Can change Can reduce or eliminate. Must not throw new or broader checked exceptions Access Can change Must not make more restrictive (can be less restrictive) Invocation Reference type determines which overloaded version is selected. Happens at compile time. Object type determines which method is selected. Happens at runtime.

What is Servlet Chaining?

Servlet Chaining is a method where the output of one servlet is piped into a second servlet. The output of the second servlet could be piped into a third servlet, and so on. The last servlet in the chain returns the output to the Web browser.

What is an Iterator ?

  • The Iterator interface is used to step through the elements of a Collection.
  • Iterators let you process each element of a Collection.
  • Iterators are a generic way to go through all the elements of a Collection no matter how it is organized.
  • Iterator is an Interface implemented a different way for every Collection.

How do you remove elements during Iteration?

Iterator also has a method remove() when remove is called, the current element in the iteration is deleted.

Why are Iterators returned by ArrayList called Fail Fast ?

Because, if list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.