Object Orientation Flashcards

1
Q

What is encapsulation?

A

Encapsulation helps hide implementation behind an interface. It has two features;

  • Instance variables are kept protected (usually with the private modifier).
  • Getter and setter methods provide access to instance variables.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is IS-A?

A

It refers to inheritance or implementation, expressed with the keyword extends or implements.”inherits from,” and “is a subtype of” are all equivalent expressions.

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

What is HAS-A?

A

An instance of one class “has a” reference to an instance of another class or another instance of the same class.

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

What are the fundamental concepts of OOP?

A
  • Encapsulation
    • Inheritance
    • Polymorphism
    • Abstraction
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Inheritance?

A
Inheritance allows a class to be a subclass of a superclass and thereby inherit public and protected variables and methods of the superclass. ıt is a key concept that underlies IS-A, polymorphism, overriding,
overloading, and casting.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which classes are subclasses of Object?

A

All classes (except class Object) are subclasses of type Object, and therefore they inherit Object’s methods.

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

What is Polymorphism?

A

Polymorphism means “many forms.” A reference variable is always of a single, unchangeable type, but it can refer to a subtype object. It applies to overriding, not to overloading.

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

What determines which methods can be called?

A

The reference variable’s type - not the object’s type.

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

Polymorphic method invocations apply only to..

A

overridden instance methods.

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

Can constructors be overriden?

A

No. Can only be overloaded.

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

What are the 6 rules for overriding?

A

❑ Must have the same argument list
❑ Must have the same return type, except that, as of Java 5, the return type can be a subclass, and this is known as a covariant return
❑ Must not have a more restrictive access modifier
❑ May have a less restrictive access modifier
❑ Must not throw new or broader checked exceptions
❑ May throw fewer or narrower checked exceptions, or any unchecked exception.

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

Can final methods be overridden?

A

No.

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

Can private methods be overridden?

A

No.

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

How can a subclass call superclass version of an overriden method?

A

super.overriddenMethodName()

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

What are the 4 rules for overloading?

A

❑ Must have different argument lists
❑ May have different return types, if argument lists are also different
❑ May have different access modifiers
❑ May throw different exceptions

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

What determines which version of “overriden” method is called?

A

Object type (not the reference variable’s type).

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

What determines which version of “overloaded” method is called?

A

Reference type.

18
Q

What is downcasting?

A
Downcasting or type refinement is the act of casting a reference of a base class to one of its derived classes. You must make an explicit cast to do this.
Animal animal = new Animal();
Dog d = (Dog) animal;

However, if the compiler knows with certainty that the cast could not possibly work, compilation will fail.
String s = (String) animal; // animal can’t EVER be a String

19
Q

What is upcasting?

A

Casting up the inheritance tree to a more general

type. It works implicitly (that is, you don’t have to type in the cast).

20
Q

How many interfaces can a class implement?

A

Many.

21
Q

Can overloaded change return type?

A

Yes.

22
Q

Can overriden change return type?

A

No, except in the case of covariant returns.

23
Q

Can a method with a primitive return type return a type other than the one in its signature?

A

For methods with primitive return types, any value that can be implicitly converted to the return type can be returned.

24
Q

Methods with an object reference return type can return..

A

a subtype.

25
Q

Methods with an interface return type can return..

A

any implementer.

26
Q

Does each class have a constructor?

A

Yes. Every class, even an abstract class, has at least one constructor.

27
Q

How are the constructors executed?

A
❑ The constructor calls its superclass constructor, which calls its superclass constructor, and so on all the way up to the Object constructor.
❑ The Object constructor executes and then returns to the calling constructor, which runs to completion and then returns to its calling constructor, and so on back down to the completion of the constructor of the actual instance being created.
28
Q

Can constructors use access modifiers?

A

Yes, constructors can use any access modifier (even private!).

29
Q

What happens if you don’t create any constructors for your class?

A

The compiler will create a default constructor if you don’t create any constructors in your class.

30
Q

What is a default consturctor?

A

The default constructor is the no-argument constructor with a no-arg call to super() automatically generated unless you define another constructor.

31
Q

What is the first statement of a consturctor?

A

The first statement of every constructor must be a call either to this() (an overloaded constructor) or to super(). The compiler will add a call to super() unless you have already put in a call to this() or super().

32
Q

When are the instance members accessible?

A

Only after the super constructor runs.

33
Q

When are the constructor for an abstract class is called?

A

When a concrete subclass is instantiated.

34
Q

Do interfaces have constructors?

A

No.

35
Q

What happens if super class does not have a no-arg constructor?

A

You must create a constructor and insert a call to super() with arguments matching those of the superclass constructor.

36
Q

Can constructors be inherited?

A

Constructors are never inherited; thus they cannot be overridden.

37
Q

Can a call to super and this be in the same constructor?

A

No, you can have one or the other, but never both. Constructors can call constructors, and so on, but sooner or later one of them better call super() or the stack will explode.

38
Q

When are the “static init” blocks are run?

A

Runs once, when the class is first loaded. Multiple blocks run from the top down.

39
Q

When are the “init” blocks are run?

A

Runs for every new instance, right after all the super constructors have run. Multiple blocks run from the top down.

40
Q

How many copies are there for a static variable?

A

Only one.

41
Q

Can we access a static variable using instance variable?

A

Yes but d.doStuff();
becomes
Dog.doStuff();

42
Q

Can static methods be overriden?

A

No. can be overloaded.