1 / 19

Structured Programming 1401104-3 Dr. Atif Alhejali

Structured Programming 1401104-3 Dr. Atif Alhejali. Lecture 4 Inheritance . Interface. Interface is an abstract type that contains no data, but  exposes  behaviours defined as methods. A class having all the methods corresponding to that interface is said to implement that interface.

roxy
Download Presentation

Structured Programming 1401104-3 Dr. Atif Alhejali

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. Structured Programming1401104-3Dr. Atif Alhejali Lecture 4 Inheritance Structured Programming

  2. Interface • Interface is an abstract type that contains no data, but exposes behaviours defined as methods. • A class having all the methods corresponding to that interface is said to implement that interface. • Furthermore, a class can implement multiple interfaces, and hence can be of different types at the same time. Structured Programming

  3. Interface Interface CAR start(); Accelerate(speed); Break(); Fill tank(Petrol litres); Car No Of Doors No of Seats Driving wheels Class start() { …} Accelerate(speed) { …} Break() { …} Fill tank(Petrol litres) { …} Structured Programming

  4. Abstract Class • In programming languages, an abstract type is a type in a nominative type system which cannot be instantiated directly. Abstract types are also known as existential types[1]. An abstract type may provide no implementation, or an incomplete implementation Structured Programming

  5. Inheritance Classes are created in hierarchies, and inheritance allows the structure and methods in one class to be passed down the hierarchy. That means less programming is required when adding functions to complex systems. If a step is added at the bottom of a hierarchy, then only the processing and data associated with that unique step needs to be added. Everything else about that step is inherited. The ability to reuse existing objects is considered a major advantage of object technology. Structured Programming

  6. Inheritance Main class Father class CAR start() Accelerate(speed) Break() No Of Doors No of Seats Driving wheels Children Classes SUV Sedan Hatchback Coupe No Of Doors No of Seats Driving wheels= 4 No Of Doors No of Seats = 5 Driving wheels= 2 No Of Doors No of Seats = 5 Driving wheels= 2 No Of Doors = 2 No of Seats = 4 Driving wheels= 2 Top speed start() Accelerate(speed) Break() 4 wheel drive() start() Accelerate(speed) Break() start() Accelerate(speed) Break() start() Accelerate(speed) Break() Engage turbo() Structured Programming

  7. Method overriding • Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class. • The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed Structured Programming

  8. Interface, Abstract Class and Class inheritance • implements • Will inherit all the data members and methods declaration • May implements some or all the declared method (or none) • May have any extra data members or methods (implemented or declared) • Interface • Can only have data members with initial values. • Can only have the methods’ declaration without any implementation • implements • Will inherit all the data members and methods declaration • Must implements all the declared methods • May have any extra data members or methods (must be implemented) • Abstract Class • Can have normal data members • Can have the methods’ declaration or implementation • Extends • Will inherit all the data members and methods • Must implements all the declared (unimplemented) methods • May have any extra data members or methods (must be implemented) • May override any implemented methods • Class • Can have normal data members • Can only have the methods’ implementation Structured Programming

  9. Example Abstract Class Interface CitizenAbstract CitizenInteface Implements Extends Implements Citizen1 Citizen2 Citizen3 Extends Class Class Class Structured Programming

  10. Interface example public interface CitizenInterface { String natinality = "Saudi"; public void print(); public void addChild(String childName); } Structured Programming

  11. Abstract class Example public abstract class CitizenAbstract{ String name; public void print(){ System.out.println(name); } public abstract void addSpouse(); } Structured Programming

  12. An abstract class implementing an interface public abstract class CitizenAbstract implements CitizenInterface { String name; public void print(){ System.out.println(name); } public abstract void addSpouse(); } Structured Programming

  13. A class implementing an interface public class Citizen1 implements CitizenInterface { public void print(){ … } public void addChild(String childName){ … } } Structured Programming

  14. Class inhertance public class Citizen2 extends Citizen1{ intnoOfChildren; public void print(){ … } } Method override Structured Programming

  15. A class inheriting an abstract class public class Citizen3 extends CitizenAbstract{ public void addChild(String childName){ } public void addSpouse(){ } } Structured Programming

  16. “super” • The keyword “super” can be used to refer to parent of the current class. • It is usually used to call an overridden method or the parent constructor. Structured Programming

  17. “super” and override example public class Citizen1{ public void print(){ System.out.println("name); } } Structured Programming

  18. “super” and override example public class Citizen2 extends Citizen1 { public void print(){ System.out.print(“print the name : ”); super.print(); } } This method overrides “print” fromCitizen1 This will call “print” at Citizen1 Structured Programming

  19. “super” and override example public class Main { public static void main(String[] args) { Citizen2 c = new Citizen2(); c.name = “Mohammed”; c.print(); } } Output: print the name : Mohammed Structured Programming

More Related