Java Interview Guide: How to Build Confidence With a Solid Understanding of Core Java Principles pdfdrive com



Yüklə 0,53 Mb.
Pdf görüntüsü
səhifə12/47
tarix06.05.2023
ölçüsü0,53 Mb.
#108601
1   ...   8   9   10   11   12   13   14   15   ...   47
Java Interview Guide

Abstract Classes vs Interfaces
It’s a common misconception to assume that abstract classes and interfaces
compete for functionality; in practice they frequently work together.
For example, Java’s AbstractList class partially implements methods from
the List interface, lessening the burden for subclasses such as ArrayList
and LinkedList. In general, abstract classes are used to plan an inheritance
hierarchy and simplify the work of subclasses. Interfaces are used to introduce
a layer of abstraction and decouple modules of code.


Anonymous Classes
In order to reduce the verbosity of the language, Java allows anonymous classes
to be instantiated with the new keyword. For example, every button in a Swing
application has an ActionListener that is invoked when the button
is clicked. Rather than create a new class file for every action, you could instead
declare anonymous classes that implement the ActionListener interface.
Anonymous classes provide support for closures. A closure is a block of code
that can be passed around while maintaining access to variables of the enclosing
scope. For example, a button that changes the title of a Swing application would
need a closure to access the window frame. Although it may appear that
anonymous classes have direct access to enclosing variables, in fact the variables
are copied in the same manner as method arguments (pass-by-value). In order
to prevent ambiguity, all enclosing variables must be declared final before
they can be accessed by an anonymous class.
Java 8 provides even more succinct support for closures in the form of lambda
expressions. A lambda expression is a single-line representation of an
anonymous class that contains a single method.


Questions
What is the difference between an abstract class and an interface?
How would you determine whether to use an abstract class or an interface?
Why can’t a class be declared both final and abstract?
What is the value of designing a codebase around the use of interfaces?
What are anonymous classes used for?
What is a closure?
What is a lambda expression?


Exceptions
Exceptions are special objects that are thrown whenever an error interrupts the
normal execution of code. All exceptions are descendents of the Throwable
class and are divided into two categories: unchecked exceptions and checked
exceptions.



Yüklə 0,53 Mb.

Dostları ilə paylaş:
1   ...   8   9   10   11   12   13   14   15   ...   47




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin