Object Orientation Flashcards Preview

JAVA OCA7 > Object Orientation > Flashcards

Flashcards in Object Orientation Deck (42)
Loading flashcards...
1

What is encapsulation?

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.

2

What is IS-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.

3

What is HAS-A?

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

4

What are the fundamental concepts of OOP?

• Encapsulation
• Inheritance
• Polymorphism
• Abstraction

5

What is Inheritance?

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.

6

Which classes are subclasses of Object?

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

7

What is Polymorphism?

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.

8

What determines which methods can be called?

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

9

Polymorphic method invocations apply only to..

overridden instance methods.

10

Can constructors be overriden?

No. Can only be overloaded.

11

What are the 6 rules for overriding?

❑ 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.

12

Can final methods be overridden?

No.

13

Can private methods be overridden?

No.

14

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

super.overriddenMethodName()

15

What are the 4 rules for overloading?

❑ 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

16

What determines which version of "overriden" method is called?

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

17

What determines which version of "overloaded" method is called?

Reference type.

18

What is downcasting?

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

What is upcasting?

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

How many interfaces can a class implement?

Many.

21

Can overloaded change return type?

Yes.

22

Can overriden change return type?

No, except in the case of covariant returns.

23

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

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

24

Methods with an object reference return type can return..

a subtype.

25

Methods with an interface return type can return..

any implementer.

26

Does each class have a constructor?

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

27

How are the constructors executed?

❑ 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

Can constructors use access modifiers?

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

29

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

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

30

What is a default consturctor?

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