Fundamentals of computing 1 - Lecture title: Inheritance

Inheritance (Is a) Method Overriding super keyword Runtime polymorphism Aggregation (Has a)

ppt41 trang | Chia sẻ: nguyenlam99 | Lượt xem: 735 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Fundamentals of computing 1 - Lecture title: Inheritance, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Lecture Title: InheritanceFundamentals of Computing 1AgendaInheritance (Is a)Method Overridingsuper keywordRuntime polymorphismAggregation (Has a)InheritanceInheritance is a mechanism in which one object acquires all the properties and behaviour of parent objectInheritance represents the IS-A relationship.Syntax of Inheritanceclass Subclass-name extends Superclass-name { //methods and fields }The keyword extends indicates that you are making a new class that derives from an existing classIn Java, a class that is inherited is called a superclass. The new class is called a subclassExample of inheritanceProgrammer is a subclass and Employee which is a superclass. Relationship between two classes is Programmer IS-A Employee. It means that Programmer is a type of Employee. Example of inheritance (cont.)Output: Programmer salary is:40000 Bonus of programmer is:10000 Programmer object can access the field of own class as well as of Employee class (code reusability).Example of inheritance (cont.)Rectangle.javaSquare.javaMainPrg.javaTypes of InheritanceWhy use Inheritance?For Method Overriding (So Runtime Polymorphism).For Code Reusability.AgendaInheritance (Is a)Method Overridingsuper keywordRuntime polymorphismAggregation (Has a)If a subclass provides a specific implementation of a method that is already provided by its superclass, it is known as Method OverridingMethod OverridingMethod Overriding is used for Runtime PolymorphismAdvantage of Method Overridingmethod must have same name as in the superclassmethod must have same parameter as in the superclass.Rules for Method OverridingUnderstanding the problem without method overridingOutput: Vehicle is runningExample of method overridingOutput: Bike is running safelyDifference between Method Overloading and Method OverridingMethod Overloading Method Overriding1) Method overloading is used to increase the readability of the program.Method overriding is used to provide the specific implementation of the method that is already provided by its super class.2) method overloading is performed within a class.Method overriding occurs in two classes that have IS-A relationship.3) In case of method overloading parameter must be different.In case of method overriding parameter must be same.AgendaInheritance (Is a)Method Overridingsuper keywordRuntime polymorphismAggregation (Has a)super keywordsuper is a reference variable that is used to refer to immediate parent class object.Uses of super Keywordsuper() is used to invoke immediate superclass constructor.super is used to refer immediate superclass data member.super is used to invoke immediate superclass method.1. super is used to invoke superclass constructor (cont.)Rule: super() is added in each subclass constructor automatically by compilerClass Bike extends Vehicle { }Bike.javacompilerClass Bike extends Vehicle { Bike(){ super() ; //first statement } }Bike.class1. super is used to invoke superclass constructor (cont.)Output: Vehicle is created Bike is createdExample: 1. super is used to invoke superclass constructorOutput: Vehicle is created Bike is created1. super is used to invoke superclass constructor (cont.)Output: Vehicle is created 10Another Example: 2. super is used to refer immediate superclass instance variableWithout super keywordOutput: 1002. super is used to refer immediate superclass instance variable (cont.)With super keywordOutput: 503. super can be used to invoke immediate superclass methodOutput: Bike is running safely Vehicle is runningProgram in case super is not requiredOutput: Vehicle is runningAgendaInheritance (Is a)Method Overridingsuper keywordRuntime polymorphismAggregation (Has a)Runtime polymorphismRuntime polymorphism is a process in which a call to an overridden method is resolved at runtime rather than at compile-timeIn this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable. UpcastingWhen reference variable of superclass refers to the object of subclass, it is known as upcastingExample of Runtime PolymorphismWhen reference variable of superclass refers to the object of subclass, it is known as upcastingOutput: Bike is running safelyExample of Runtime PolymorphismFigure.javaCircle.javaSquare.javaRectangle.javaExample of Runtime PolymorphismMainPrg.javaRuntime Polymorphism with data memberOverriden method is not the datamember, so runtime polymorphism can't be achieved by data membersOutput: 90AgendaInheritance (Is a)Method Overridingsuper keywordRuntime polymorphismAggregation (Has a)AggregationIf a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationshipclass Employee{ int id; String name; Birthday birth;//Birthday is a class ...}Employee has an entity reference variable birth, so relationship is Employee HAS-A BirthdayWhy use Aggregation?For Code Reusability.When use Aggregation?Code reuse is also best achieved by aggregation when there is no is-a relationship.Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice.Understanding meaningful example of AggregationUnderstanding meaningful example of Aggregation (cont.)What we have coveredInheritance (Is a)Method Overridingsuper keywordRuntime polymorphismAggregation (Has a)

Các file đính kèm theo tài liệu này:

  • pptlecture_09_inheritance_and_polymorphism_015.ppt
Tài liệu liên quan