Содержание
- 2. Agenda Inheritance Fields/Methods in Extended Classes Constructors in extended classes Inherited object construction Overloading and Overriding
- 3. Agenda Type conversion protected members Object: the ultimate superclass
- 4. Inheritance Inheritance: you can create new classes that are built on existing classes. Through the way
- 5. Inheritance "is a" relationship Inheritance "has a" relationship Composition, having other objects as members Example Employee
- 6. Definitions A class that is derived from another class is called a subclass (also a derived
- 7. Definitions Excepting Object, which has no superclass, every class has one and only one direct superclass
- 8. Definitions Every class is an extended (inherited) class, whether or not it’s declared to be. If
- 9. Inheritance modifier(s) class ClassName extends ExistingClassName {}
- 10. sample classes Superclass public class Person{ private String name; public Person ( ) { name =
- 11. Inheritance Class hierarchy of previous example Object Person Student
- 12. Fields/Methods in Extended Classes An object of an extended class contains two sets of variables and
- 13. Constructors in extended classes A constructor of the extended class can invoke one of the superclass’s
- 14. Three phases of an object’s construction When an object is created, memory is allocated for all
- 15. To Illustrate the Construction Order. . . Y objectY = new Y(); Step what happens xOri
- 16. Overloading and Overriding Methods Overloading: providing more than one method with the same name but different
- 17. Accessibility and Overriding a method can be overridden only if it’s accessible in the subclass private
- 18. Concrete1 c1 = new Concrete1(); c1.show( ); Output? Base.pri() Base.pac() Concrete1.pro() Concrete1.pub() Sample classes package P1;
- 19. Sample classes (cont.) package P1; import P2.Concrete1; public class Concrete2 extends Concrete1 { public void pri(
- 20. Hiding fields Fields cannot be overridden, they can only be hidden If a field is declared
- 21. Polymorphism Java allows us to treat an object of a subclass as an object of its
- 22. Polymorphism Late binding or dynamic binding (run-time binding): Method to be executed is determined at execution
- 23. Example Classes class SuperShow { public String str = “SuperStr”; public void show( ) { System.out.println(“Super.show:”
- 24. Type compatibility Java is a strongly typed language. Compatibility when you assign the value of an
- 25. Type conversion (1) The types higher up the type hierarchy are said to be wider, or
- 26. Type conversion (2) Explicit type casting: a type name within parentheses, before an expression for widening
- 27. Type conversion (3) Type testing: you can test an object’s actual class by using the instanceof
- 28. protected members To allow subclass methods to access a superclass field, define it protected. But be
- 29. What protected really means Precisely, a protected member is accessible within the class itself, within code
- 30. Protected Example package A; public class Employee { protected Date hireDay; . . . } package
- 31. Object: the ultimate superclass The object class is the ultimate ancestor: every class in Java extends
- 32. The class Object: Equivalent Definition of a Class public class Clock { //Declare instance variables as
- 33. final Methods and Classes Declaring variables final Indicates they cannot be modified after declaration Must be
- 35. Скачать презентацию