1 / 21

Chapter 1 Inheritance

Chapter 1 Inheritance. Introduction to Inheritance. Inheritance is one of the main techniques of object-oriented programming (OOP)

ipo
Télécharger la présentation

Chapter 1 Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Cat Dog Fox Inheritance Animal

  4. Bus Truck Car Inheritance Land Vehicle

  5. Bus Truck Inheritance Land Vehicle Car Toyota Yaris Corolla Camry

  6. An Example Inheritance Hierarchy • 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. A derived class, also called a subclass (or child class), is defined by starting with another already defined class, called a baseclass 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. Derived Class (subclass) University Of Ha’il

  9. The extends clause in a class declaration establishes an inheritance relationship between two classes. It has the following syntax: classDerivedClassextendsBaseClass { // body of the class } Derived Classes 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 Bcan be called a descendent of class A

  11. 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 Inheritance and Variables University Of Ha’il

  12. class C1 { static int x; } class C2 extends C1 { staticString 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); } } Example(Inheritance and variables) Output: p.x=55 q.x=This is a String University Of Ha’il

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

  14. Example • Inherit1.java • Inherit2.java University Of Ha’il

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

  16. 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. 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 lets you define a similar operation in different ways for different data Overloading deals with multiple methods in the same class with the same name but different signatures. 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 vs Overriding • Comparison between overloading and overriding

  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 • 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() University Of Ha’il

  21. Example (Method Overriding) classMethodOverriding { publicstaticvoid 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 { void hello() { System.out.println( "Hello from B1" ); } } class C1 extends B1 { void hello() { System.out.println( "Hello from C1"); } } Output: Hello from C1 Hello from C1 University Of Ha’il

More Related