Chapter 2 - Object Orientation Flashcards
To answer all the Object Oriented questions on the exam
Encapsulation
helps hide implementation behind an interface or API.
Encapsulation code has 2 features
- ) Instance variables are kept protected - usually with the private modifier
- ) Getter and setter methods provide access to instance variables
IS-A
Refers to inheritance or implementation
IS-A
is expressed with the keyword extends
HAS-A
means an instance of one class “has a” reference to an instance of another class or another instance of the same class.
Polymorphism means
many forms
A reference variable
is always of a single, unchangeable type, but it can refer to a subtype object.
A single object can be referred to by reference variables of many different types
as long as they are the same type or supertype of the object.
The reference variables type (not the object type)
determines which methods can be called
Polymorphic method invocations
apply only to overridden instance methods
Methods can be overridden or overloaded
constructors can be overloaded but not overridden
Abstract methods
must be overridden by the first concrete (non-abstract) subclass
An overriding method
- must have same arg list
- must have same return type, the return type can be a subclass - known as a covariant return
- must not have a more restrictive access modifier
- my have a less restrictive access modifier
- Must not throw new or broader checked exceptions
- May throw fewer or narrower exceptions or any unchecked exception
final methods
cannot be overridden
Only inherited methods
may be overridden… private methods are NOT inherited
overloading means
reusing a method name, but with different arguments
Overloaded methods
- must have different argument lists
- May have different return types, if arg lists are also different
- May have different access modifiers
- May throw different exceptions
Methods from a superclass
can be overloaded in a subclass
Polymorphism applies to overriding
not to overloading
Object type (not the reference variables type)
determines which overriding method is used at runtime
Reference type determines
which overloaded method will be used at compile time
Two types of reference variable casting
downcasting and upcasting
Downcasting
Reference variables of subtypes must be explicitly cast, resulting in access to the subtypes members with this new reference variable.
Upcasting
can be done explicitly or implicitly.
When you are implementing an interface
you are fulfilling its contract