Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1 Inheritance University Of Ha’il.

Similar presentations

Presentation on theme: "Chapter 1 Inheritance University Of Ha’il."— Presentation transcript:

1 Chapter 1 Inheritance University Of Ha’il

2 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 Base Class Derived Class

3 Inheritance Animal Dog Fox Cat

4 Inheritance Land Vehicle Truck Car Bus

5 Inheritance Land Vehicle Truck Bus Car Toyota Yaris Corolla Camry

6 An Example Inheritance Hierarchy
The implicit possession by all subclasses of features defined in its superclasses Data and behavior associated with super-classes are accessible to those subclasses

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 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. University Of Ha’il

9 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 University Of Ha’il

10 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

11 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 University Of Ha’il

12 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 University Of Ha’il

13 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 University Of Ha’il

14 Example University Of Ha’il

15 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. Dr.Mwaffaq Abu Alhija University Of Ha’il

16 Inheriting Methods Override method: Inherit 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 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 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 Overloading deals with multiple methods in the same class with the same name but different signatures. Overriding lets you define a similar operation in different ways for different object types Overloading lets you define a similar operation in different ways for different data

19 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. University Of Ha’il

20 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 The following method header is not valid in a derived class: University Of Ha’il

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

Download ppt "Chapter 1 Inheritance University Of Ha’il."

Similar presentations

Ads by Google