Chapter 6 - Class Design Flashcards

(24 cards)

1
Q

Class optional modifiers

A

final
abstract
sealed
non-sealed
static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can you explicitly inherit Object class in Java

A

Yes, you can explicitly inherit Object in Java, but it’s completely redundant since every class already implicitly extends Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How many public classes in one file

A

Almost one public top level.

Meaning it’s not required but when you do there has to be maximum 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Class access modifiers for top level class

Note: nested classes can have more

A

public
{package}

protected is only for members
private not allowed because when you do other classes won’t be able to use it so what’s the point.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Hiding a variable

A

When you declare a variable in subclass that is also present in super class

You still have 2 copies in subclass but subclass takes precedence when referred. user super operator to refer to the super class members and this operator to refer to the current class (not object)

Also reference type being a super class type will always executes and refer to super class members

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Default constructor

A

Java adds it during compile time when no other constructors are coded irrespective of signature

public Animal(){
super();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Are fields polymorphic when you override variables?

A

No

Only methods are polymorphic and evaluated at runtime. Variables are only hidden and accessed based on super/this/reference type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

this() or super() placement in overloaded constructors

A

If used it should be the only one and first statement

Note: comment lines if placed before are ignored

Note: if neither used compiler puts super() [no argument constructor] automatically in 1st line of the overloaded constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Subclass not having any constructor but super class has overloaded constructor with arguments but not an explicit no argument constructor

A

Doesn’t compile

Since Java inserts default constructor in subclass when not declared it expects a similar constructor from super class either created by default or an explicit constructor coded

Solution: you should use constructor in subclass that calls super(arguments)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

super() always refers to the most direct parent

A

It’s transitive. A super class can use super() to call it’s immediate super class type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

final instance variable assignment

A

It has to be set exactly once during initialization process that includes declare statements, initializer and constructors

Note: static final variables have two places only. Declaration or static initializer

Note: unlike local final variables which are not required to be initialized before they are first used, instance variables need to be assigned during instantiating

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Static variable initialization during class loading

A

1) starts with higher most super class and wind down to its inner most subclass

2) in each class execute static variable declaration and initializers in the order they appear

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Instance variable order

A

1) initialize the super class by executing variable declaration and initializers in the order they appear then executes given constructors in super class
2) wind down to subclass and do the same step 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Instance initialization order

A

1) class initialization
2) starts with constructor flow from subclass to the superclass. Once you start from super class execute instance variable and initializers in the order they appear
3) continue step 2 in construction chain from highest super class to lowest subclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Instance creation with respect to static

A

1) start from super class to subclass and initialize both static variables and initializers in the order they appear
2) repeat step 1 for each class
3) follow constructor chain flow from subclass all the way out to the highest super class. You will end up working from super class. Initialize instance variables and instance initializers in the order they appear followed by constructor method execution
4) repeat step 2 for each class when chain flow comes to the current class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Method overriding rules

A

1) should have same signature
2) should have at least same access or more access modifier
3) child method should not introduce new checked exception or have a subtype checked exception
4) return type must be same or subtype

Note: Regd 3, it can have any number of unchecked exceptions

Note: if the parent method is a private method, the child method with same signature is not considered as overridden. In this case above rules don’t apply. You can freely use any return type or checked exceptions

17
Q

Hiding static method

A

They are bound to be class not an instance

All 4 rules apply in addition to new rule

5) child method needs to be defined as static too. Else it won’t compile

18
Q

Does static members are inherited

A

Yes they are even though they can be hidden. If not hidden they are treated as inherited

19
Q

public class Bear{
public static void sneeze(){}
public void hibernate(){}
public static void laugh(){}
}

public class Panda extends Bear{
public void sneeze(){}
public static void hibernate(){}
protected static void laugh(){}
}

Which lines doesn’t compile

A

All 3 overriding child methods doesn’t compile

1) child needs to have static
2) can not make it static when you have an instance method in parent class
3) can not have more restrictive access

20
Q

Abstract method rules

A

1) The declaration should end with a semicolon and no body
2) only instance methods can be abstract
3) follows all overridng rules

21
Q

Abstract and final modifier

A

Not allowed

For obvious reasons. Purpose of abstract is to override whereas final is to stop overriding

22
Q

Abstract and private modifiers on method

A

Not allowed

Abstract purpose is to override in another class. If it’s declared as private too then that defeats the purpose

23
Q

Final and private

A

Allowed (albeit redundant)

They both serve similar purpose.
Final is to stop overriding and private is to hide it and no way of overriding

24
Q

Abstract and static modifiers

A

Not allowed

Abstract is to be used on instance methods only