Flashcards in Object Orientation Deck (42)
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.
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.
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.
What are the fundamental concepts of OOP?
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.
Which classes are subclasses of Object?
All classes (except class Object) are subclasses of type Object, and therefore they inherit Object's methods.
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.
What determines which methods can be called?
The reference variable's type - not the object's type.
Polymorphic method invocations apply only to..
overridden instance methods.
Can constructors be overriden?
No. Can only be overloaded.
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.
Can final methods be overridden?
Can private methods be overridden?
How can a subclass call superclass version of an overriden method?
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
What determines which version of "overriden" method is called?
Object type (not the reference variable's type).
What determines which version of "overloaded" method is called?
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
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).
How many interfaces can a class implement?
Can overloaded change return type?
Can overriden change return type?
No, except in the case of covariant returns.
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.
Methods with an object reference return type can return..
Methods with an interface return type can return..
Does each class have a constructor?
Yes. Every class, even an abstract class, has at least one constructor.
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.
Can constructors use access modifiers?
Yes, constructors can use any access modifier (even private!).
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.