190 likes | 282 Views
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.
E N D
Structured Programming1401104-3Dr. Atif Alhejali Lecture 4 Inheritance Structured Programming
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
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
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
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
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
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
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
Example Abstract Class Interface CitizenAbstract CitizenInteface Implements Extends Implements Citizen1 Citizen2 Citizen3 Extends Class Class Class Structured Programming
Interface example public interface CitizenInterface { String natinality = "Saudi"; public void print(); public void addChild(String childName); } Structured Programming
Abstract class Example public abstract class CitizenAbstract{ String name; public void print(){ System.out.println(name); } public abstract void addSpouse(); } Structured Programming
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
A class implementing an interface public class Citizen1 implements CitizenInterface { public void print(){ … } public void addChild(String childName){ … } } Structured Programming
Class inhertance public class Citizen2 extends Citizen1{ intnoOfChildren; public void print(){ … } } Method override Structured Programming
A class inheriting an abstract class public class Citizen3 extends CitizenAbstract{ public void addChild(String childName){ } public void addSpouse(){ } } Structured Programming
“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
“super” and override example public class Citizen1{ public void print(){ System.out.println("name); } } Structured Programming
“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
“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