Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming With Java ICS201 University Of Ha’il1 ICS 201 Introduction to Computer Science Inheritance.

Similar presentations


Presentation on theme: "Programming With Java ICS201 University Of Ha’il1 ICS 201 Introduction to Computer Science Inheritance."— Presentation transcript:

1 Programming With Java ICS201 University Of Ha’il1 ICS 201 Introduction to Computer Science Inheritance

2 Programming With Java ICS201 Inheritance Hierarchy

3 Programming With Java ICS201 Encapsulation  Data Hiding  Abstraction  Security ؟ 3

4 Programming With Java ICS201  Encapsulation allows the programmer to group data and the methods that operate on them together in one place, and to hide details from the user. In Java, hiding details is done by marking them private Encapsulation (data hiding) withDraw Deposit Transfer Print AccountNo AccountValue NameAddress Change Address withDraw any value 4

5 Programming With Java ICS201 Encapsulation Person Vending Machine Buy Pepsi Sell (1 SR, Pepsi) Sell 5

6 Programming With Java ICS201 - public and private Modifiers … Illegal because we try to access a private member (age) from o utside the class Employee

7 Programming With Java ICS201 Problem..  It is considered good programming practice to make all instance variables private Question: how to access and modify the instanc e variables of Employee objects e1, e2, e3 and e 4?.. answer.. Use accessor and mutaor method s …. next slide..

8 Programming With Java ICS201 - Accessor and Mutator Methods …  The methods that retrieve the data of fields are called accessors. The data can be accessed but not changed The name of an accessor method starts with the word get Example: public String getName() { return name; }  The methods that modify the data of fields are called mutators. The name of a mutator method starts with the word set Example: public void setName(String n) { name = n; }

9 Programming With Java ICS201 - Accessor and Mutator Methods (Example) Accessor method for instance variable name Mutator method for instance variable name Modifying the name of e1 using a mutator method

10 Programming With Java ICS201 Encapsulation and Inheritance Pitfall: Use of Private Instance Variables from the Base Class  An instance variable that is private in a base class is not accessible by name in the definition of a method in any other class, not even in a method definition of a derived class

11 Programming With Java ICS201 Encapsulation and Inheritance Pitfall: Use of Private Instance Variables from the Base Class  Instead, a private instance variable of the base class can only be accessed by the public accessor and mutator methods defined in that class

12 Programming With Java ICS201 Access Modifiers  public Can be used in any Java program without restriction  private may only be used by the instance of the class that declares the variable or method  Protected available to all classes in the same package available to all subclasses( even those in different packages )

13 Programming With Java ICS201 What is Package ?  Way to group a related class into one unit  To resolve the name conflicts between class names  Include all the classes in package import packageName.*; import packageName.className ;

14 Programming With Java ICS201 Some Predefined Java Packages Package NameContents java.appletClasses for implementing applets java.awtClasses for graphics, windows, and GUI’s java.awt.eventClasses supporting AWT event handling java.awt.imageClasses for image handling java.ioClasses for input and output java.langBasic language classes like Math (always available in any Java program) java.netClasses for networking java.utilUseful auxiliary classes like Date

15 Programming With Java ICS201 Visibility and Inheritance VisibilityPublicdefaultprotectedprivate Clients in same package CCCNone Clients in different packages CNone Subclass in same package CCCNone Subclass in different package CNoneC Note: C; Can access default: If the access modifier is omitted

16 Programming With Java ICS201 © 2006 Pearson Addison-Wesley. All rights reserved7-16 Access Modifiers

17 Programming With Java ICS201 The class Object  All classes defined without an explicit extends clause automatically extend Object The Object Class is the Superclass of Every Java Class

18 Programming With Java ICS201 The Class Object  The class Object is in the package java.lang which is always imported automatically public class Circle Equivalent public class Circle extends Object  Most useful methods: String toString() boolean equals(Object otherObject) Object clone()  Good idea to override these methods in your own classes


Download ppt "Programming With Java ICS201 University Of Ha’il1 ICS 201 Introduction to Computer Science Inheritance."

Similar presentations


Ads by Google