Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming With Java ICS201 University Of Ha’il1 Chapter 1 Inheritance.

Similar presentations


Presentation on theme: "Programming With Java ICS201 University Of Ha’il1 Chapter 1 Inheritance."— Presentation transcript:

1 Programming With Java ICS201 University Of Ha’il1 Chapter 1 Inheritance

2 Programming With Java ICS201 Introduction to Inheritance  Inheritance is one of the main techniques of object- oriented programming (OOP)  Using this technique, further classes can be created from existing ones; those classes are said to inherit the methods and instance variables of the class they inherited The original class is called the base class The new class is called a derived class  Advantage: Reusing existing code 7-2 Base Class Derived Class

3 Programming With Java ICS201 Inheritance Animal DogFoxCat 3

4 Programming With Java ICS201 Inheritance Land Vehicle TruckCarBus 4

5 Programming With Java ICS201 Inheritance Land Vehicle Car Toyota TruckBus YarisCorollaCamry 5

6 Programming With Java ICS201 6 An Example Inheritance Hierarchy Data and behavior associated with super-classes are accessible to those subclasses

7 Programming With Java ICS201 7 The is-a Rule  Always check generalizations to ensure they obey the is-a rule “A checking account is an account” “A village is a municipality” “A cat is an animal”

8 Programming With Java ICS201 University Of Ha’il8 Derived Class (subclass)  A derived class, also called a subclass (or child class), is defined by starting with another already defined class, called a base class or superclass or parent class, and adding (and/or changing) methods, instance variables, and static variables  The derived class inherits:  all the public methods,  all the public and private instance variables, and  all the public and private static variables from the base class.  The derived class can add more instance variables, static variables, and/or methods.

9 Programming With Java ICS201 University Of Ha’il9 Derived Classes  The extends clause in a class declaration establishes an inheritance relationship between two classes. It has the following syntax: class DerivedClass extends BaseClass { // body of the class } Base Class Derived Class

10 Programming With Java ICS201 Parent and Child Classes  A base class is often called the parent class A derived class is then called a child class  These relationships are often extended such that a class that is a parent of a parent... of another class is called an ancestor class If class A is an ancestor of class B, then class B can be called a descendent of class A 7-10

11 Programming With Java ICS201 University Of Ha’il11 Inheritance and Variables Variable Hiding  If a variable of a class has a same name (type maybe different) as a superclass variable, in that case class variable hides the superclass variable.  You can access a hidden variable by using super keyword super.variable

12 Programming With Java ICS201 University Of Ha’il12 Example (Inheritance and variables) class C1 { static int x; } class C2 extends C1 { static String x; } class Cc { public static void main(String[] args) { C1 p = new C1(); p.x = 55; System.out.println("p.x=" + p.x); C2 q = new C2(); q.x = "This is a String"; System.out.println( "q.x=" + q.x); } Output: p.x=55 q.x=This is a String

13 Programming With Java ICS201 University Of Ha’il13 Example (Inheritance and variables) class M100 { int x = 100; } class M200 extends M100 { String x = " Welcome “ ; void display() { System.out.println( "x=" + x); System.out.println( "super.x=" +super.x); } class SuperKeyword { public static void main(String[] args) { M200 m200 = new M200(); m200.display(); } Output: x= Welcome super.x=100

14 Programming With Java ICS201 Example  Inherit1.java Inherit1.java  Inherit2.java Inherit2.java University Of Ha’il14

15 Programming With Java ICS201 Dr.Mwaffaq Abu AlhijaUniversity Of Ha’il15 Homework Write an application that illustrates how to access a hidden variable. Class G declares a static variable x. Class H extends G and declares an instance variable x. A display() method in H displays both of these variables.

16 Programming With Java ICS201 Inheriting Methods  Override method: Supply a different implementation of a method that exists in the superclass Must have same signature (same name and same parameter types)  Inherit method: Don't supply a new implementation of a method that exists in superclass Superclass method can be applied to the subclass objects

17 Programming With Java ICS201 Inheriting Methods (Cont’d)  Add method: Supply a new method that doesn't exist in the superclass New method can be applied only to subclass objects

18 Programming With Java ICS Overloading vs Overriding  Comparison between overloading and overriding Overriding deals with two methods, one in a parent class and one in a child class with the same signature Overriding lets you define a similar operation in different ways for different object types Overloading deals with multiple methods in the same class with the same name but different signatures. Overloading lets you define a similar operation in different ways for different data

19 Programming With Java ICS201 University Of Ha’il19 Method Overriding  The access permission of an overridden method can be changed from private in the base class to public in the derived class.  However, the access permission of an overridden method can not be changed from public in the base class to a more restricted access permission in the derived class.

20 Programming With Java ICS201 University Of Ha’il20 Method Overriding  Given the following method header in a base case: private void doSomething()  The following method header is valid in a derived class: public void doSomething() However, the opposite is not valid  Given the following method header in a base case: public void doSomething()  The following method header is not valid in a derived class: private void doSomething()

21 Programming With Java ICS201 University Of Ha’il21 Example (Method Overriding) class A1 { void hello() { System.out.println( "Hello from A1" ); } class B1 extends A1 { void hello() { System.out.println( "Hello from B1" ); } class C1 extends B1 { void hello() { System.out.println( "Hello from C1"); } class MethodOverriding { public static void main(String[] arg) { C1 obj = new C1(); obj.hello(); A1 a = new C1() ; a.hello(); } Output: Hello from C1


Download ppt "Programming With Java ICS201 University Of Ha’il1 Chapter 1 Inheritance."

Similar presentations


Ads by Google