Presentation is loading. Please wait.

Presentation is loading. Please wait.

 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.

Similar presentations


Presentation on theme: " In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should."— Presentation transcript:

1

2  In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should still use getter and setter methods for values of variables.

3  Need to plan for inheritance if writing a large application  When you subclass a class you should only:  add additional methods  add additional variables  override methods  Modifying existing software can be dangerous - reuse is the safest policy  With inheritance you can:  inherit the behaviour you need  override the (few) methods you want to behave differently  add new methods to do additional things

4  The signature of a method is determined by the method name and the number and data types of arguments.  The following 3 methods have different signatures ◦ public void myMethod (int one, int two) ◦ public void myMethod (int one, float two) ◦ public void myMethod (int one, float two, int three)

5  The signature of a method does not include its modifier (public, private, protected etc) and return type.  The following declarations will result in errors because the methods with the same name do not have different signatures. ◦ public void methodOne (int x) ◦ private void methodOne (int x) ◦ public int methodTwo (float y) ◦ public float methodTwo (float y)

6  Polymorphism means many shapes and in the context of a programming language it means that you can use one name to refer to many methods  While many methods have the same name only one of them is executed and the compiler makes its decision about which one to execute based on the way the method is called.  2 forms of Polymorphism we will look at ◦ Overloading ◦ Overriding

7  Polymorphism is the way in which the appropriate method to act on an object is selected amongst those methods of the same name

8

9  But if a subclass has quite different behaviour, how does Java know what to do?  Overridding means that the subclass has a method with the same method declaration as a method in the superclass  Java automatically selects the version of the method associated with the class of the object that’s using it.

10


Download ppt " In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should."

Similar presentations


Ads by Google