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