Class design Flashcards
Inheritance
Inheritance is the process by which the new child subclass automatically includes any public or protected primitives, objects, or methods defned in the parent class.
Java supports single inheritance, by which a class may inherit from only one direct parent class.
Java doesn’t support multiple inheritance!
Java allows only one public class per file
true
public access modifer
The public access modifer applied to a class indicates that it can be referenced and used in any class.
default package private modifer
The default package private modifer, which is the lack of any access modifer, indicates the class can be accessed only by a subclass or class within the same package.
java.lang.Object
java.lang.Object is the only class that doesn’t have any parent classes.
The first statement of every constructor is a call to another constructor within the class using this(), or a call to a constructor in the direct parent class using super().
true
The super() call may not be used after the first statement of the constructor.
true
If no super() call is declared in a constructor, Java will insert a no-argument super()
as the first statement of the constructor.
true
If the parent doesn’t have a no-argument constructor and the child doesn’t define any constructors, the compiler will throw an error and try to insert a default no-argument constructor into the child class.
true
If the parent doesn’t have a no-argument constructor, the compiler requires an explicit
call to a parent constructor in each child constructor
true
Hiding Static Methods
A hidden method occurs when a child class defnes a static method with the same name and signature as a static method defned in a parent class.
Overriding vs. Hiding Methods
Unlike overriding a method, in which a child method
replaces the parent method in calls defned in both the parent and child, hidden methods only replace parent methods in the calls defned in the child class.
final methods cannot be overridden.
true
Hiding Variables
When you hide a variable, you defne a variable with the same name as a variable in a parent class.
This creates two copies of the variable within an instance of the child class: one instance defned for the parent reference and another defned for the child reference.
abstract class
An abstract class is a class that is marked with the abstract keyword and cannot be instantiated
abstract method
An abstract method is a method marked with the abstract keyword defned in an abstract class, for which no implementation is provided in the class in which it is declared.
an abstract method may only be defned in an abstract class.
+
concrete class
A concrete class is the frst nonabstract subclass that extends an abstract class and is required to implement all inherited abstract methods.
A class may implement multiple interfaces, each separated by a comma
+
interface
An interface is an abstract data type that defnes a list of abstract public methods that any class implementing the interface must provide.
a class can implement an interface, a class cannot extend an interface.
but
an interface can extend another interface, an interface cannot implement another interface.
Interface variables
Interface variables are assumed to be public, static, and final
The value of an interface variable
The value of an interface variable must be set when it is declared since it is marked as final.
Default Interface Methods
A default method is a method defned within an
interface with the default keyword in which a method body is provided
Static Interface Methods
A static method defned in an interface is not inherited in any classes that implement the interface.
Any field in an interface is implicitly public, static, and final, whether these keywords are specified or not.
+
varialbles are hidden, methods are overriden.
true
a interface can have static method but method must have body in that case
+
Overriding a Method
You can override a method a method by declaring a new method with the signature and return type as the method in the parent class.
When you override a method, you may reference the parent version of the method using the super keyword.
Hiding Variables
When you hide a variable, you defne a variable with the same name as a variable in a parent class. This creates two copies of the variable within an instance of the child class:
one instance defned for the parent reference and another defned for the child reference.
Fields defined in an interface are ALWAYS considered as public, static, and final. Even if you don’t explicitly define them as such.
+
An interface method cannot be default and static at the same time because a default method is always an instance method.
+
Trying to override a static method with a non-static method (and vice-versa) in a class will result in a compilation error.
+