Java Class Design Flashcards
What happens when a member of the class has no access modifier?
It’s default
What is the difference between protected and default access?
Protected members are accessible in the subclass, whereas the default members are not
Polymorphism can be of two forms: dynamic and static.
What is the difference between them?
Dynamic polimorphism: When different forms of a single entity are resolved during runtime
static pol.: When forms of a single entity are resolved at compile time (ex. function overloading, abstract methods)
What are the key points for overriding a method?
- Should have the same argument list types (orcompatible types) as the base version
- should have the same return type (can be a subclass - covariant return types)
- should not have a more restrictive access modifier than the base version, but may have a less restrictive access modifier
- Should not throw new or broader checked exceptions, but may throw fewer or narrower checked exceptions or any unchecked exceptions
What is the signature of the method equals?
public boolean equals(Object obj)
What are the key points of making a class immutable?
- Make the fields final and initialize them in the constructor
- make sure the methods dont change the contents inside those mutable objects
- dont share the references outside the class, ex as a return value from methods in that class
- if you must return a reference, return the deep copy of the object
- provide only getters, in case changes must be made to the contents of the object, create a new immutable object with the necessary changes and return that reference
- declare the class final
What is encapsulation
combining data and the functions operating it as a single unit
What are the four flavors of nested classes in java?
- Static nested class
- Inner class
- Local inner class
- anonymous inner class
Where can you defined a static nested class(or interface)?
you can define a class ( or interface) as a static member inside another class (or interface)
the combinations:
class Outer { // an outer class has a static nested class static class Inner {} }
interface Outer { // an outer interface has a static nested class static class Inner {} } class Outer { // an outer class has a static nested interface static interface Inner {} }
interface Outer { // an outer interface has a static nested interface static interface Inner {}
}
Where can you define a inner class?
You can define a class (or interface) as a non static member inside another class
class Outer { // an outer class has an inner class class Inner {} } class Outer { // an outer class has an inner interface interface Inner {} }
How to instantiate a static nested class?
OuterClassName.NestedClassName staticNested = new OuterClassName.NestedClassName()
how to instantiate a inner class?
center = this.new Point(x, y);
this.new InnerClass();
Which three kinds of methods can interface have?
Abstract, default and static methods
Whats the difference between abstract classes and interfaces?
Fields: abstract class can have static and non-static fields. Interfaces cannot cannot have non-static fields
constants: abstract class can have both static and non static constants. Interfaces can only have static constants
constructor: you can define a constructor in an abstract class. Cannot defined/declare constructor in an interface
acess specifiers: you can have private and protected/public members in an abstract class. cannot have any private or protected members in an interface
What are default methods?
default methods are instance methods. Inside the default method, this keyword refers to the declaring interface. default methods can call methods from the interface they are enclosed in.