Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICS 201 Inheritance Introduction to Computer Science

Similar presentations


Presentation on theme: "ICS 201 Inheritance Introduction to Computer Science"— Presentation transcript:

1 ICS 201 Inheritance Introduction to Computer Science
University Of Ha’il

2 Inheritance Hierarchy

3 Encapsulation Data Hiding Abstraction Security ؟

4 Encapsulation (data hiding)
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 withDraw Deposit Transfer Print AccountNo AccountValue Name Address withDraw any value Change Address

5 Encapsulation Sell (1 SR, Pepsi) Person Vending Machine Sell Buy Pepsi

6 - public and private Modifiers …
Illegal because we try to access a private member (age) from outside the class Employee

7 Problem .. It is considered good programming practice to make all instance variables private Question: how to access and modify the instance variables of Employee objects e1, e2, e3 and e4? .. answer .. Use accessor and mutaor methods …. next slide ..

8 - 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 - 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 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 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 Access Modifiers public private Protected
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 What is Package ? Include all the classes in package
Way to group a related class into one unit To resolve the name conflicts between class names import packageName.className ; Include all the classes in package import packageName.*;

14 Some Predefined Java Packages
Package Name Contents java.applet Classes for implementing applets java.awt Classes for graphics, windows, and GUI’s java.awt.event Classes supporting AWT event handling java.awt.image Classes for image handling java.io Classes for input and output java.lang Basic language classes like Math (always available in any Java program) java.net Classes for networking java.util Useful auxiliary classes like Date

15 Visibility and Inheritance
Public default protected private Clients in same package C None Clients in different packages Subclass in same package Subclass in different package Note: C; Can access default: If the access modifier is omitted

16 Access Modifiers © 2006 Pearson Addison-Wesley. All rights reserved

17 The Object Class is the Superclass of Every Java Class
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 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 "ICS 201 Inheritance Introduction to Computer Science"

Similar presentations


Ads by Google