structured programming 1401104 3 dr atif alhejali
Download
Skip this Video
Download Presentation
Structured Programming 1401104-3 Dr. Atif Alhejali

Loading in 2 Seconds...

play fullscreen
1 / 19

Structured Programming 1401104-3 Dr. Atif Alhejali - PowerPoint PPT Presentation


  • 62 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Structured Programming 1401104-3 Dr. Atif Alhejali' - roxy


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
structured programming 1401104 3 dr atif alhejali

Structured Programming1401104-3Dr. Atif Alhejali

Lecture 4

Inheritance

Structured Programming

interface
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

interface1
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
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
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

inheritance1
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
  • 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
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
Example

Abstract Class

Interface

CitizenAbstract

CitizenInteface

Implements

Extends

Implements

Citizen1

Citizen2

Citizen3

Extends

Class

Class

Class

Structured Programming

interface example
Interface example

public interface CitizenInterface {

String natinality = "Saudi";

public void print();

public void addChild(String childName);

}

Structured Programming

abstract class example
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
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
A class implementing an interface

public class Citizen1 implements CitizenInterface {

public void print(){

}

public void addChild(String childName){

}

}

Structured Programming

class inhertance
Class inhertance

public class Citizen2 extends Citizen1{

intnoOfChildren;

public void print(){

}

}

Method override

Structured Programming

a class inheriting an abstract class
A class inheriting an abstract class

public class Citizen3 extends CitizenAbstract{

public void addChild(String childName){

}

public void addSpouse(){

}

}

Structured Programming

super
“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
“super” and override example

public class Citizen1{

public void print(){

System.out.println("name);

}

}

Structured Programming

super and override example1
“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 example2
“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

ad