Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities.

Similar presentations


Presentation on theme: "Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities."— Presentation transcript:

1 Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities are shared between objects, and it makes sense to write these so that the developer only writes them once. Less to write, more time to have fun. Easier to maintain code. If there is a bug you only need to solve it once.

2 Reuse Example from the real world:
A TV manufacturer has created software for a TV that can view TV, change channels, view TV listings. All the normal TV features. They need to create a version with recording facilities. It would be much more sensible to use the same code for the normal features and extend the functionality to record TV as well.

3 Inheritance In OOP this is commonly achieved using inheritance. A new class can be made that extends another class. The new class is known as a child class/subclass of the parent class/superclass (the already existing class). The child class inherits all of the data and methods of the parent class. can declare new attributes and methods. can, if necessary, override methods to change the behaviour of the methods inherited from the parent. A child object can be assigned to a parent variable.

4 Libraries and API An OOP program usually consists of many classes where a lot of them are inherited from a parent class to provide the functionalities that are needed. Often the development consists of creating a library of classes. These can be shared and reused in extensions of the application or even shared as completely new programs . These are often called Application Programming Interfaces (API).

5 Organizing inheritance
Class A Class B Class C Class D Class E

6 Organizing inheritance - modifiers
When declaring attributes and methods the developer can control which other objects are able to access the attributes and call the methods. These are called modifiers, and there are different levels of access: Private level: Only the object can access. Protected level: Only the object or objects of subclasses can access. Package level: Only objects organised in the same package can access. Public level: Any object can access.

7 Controlling access Modifier Class Classes inside package Subclasses
outside package World public Y protected N No modifier (package) private

8 Organizing inheritance - modifiers
Class A Access: Public Protected Class B Class C Class D Class E

9 Organizing inheritance - modifiers
Class A Class B Class C Access: Public Class D Class E

10 Organizing inheritance - overriding
Class A protected int count Class B Class C Access: this super Class D Class E protected int count

11 Organizing inheritance - overriding
Class A protected int doSomething() Class B Class C Access: super Class D Class E protected int doSomething() Change behaviour in subclass. Can still call superclass’ method to Provide original behaviour.


Download ppt "Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities."

Similar presentations


Ads by Google