Chapter2
Un article de Sometimes Kitties Think Too.
Sommaire |
[modifier]
Chapter 2
[modifier]
Encapsulation
+ make instance variable private
+ public accessor methods using set/get
[modifier]
Inheritance
+ every class inherits from Object and so contains the methods
notify, wait, equals,
[modifier]
HAS-A and IS-A
+ "is-a" referes to inheritance
+ "has-a" refers to instance variables
[modifier]
Polymorphism
+ multiple inheritance NOT supported
+ only applies to method invocations
+ no static methods or variables are selected
[modifier]
Overloading/Overriding
+ COMPILER will only alllow methods in reference class to invoke a method on declared object:
Animal c = new Horse()
(reference class) (object class)
c can only call Animal methods, not Horse methods
but if Horse overrides one of Animal's methods, the Horse method will be
invoked rather than the Animal method
+ reference type determines which class' will supply the instance variable
e.g.
c.saddle - will be Animal's saddle, not Horse's saddle
[modifier]
Overriding
1) access modifier cannot be more restrictive than that of what it's overriding.
But it can be LESS restrictive
2) argument list must match that of overriden method
3) return type must be same/subtype of original method
4) can only override methods that are inherited
5) can throw "unchecked" (runtime) exceptions
must not throw broader exceptions
can throw fewer or narrower exceptions
6) cannot override a final, private or static method
+ can invoke superclass method when overriding
e.g. super.methodFromAbove()
+ If a method is overridden but you use a polymorphic (supertype) reference
to refer to the subtype object with the overriding method, the compiler
assumes you're calling the supertype version of the method.
e.g.
class Animal {
void makeNoise() { System.out.println("gentle sound");
}
class Horse extends Animal {
void makeNoise() {System.out.println("Neighhhhh");
}
|
Animal a = new Horse(); a.makeNoise();
----should print------> "Neighhhhh"
[modifier]
Overloading
**1) must change argument list
2) can change return type
3) can change access modifier
4) can declare new/broader checked exception
5) can be declared in same or subclass
+ object type determines which OVERRIDDEN version is called at runtime
e.g. Horse's makeNoise() method will be called at runtime
+ reference type determines which OVERLOADED method is called at compile time
(because the method only sees the method type of the argument parameter)
[modifier]
Static Methods and Overridding
- for non-static methods, the method executed will be dictated by the object type.
- for static methods, the method executed is determined by the reference type.
e.g.
Animal a = new Dog(); a.eats() ---> eats kibble, using Dog method if eats() is non-static
Animal a = new Dog(); a.eats() ---> uses Animal method if eats() is static
Arguments Overload Override
======================================================================================
arguments MUST change cannot change
------------------------------------------------------------------------------
return may change No change (except covariant)
type
------------------------------------------------------------------------------
access can change cannot be more restrictive
------------------------------------------------------------------------------
invocation reference type Object type -
at compile time method choosen is determined by object
that is on the heap at the time
determined at run-time
------------------------------------------------------------------------------
[modifier]
Polymorphism
- When does casting not work?
(inconvertible types error at compile time), when not in same inheritance tree
- upcasting (to supertypes and interfaces) works implicitly (because it's more restrictive)
example:
Dog implements Pet
|
| (extends)
|
V
Beagle
- Beagle doesn't have to override any Pet methods because Dog already should have
[modifier]
Interface Implementation
contracts for implementing methods of interfaces:
1) provide non-abstract implementations for all methods of declared intf
2) follow rules for legal overrides
3) declare no checked exceptions (other than the ones already declared)
4) maintain signature of intf method
5) more than one intf can be implemented (but only one class can be extended)
#1 doesn't apply if implementation class is abstract or if one intf is extending another intf
+ interface cannot implement other interfaces
+ intfs CANNOT extend a class
[modifier]
Legal Return Types
+ covariant returns - a subtype of the signature method return type can be returned
in overridden methods new in JAVA 5
[modifier]
Rules for return values:
1) null can be returned for object reference return types
2) arrays are legal return types
3) a primitive return typed method can have return any primative
that can be implicitly converted to the return type
or that can be explicitly cast
4) nothing can be returned if type is void
5) object reference return type must return any object that can be implicitly cast
(passes IS-A test)
[modifier]
Constructors & Instantiation
+ constructors invoke the constructors of super classes all the way up the hierarchy
[modifier]
Rules for Constructors
1) can use any access modifier
2) name matches that of the class
3) no return type
4) default (NO-ARG) constructor automatically created only if no other constructors exist
5) first line in a constructor is implicit or explicit call to this() or super()
6) only static variables and methods can be inserted into super() calls
7) interfaces have no constructors
8) must supply arguments if a super class constructor takes arguments
class Bar {
Bar(int x) { }
}
class Foo extends Bar {
static int y = 1;
Foo() {
super(y);
}
}
|
[modifier]
Order for Instantiation
1) Instance variables are assigned default values
2) Constructor invoked which invokes superclass constructor(s)
3) Superclass constructors complete
4) Instance variables initialzed as part of declaration (except compile-time constants)
5) Constructor completes
6) Initialization block completes
+ static initialization always happen before the constructor is called.
+ constructors will call the nearest overriden method (in the case of overridden methods)
+ final instance variables need to be explicitly initialized before the constructor
completes or else compiler error
[modifier]
Static Variables & Methods
+ static methods can't call non-static variables
+ static methods cannot be overriden
(although can be redefined)
+ static means it exists once in the entire class and is shared by all instance members
can be call from any class w/o needing to be instantiated
+ static variables cannot be declared in instance methods
[modifier]
Exceptions
+ checked --> represents invalid conditions methods are obliged to handle
+ unchecked --> like a RuntimeException, represents a defect in the program
[modifier]
Questions
1. Do instance variables get overridden when classes are extended?
Yes. Instance variable overrriding follows the regular rules of polymorphism:
reference type determines the origin of the variable.
