Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. P ART 3: A GGREGATION, I NHERITANCE AND P.

Similar presentations


Presentation on theme: "Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. P ART 3: A GGREGATION, I NHERITANCE AND P."— Presentation transcript:

1 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. P ART 3: A GGREGATION, I NHERITANCE AND P OLYMORPHISM 1

2 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. C LASS A BSTRACTION AND E NCAPSULATION Class abstraction means to separate class implementation from the use of the class. The creator of the class provides a description of the class and let the user know how the class can be used. The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user. 2

3 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. P ROBLEM : D ESIGNING THE L OAN C LASS 3

4 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. P ROBLEM : T HE BMI C LASS 4 BMI=weight/height 2 where weights in Kg and heights in m

5 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. O BJECT C OMPOSITION Composition is actually a special case of the aggregation relationship. Aggregation models has-a relationships and represents an ownership relationship between two objects. The owner object is called an aggregating object and its class an aggregating class. The subject object is called an aggregated object and its class an aggregated class. 5

6 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. C LASS R EPRESENTATION An aggregation relationship is usually represented as a data field in the aggregating class. For example, the relationship in Figure 10.6 can be represented as follows: 6

7 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. A GGREGATION OR C OMPOSITION Since aggregation and composition relationships are represented using classes in similar ways, many texts don’t differentiate them and call both compositions. 7

8 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. A GGREGATION B ETWEEN S AME C LASS Aggregation may exist between objects of the same class. For example, a person may have a supervisor. 8 public class Person { // The type for the data is the class itself private Person supervisor;... }

9 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. A GGREGATION B ETWEEN S AME C LASS What happens if a person has several supervisors? 9

10 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. E XAMPLE : T HE C OURSE C LASS 10

11 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. I MMUTABLE O BJECTS AND C LASSES 11 If the contents of an object cannot be changed once the object is created, the object is called an immutable object and its class is called an immutable class. If you delete the set method in the Circle class in the preceding example, the class would be immutable because radius is private and cannot be changed without a set method. A class with all private data fields and without mutators is not necessarily immutable. For example, the following class Student has all private data fields and no mutators, but it is mutable.

12 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. E XAMPLE 12 public class Student { private int id; private BirthDate birthDate; public Student(int ssn, int year, int month, int day) { id = ssn; birthDate = new BirthDate(year, month, day); } public int getId() { return id; } public BirthDate getBirthDate() { return birthDate; } } public class BirthDate { private int year; private int month; private int day; public BirthDate(int newYear, int newMonth, int newDay) { year = newYear; month = newMonth; day = newDay; } public void setYear(int newYear) { year = newYear; } } public class Test { public static void main(String[] args) { Student student = new Student(111223333, 1970, 5, 3); BirthDate date = student.getBirthDate(); date.setYear(2010); // Now the student birth year is changed! } }

13 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. W HAT C LASS IS I MMUTABLE ? 13 For a class to be immutable, it must mark all data fields private and provide no mutator methods and no accessor methods that would return a reference to a mutable data field object.

14 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. I NHERITANCE Defining new classes from existing classes. Inheritance enables you to define a general class (superclass) and later extend it to more specialized classes (subclasses). “is-a” relationship: using a class to model objects of the same type (part time and full time employees). Common attributes and behaviors are defined in the superclass and are inherited into subclasses and subsequent subclasses. Superclass is called parent class or a base class. Subclasses may have more specialized attributes and behaviors and is called child class, extended class or derived class.. 14

15 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. I NHERITANCE TYPES Single inheritance : Subclass is derived from one existing class (superclass). Multiple inheritance: Subclass is derived from more than one superclass Not supported by Java In Java, a class can only extend the definition of one class 15

16 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. S UPERCLASSES AND S UBCLASSES 16 GeoShape color: String printColor( ):void Circle radius : double findArea( ):void Rect length : double width : double findArea( ):void

17 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. E XTENDS KEYWORD Use extends keyword to define that a class is a subclass from another class. public class Circle extends GeoShape {. } GeoShape  superclass Circle  subclass 17

18 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. S OME RULES 1. The private members of the superclass are private to the superclass. 2. The subclass can directly access the public, non ; members of the superclass. 3. The subclass can include additional data and/or method members. 18

19 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. A RE SUPERCLASS ’ S C ONSTRUCTOR I NHERITED ? 19 No. They are not inherited. They are invoked explicitly or implicitly. Explicitly using the super keyword. A constructor is used to construct an instance of a class. Unlike properties and methods, a superclass's constructors are not inherited in the subclass. They can only be invoked from the subclasses' constructors, using the keyword super. If the keyword super is not explicitly used, the superclass's no-arg constructor is automatically invoked.

20 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. S UPERCLASS ’ S C ONSTRUCTOR I S A LWAYS I NVOKED 20 A constructor may invoke an overloaded constructor or its superclass’s constructor. If none of them is invoked explicitly, the compiler puts super() as the first statement in the constructor. For example,

21 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. U SING THE K EYWORD SUPER To call a superclass constructor To call a superclass method To call a superclass attributes 21 The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways:

22 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. CAUTION 22 You must use the keyword super to call the superclass constructor. Invoking a superclass constructor’s name in a subclass causes a syntax error. Java requires that the statement that uses the keyword super appear first in the constructor.

23 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. C ONSTRUCTOR C HAINING 23 public class Staff extends Employee { public Staff () { System.out.println("(3) Faculty's no-arg constructor is invoked"); } class Employee extends Person { public Employee() { System.out.println("(2) Employee's no-arg constructor is invoked"); } } class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); } Class Test{ public static void main(String arg[]){ Staff s1=new Staff(); } Constructing an instance of a class invokes all the superclasses’ constructors along the inheritance chain. This is called constructor chaining.

24 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T RACE E XECUTION 24 public class Staff extends Employee { public Staff () { System.out.println("(3) Faculty's no-arg constructor is invoked"); } class Employee extends Person { public Employee() { System.out.println("(2) Employee's no-arg constructor is invoked"); } } class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); } Class Test{ public static void main(String arg[]){ Staff s1=new Staff(); } 1. Start from the main method

25 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T RACE E XECUTION 25 public class Staff extends Employee { public Staff () { System.out.println("(3) Faculty's no-arg constructor is invoked"); } class Employee extends Person { public Employee() { System.out.println("(2) Employee's no-arg constructor is invoked"); } } class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); } Class Test{ public static void main(String arg[]){ Staff s1=new Staff(); } 2. Invoke Faculty constructor

26 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T RACE E XECUTION 26 public class Staff extends Employee { public Staff () { System.out.println("(3) Faculty's no-arg constructor is invoked"); } class Employee extends Person { public Employee() { System.out.println("(2) Employee's no-arg constructor is invoked"); } } class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); } Class Test{ public static void main(String arg[]){ Staff s1=new Staff(); } 3. Invoke Employee’s no- arg constructor

27 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T RACE E XECUTION 27 public class Staff extends Employee { public Staff () { System.out.println("(3) Faculty's no-arg constructor is invoked"); } class Employee extends Person { public Employee() { System.out.println("(2) Employee's no-arg constructor is invoked"); } } class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); } Class Test{ public static void main(String arg[]){ Staff s1=new Staff(); } 4. Invoke Person no-arg construct

28 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T RACE E XECUTION 28 public class Staff extends Employee { public Staff () { System.out.println("(3) Faculty's no-arg constructor is invoked"); } class Employee extends Person { public Employee() { System.out.println("(2) Employee's no-arg constructor is invoked"); } } class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); } Class Test{ public static void main(String arg[]){ Staff s1=new Staff(); } 5. Print

29 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T RACE E XECUTION 29 public class Staff extends Employee { public Staff () { System.out.println("(3) Faculty's no-arg constructor is invoked"); } class Employee extends Person { public Employee() { System.out.println("(2) Employee's no-arg constructor is invoked"); } } class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); } Class Test{ public static void main(String arg[]){ Staff s1=new Staff(); } 6. Execute println

30 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T RACE E XECUTION 30 public class Staff extends Employee { public Staff () { System.out.println("(3) Faculty's no-arg constructor is invoked"); } class Employee extends Person { public Employee() { System.out.println("(2) Employee's no-arg constructor is invoked"); } } class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); } Class Test{ public static void main(String arg[]){ Staff s1=new Staff(); } 7. Execute println

31 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T RACE OUTPUT 31 (1) Person's no-arg constructor is invoked (2) Employee's no-arg constructor is invoked (3) Faculty's no-arg constructor is invoked

32 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. UML CLASS DIAGRAM Can you draw the UML class diagram for the previous example? 32

33 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. E XAMPLE ON THE I MPACT OF A S UPERCLASS WITHOUT NO - ARG C ONSTRUCTOR 33 public class Apple extends Fruit { } class Fruit { public Fruit(String name) { System.out.println("Fruit's constructor is invoked"); } Find out the errors in the program:

34 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. E XAMPLE ON THE I MPACT OF A S UPERCLASS WITHOUT NO - ARG C ONSTRUCTOR 34 public class Apple extends Fruit { Apple(){ super(“”); } class Fruit { public Fruit(String name) { System.out.println("Fruit's constructor is invoked"); } solution1:

35 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. E XAMPLE ON THE I MPACT OF A S UPERCLASS WITHOUT NO - ARG C ONSTRUCTOR 35 public class Apple extends Fruit { } class Fruit { public Fruit(){} public Fruit(String name) { System.out.println("Fruit's constructor is invoked"); } solution2: By default calls Fruit no-arg constructor

36 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. D EFINING A S UBCLASS A subclass inherits from a superclass. You can also: F Add new properties F Add new methods F Override the methods of the superclass 36

37 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. O VERRIDING M ETHODS IN THE S UPERCLASS 37 A subclass inherits methods from a superclass. Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass. This is referred to as method overriding. Class Shape{ String color; String toString(){ return “this color=“ + color;} } public class Circle extends Shape{ double radius; String toString(){ return super.toString() + "\nradius is " + radius; }

38 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. NOTE 38 Overriding methods can not narrow the visibility of overridden methods but in contrary it can wider it. An instance method can be overridden only if it is accessible. Thus a private method cannot be overridden, because it is not accessible outside its own class. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. Like an instance method, a static method can be inherited. However, a static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.

39 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. O VERRIDING VS. O VERLOADING 39

40 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T HE O BJECT C LASS AND I TS M ETHODS Every class in Java is descended from the java.lang.Object class. If no inheritance is specified when a class is defined, the superclass of the class is Object. 40

41 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T HE TO S TRING () METHOD IN O BJECT The toString() method returns a string representation of the object. The default implementation returns a string consisting of a class name of which the object is an instance, the at sign (@), and a number representing this object. 41 Loan n1 = new Loan(); System.out.println(n1.toString()); The code displays something like Loan@15037e5. This message is not very helpful or informative. Usually you should override the toString method so that it returns a information string about the object.

42 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. P OLYMORPHISM, D YNAMIC B INDING AND G ENERIC P ROGRAMMING public class PolymorphismDemo { public static void main(String[] args) { m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); } public static void m(Object x) { System.out.println(x.toString()); } class GraduateStudent extends Student { } class Student extends Person { public String toString() { return "Student"; } class Person extends Object { public String toString() { return "Person"; } Method m takes a parameter of the Object type. You can invoke it with any object.

43 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. P OLYMORPHISM, D YNAMIC B INDING AND G ENERIC P ROGRAMMING In the previous example: When the method m(Object x) is executed, the argument x’s toString method is invoked. x may be an instance of GraduateStudent, Student, Person, or Object. Classes GraduateStudent, Student, Person, and Object have their own implementation of the toString method. Which implementation is used will be determined dynamically by the Java Virtual Machine at runtime. This capability is known as dynamic binding. 43 An object of a subtype can be used wherever its supertype value is required. This feature is known as polymorphism. (declaring a reference variable of super type and assigns its value to a subtype object) Example: GeoShape g1=new Circle( );

44 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. D YNAMIC B INDING Dynamic binding works as follows: Suppose an object o is an instance of classes C 1, C 2,..., C n-1, and C n, where C 1 is a subclass of C 2, C 2 is a subclass of C 3,..., and C n-1 is a subclass of C n. That is, C n is the most general class, and C 1 is the most specific class. In Java, C n is the Object class. If o invokes a method p, the JVM searches the implementation for the method p in C 1, C 2,..., C n-1 and C n, in this order, until it is found. Once an implementation is found, the search stops and the first- found implementation is invoked. 44

45 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. M ETHOD M ATCHING VS. B INDING Matching a method signature and binding a method implementation are two issues. The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compilation time. A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the implementation of the method at runtime. 45

46 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. G ENERIC P ROGRAMMING Polymorphism allows methods to be used generically for a wide range of object arguments. This is known as generic programming. If a method’s parameter type is a superclass (e.g., Object), you may pass an object to this method of any of the parameter’s subclasses (e.g., Student or String). When an object (e.g., a Student object or a String object) is used in the method, the particular implementation of the method of the object that is invoked (e.g., toString) is determined dynamically. 46 public class PolymorphismDemo { public static void main(String[] args) { m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); } public static void m(Object x) { System.out.println(x.toString()); } class GraduateStudent extends Student { } class Student extends Person { public String toString() { return "Student"; } class Person extends Object { public String toString() { return "Person"; }

47 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. C ASTING O BJECTS You have already used the casting operator to convert variables of one primitive type to another. Casting can also be used to convert an object of one class type to another within an inheritance hierarchy. In the preceding section, the statement m(new Student()); assigns the object new Student() to a parameter of the Object type. This statement is equivalent to: Object o = new Student(); // Implicit casting m(o); 47 The statement Object o = new Student(), known as implicit casting, is legal because an instance of Student is automatically an instance of Object.

48 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. C ASTING O BJECTS class Test{ public static void main(String a[ ]){ Object s1=new Std(); m(s1); } public static void m(Object o){ ((Std)o).printInfo(); } 48 Here you need explicit casting because Object class does not contain printInfo method so during method matching process compilers needs to find printInfo method in the declared class or the casting one, then during method binding JVM finds the implementation of printInfo and dinamically executes it.

49 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. W HY C ASTING I S N ECESSARY ? Suppose you want to assign the object reference o to a variable of the Student type using the following statement: Student b = o; A compile error would occur. Why does the statement Object o = new Student() work and the statement Student b = o doesn’t? This is because a Student object is always an instance of Object, but an Object is not necessarily an instance of Student. Even though you can see that o is really a Student object, the compiler is not so clever to know it. To tell the compiler that o is a Student object, use an explicit casting. The syntax is similar to the one used for casting among primitive data types. Enclose the target object type in parentheses and place it before the object to be cast, as follows: Student b = (Student)o; // Explicit casting 49

50 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T HE INSTANCEOF O PERATOR Use the instanceof operator to test whether an object is an instance of a class: Object myObject = new Circle();... // Some lines of code /** Perform casting if myObject is an instance of Circle */ if (myObject instanceof Circle) { System.out.println("The circle diameter is " + ((Circle)myObject).getDiameter());... } 50

51 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. E XAMPLE : D EMONSTRATING P OLYMORPHISM AND C ASTING GeoShape System Example. 51

52 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T HE EQUALS M ETHOD The equals() method compares the contents of two objects. The default implementation of the equals method in the Object class is as follows: 52 public boolean equals(Object obj) { return (this == obj); } For example, the equals method is overridden in the Circle class. public boolean equals(Object o) { if (o instanceof Circle) { return radius == ((Circle)o).radius; } else return false; }

53 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. NOTE 53 The == comparison operator is used for comparing two primitive data type values or for determining whether two objects have the same references. The equals method is intended to test whether two objects have the same contents, provided that the method is modified in the defining class of the objects. The == operator is stronger than the equals method, in that the == operator checks whether the two reference variables refer to the same object.

54 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T HE A RRAY L IST C LASS You can create an array to store objects. But the array’s size is fixed once the array is created. Java provides the ArrayList class that can be used to store an unlimited number of objects. 54

55 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. G ENERIC T YPE ArrayList is known as a generic class with a generic type E. You can specify a concrete type to replace E when creating an ArrayList. For example, the following statement creates an ArrayList and assigns its reference to variable cities. This ArrayList object can be used to store strings. 55 ArrayList cities = new ArrayList ();

56 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T HE M Y S TACK C LASSES – SELF READING A stack to hold objects. 56

57 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T HE PROTECTED M ODIFIER The protected modifier can be applied on data and methods in a class. A protected data or a protected method in a public class can be accessed by any class in the same package or its subclasses, even if the subclasses are in a different package. private, default, protected, public 57

58 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. V ISIBILITY M ODIFIERS 58

59 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. A S UBCLASS C ANNOT W EAKEN THE A CCESSIBILITY 59 A subclass may override a protected method in its superclass and change its visibility to public. However, a subclass cannot weaken the accessibility of a method defined in the superclass. For example, if a method is defined as public in the superclass, it must be defined as public in the subclass.

60 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. T HE FINAL M ODIFIER The final class cannot be extended: final class Math {... } The final variable is a constant: final static double PI = 3.14159; The final method cannot be overridden by its subclasses. 60

61 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. NOTE 61 The modifiers are used on classes and class members (data and methods), except that the final modifier can also be used on local variables in a method. A final local variable is a constant inside a method.


Download ppt "Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. P ART 3: A GGREGATION, I NHERITANCE AND P."

Similar presentations


Ads by Google