Object Orientation Flashcards
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?
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
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?
No.
Can private methods be overridden?
No.
How can a subclass call superclass version of an overriden method?
super.overriddenMethodName()
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?
Reference type.
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?
Many.
Can overloaded change return type?
Yes.
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..
a subtype.