Lec03 - Encapsulation & Subclassing.pdf - Unauthorized...

This preview shows page 1 - 5 out of 28 pages.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Encapsulation and SubclassingPeter Muturi ([email protected]ฺacฺke) has a non-transferable license touse this Student GuideฺUnauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ
Java SE 7 Programming 3 - 2Copyright © 2012, Oracle and/or its affiliates. All rights reserved.ObjectivesAfter completing this lesson, you should be able to do the following:Use encapsulation in Java class designModel business problems using Java classesMake classes immutableCreate and use Java subclassesOverload methodsUse variable argument methodsPeter Muturi ([email protected]ฺacฺke) has a non-transferable license touse this Student GuideฺUnauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ
The termencapsulationis defined by the Java Technology Reference Glossary as follows:“The localization of knowledge within a module. Because objects encapsulate data and implementation, the user of an object can view the object as a black box that provides services. Instance variables and methods can be added, deleted, or changed, but if the services provided by the object remain the same, the code that uses the object can continue to use it without being rewritten.”An analogy for encapsulation is the steering wheel of a car. When you drive a car, whether it is your car, a friend's car, or a rental car, you probably never worry about how the steering wheel implements a right-turn or left-turn function. The steering wheel could be connected to the front wheels in a number of ways: ball and socket, rack and pinion, or some exotic set of servo mechanisms.As long as the car steers properly when you turn the wheel, the steering wheel encapsulates the functions you needyou do not have to think about the implementation.Java SE 7 Programming 3 - 3Copyright © 2012, Oracle and/or its affiliates. All rights reserved.EncapsulationThe term encapsulationmeans to enclose in a capsule, or to wrap something around an object to cover it. In object-oriented programming, encapsulation covers, or wraps, the internal workings of a Java object.Data variables, or fields, are hidden from the user of the object.Methods, the functions in Java, provide an explicit service to the user of the object but hide the implementation.As long as the services do not change, the implementation can be modified without impacting the user.Peter Muturi ([email protected]ฺacฺke) has a non-transferable license touse this Student GuideฺUnauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ
A Simple ModelSuppose that you are asked to create a model of a typical employee. What data might you want to represent in an object that describes an employee?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture