Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..

Similar presentations


Presentation on theme: "Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class….."— Presentation transcript:

1 Chapter 5 Objects and Classes Inheritance

2 Solution Assignments 3 & 4 Review in class…..

3 Extending A Class

4 Superclass and Subclass You can only extend one class (Java does not allow multiple inheritence) Whenever you want to access a instance field of your super class inside of your sub class you have to use the keyword super

5 NOTE Recall that the this keyword has two meanings: to denote a reference to the implicit parameter and to call another constructor of the same class. Likewise, the super keyword has two meanings: to invoke a superclass method and to invoke a superclass constructor. When used to invoke constructors, the this and super keywords are closely related. – The constructor calls can only occur as the first statement in another constructor. The construction parameters are either passed to another constructor of the same class (this) or a constructorof the superclass (super).

6 Dynamic Binding The fact that an object variable can refer to multiple actual types is called polymorphism. Automatically selecting the appropriate method at runtime is called dynamic binding. See eclipse example

7 Final Methods

8 Final Classes

9

10

11

12

13

14 Summary of Access Modifiers Here is a summary of the four access modifiers in Java that control visibility: – 1. Visible to the class only (private). – 2. Visible to the world (public). – 3. Visible to the package and all subclasses (protected). – 4. Visible to the package—the (unfortunate) default. No modifiers are needed.

15

16 Object Class Of course, a variable of type Object is only useful as a generic holder for arbitrary values. To do anything specific with the value, you need to have some knowledge about the original type and then apply a cast: Employee e = (Employee) obj;

17 Cosmic Superclass Methods equals() – tests whether one object is considered equal to another. hashcode() – is an integer that is derived from an object – must be redefined is equals() is redefined toString() – that returns a string representing the value of this object – Most (but not all) toString methods follow this format: the name of the class, followed by the field values enclosed in square brackets

18 equals() boolean equals(Object other) – All objects have both identity (the object's location in memory) and state (the object's data members and their values). The == operator always compares identity. The default implementation of equals compares identity as well. instanceof tests whether the thing on the LHS is an instance of the type on the RHS or some subtype. Class getClass() ==... tests whether the types are identical.

19 String toString() Can be any descriptive string of the object Common format: “ClassName[field1=val1,…, fieldn=valn]” (see EqualsTest.java) in sample code

20 Generic Array List Dynamically add/remove array elements No over-allocation No manual reallocation

21 Object Wrappers and Autoboxing Wrapper classes for primitives – to convert primitives types into Objects – int translates to Integer class – long translates to Long class – etc. – final (cannot subclass) Autoboxing: – list.add(3) translates to list.add(new Integer(3)) Conversely, int n = list.get(i); – int n translates list.get(i).intValue();

22 Methods With Variable Number of Parameters

23 Varargs Method’s Cont public static double max(double... values) { double largest = Double.MIN_VALUE; for (double v : values) if (v > largest) largest = v; return largest; } Si mply call the function like this: double m = max(3.1, 40.4, -5); The compiler passes a new double[] { 3.1, 40.4, -5 } to the max function.


Download ppt "Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class….."

Similar presentations


Ads by Google