Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Programming 1401104-3 Dr. Atif Alhejali Lecture 4 Inheritance 1Structured Programming.

Similar presentations


Presentation on theme: "Structured Programming 1401104-3 Dr. Atif Alhejali Lecture 4 Inheritance 1Structured Programming."— Presentation transcript:

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

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

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 Programming4

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 Programming5

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

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 Programming7

8 Interface, Abstract Class and Class inheritance Structured Programming8 Interface Can only have data members with initial values. Can only have the methods declaration without any implementation Abstract Class Can have normal data members Can have the methods declaration or implementation Class Can have normal data members Can only have the methods implementation 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) 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) 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

9 Example Structured Programming9 CitizenInteface CitizenAbstract Citizen1Citizen2Citizen3 Implements Extends Interface Abstract Class Class

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

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

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 Programming12

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

14 Class inhertance public class Citizen2 extends Citizen1{ int noOfChildren; public void print(){ … } Structured Programming14 Method override

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

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 Programming16

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

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

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 Programming19


Download ppt "Structured Programming 1401104-3 Dr. Atif Alhejali Lecture 4 Inheritance 1Structured Programming."

Similar presentations


Ads by Google