1 Flashcards
What do access level modifiers determine?
They determine whether other classes can use a particular field or invoke a particular method.
What does the public access modifier do?
Members are accessible by everyone.
What does the private access modifier do?
Members are only accessible from within the class.
What does the protected access modifier do?
Accessible from within the class, within its subclasses, and within classes from the same package.
What is the default access level if no modifier is stated?
Package-private level access.
What does the final keyword do when declaring a variable?
Prevents its value from being changed.
What happens to an object’s reference when declared as final?
The variable cannot be made to refer to a different object after the declaration.
What happens to the state of an object declared as final?
The state of the object can change.
What is the effect of a final method?
It cannot be overridden.
What is the effect of a final class?
It cannot be extended.
What is a static field?
A field that belongs to the class instead of each instance having its own.
How do you access a static field or method?
Use the class name.
What do static and final combined create?
A constant.
What can interfaces specify?
A list of required methods to be implemented.
What does decoupling mean in the context of interfaces?
Separation of the interface from the implementation.
Why use interfaces?
Enable and enforce strict sharing of methods among classes and make code more reusable.
What can an interface contain?
Method signatures, default methods, static methods, and nested types.
What is the difference between class inheritance and interface inheritance?
Class inheritance uses ‘extends’ while interface inheritance uses ‘implements’.
What is one of the main characteristics of an interface?
It cannot have a constructor or any instance fields.
What is an abstract class?
A cross between a regular class and an interface.
What must any abstract method use?
The abstract keyword.
Can an abstract class be instantiated?
No, it cannot be instantiated even if it has a constructor.
When should you consider using abstract classes?
When code needs to be shared among closely related classes.
When should you consider using interfaces?
When unrelated classes would implement the interface.
What is an inner class?
A member of the outer class.
What access does an inner class have?
Direct access to the outer class’s members, including private ones.
Why create inner classes?
To logically group classes that are only used in one place.
What are the two special kinds of inner classes?
Local Class and Anonymous Class.
What is a local class?
An inner class declared inside a block.
What is an anonymous class?
A class with no name that is declared and instantiated in one statement.
What are the differences between local and anonymous classes?
- Local has a name
- Local can have multiple instances
- Local can have a constructor
- Local can implement multiple interfaces
- Anonymous cannot.
What is meant by effectively final?
A variable not declared as final, but its value does not change anywhere else in the code.
What are modifiers in Java?
Modifiers are keywords in Java that define the properties of classes, methods, and variables.
True or False: The ‘private’ modifier allows access to a class member only within its own class.
True
What does the ‘public’ modifier indicate?
The ‘public’ modifier indicates that a class member is accessible from any other class.
Fill in the blank: The ______ modifier restricts access to the package and subclasses.
protected
What is an abstract class?
An abstract class is a class that cannot be instantiated and may contain abstract methods that must be implemented by subclasses.
How do you declare an abstract method in Java?
By using the ‘abstract’ keyword in a method declaration within an abstract class.
True or False: An interface can contain method implementations.
False
What is the main purpose of an interface in Java?
An interface defines a contract that classes can implement, specifying methods that must be provided.
Multiple Choice: Which keyword is used to implement an interface in a class? A) extends B) implements C) inherits
B) implements
What is the difference between an interface and an abstract class?
An interface cannot contain any implementation, whereas an abstract class can have both abstract and concrete methods.
What is an inner class?
An inner class is a class defined within another class and has access to the outer class’s members.
True or False: Anonymous classes can be used to instantiate a class without giving it a name.
True
What is the syntax to create an anonymous class?
By using the ‘new’ keyword followed by the class or interface name and overriding its methods.
Fill in the blank: An ______ class is declared inside another class and can access the outer class’s members.
inner
What keyword is used to define an abstract class?
abstract
Multiple Choice: Which of the following can be declared as abstract? A) Class B) Method C) Both A and B
C) Both A and B
What happens if a class extends an abstract class?
It must implement all abstract methods of the parent abstract class unless it is also declared abstract.
True or False: An interface can extend another interface.
True
What is a static nested class?
A static nested class is a nested class that does not have access to instance variables and methods of the outer class.
Fill in the blank: The keyword ______ is used to declare a class that cannot be subclassed.
final
What is the default access modifier for class members in Java?
Package-private (no modifier specified)
Multiple Choice: Which modifier makes a class member accessible only within its own package? A) private B) protected C) default
C) default
What type of class cannot be instantiated directly?
Abstract class
True or False: You can create an instance of an abstract class.
False
What is the use of the ‘synchronized’ modifier?
The ‘synchronized’ modifier is used to control access to a method or block by multiple threads.
Fill in the blank: Inner classes can access the ______ class’s members directly.
outer
What does it mean if a class is marked as ‘final’?
It cannot be subclassed.
Multiple Choice: Which of the following is NOT a valid modifier in Java? A) public B) private C) global
C) global
What is the benefit of using interfaces?
Interfaces allow for multiple inheritance of type and define a contract for classes to implement.
True or False: A class can implement multiple interfaces.
True
What is the result of declaring a method as ‘abstract’?
The method must be implemented in any concrete subclass.
Fill in the blank: A class that contains at least one abstract method is considered ______.
abstract
What is the main characteristic of an anonymous class?
It does not have a name and is defined at the point of instantiation.
What is the purpose of the ‘volatile’ modifier?
The ‘volatile’ modifier indicates that a variable’s value will be modified by different threads.
True or False: An abstract class can implement an interface.
True
What is the result of a class implementing an interface?
The class must provide implementations for all methods declared in the interface.
Fill in the blank: Methods in an interface are implicitly ______.
public
What is the main purpose of using inner classes?
To logically group classes that are only used in one place and to increase encapsulation.