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 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

4 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

5 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 )

6 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 ;

7 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

8 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

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

10 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

11 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

12 Programming With Java ICS201 Define equals See Object2.java 7-12

13 Programming With Java ICS201 Object-Oriented Concept  Object & Class  Encapsulation  Inheritance  Polymorphism 13

14 Programming With Java ICS201 14 Polymorphism  polymorphism: many forms

15 Programming With Java ICS201 University Of Ha’il15 Polymorphism  Polymorphism (from the Greek, meaning "many forms", or something similar)  The mechanism by which several methods can have the same name and implement the same abstract operation. Requires that there be multiple methods of the same name The choice of which one to execute depends on the object that is in a variable Reduces the need for programmers to code many if- else or switch statements

16 Programming With Java ICS201 University Of Ha’il16 Polymorphism  Methods can be overloaded. A method has the same name as another member methods, but the type and/or the count of parameters differs. class Integer { float val; void add( int amount ) { val = val + amount; } void add( int num, int den) { val = float(num) / den; }

17 Programming With Java ICS201 Polymorphism  Add(integer, integer)  Add(string, string)  Add(string, integer) Add(1,1)  2 Add(“Hello”, “World”)  “HelloWorld” Add(“Hello”, 2)  “Hello2” 17

18 Programming With Java ICS201 Polymorphism :PaySlip :HourlyPaidEmployee :WeeklyPaidEmployee :MonthlyPaidEmployee  getTotalPay() calculatePay() 18


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

Similar presentations


Ads by Google