Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Orientated Design and Programming Unit 8: Inheritance and Polymorphism Jin Sa.

Similar presentations


Presentation on theme: "Object-Orientated Design and Programming Unit 8: Inheritance and Polymorphism Jin Sa."— Presentation transcript:

1 Object-Orientated Design and Programming Unit 8: Inheritance and Polymorphism Jin Sa

2 Inheritance Inheritance is the mechanism by which attributes and behaviours are passed by a ‘superclass’ to a ‘subclass’ the subclass does not need to re- implement existing behaviour, it can use the existing behaviour and data by inheriting it.

3 Why inherit Person <- Student <- MScStudent <- ResearchStudent <- Customer <- CorporateCustomer <- individualCustomer

4 What to inherit Subclass inherits from a Superclass all (non- private) superclass behaviours and attributes The subclass even receive all the relationships of its superclass. The subclass may provide additional attributes and behaviours, relevant to its specialised level of abstraction. subclass may modify the behaviours it inherits from its superclass.

5 Terminology Given a subclass MySub and a superclass MySuper. Then we say… –MySub inherits from MySuper. –MySub is a subclass of MySuper. –MySub is a specialisation of MySuper. –MySuper is a superclass of MySub. –MySuper is a generalisation of MySub.

6 When to use inheritance Avoid duplication in functionality. There is some common functionality. But remember! Don’t confuse object instances with inheritance. Inheritance implies different types, not different instances of the same type. –Examples in class

7 Student activity 8.1 Give an example of Inheritance –Think of something that we have not mentioned so far.

8 Implementing inheritance in Java Java allows you to derive new classes from existing classes using “extends” Example: student class and MSc student class. Student class: –Attributes: name, mark –Methods: getMark, setMark. MScStudent class could be derived from the Student class, therefore inheriting the variables and methods contained in the Student class. –New attribute: supervisor and method: chooseSupervisor

9 An example of inheritance: Student public class Student { private String name; private int mark; public void setName(String n){ name=n;} public String getName() { return name;} public void setMark(int m) { mark=m;} public int getMark(){ return mark;} }

10 An example of inheritance: MScStudent public class MScStudent extends Student { private String supervisor; public void chooseSupervisor(String nm) { supervisor=nm;} public String getSupervisor() { return supervisor;} }

11 Using MScStudent public class TestInheritance { public static void main(String [] args) { MScStudent s2=new MScStudent(); s2.setName("Mike"); s2.chooseSupervisor("Jane"); System.out.println("s2 is "+ s2.getName()+ "--Supervisor is "+s2.getSupervisor()); }

12 Student activity 8.2 Implement Student and MScStudent Explore private, public attributes –Add a new method in the MScStudent class. The new method should change the value of the variable name. e.g. public String getName(){return name;} –Can you compile MScStudent now? Explain why and post your explanation to the module forum.

13 The protected modifier and visibility modifiers revisited A protected data or a protected method can be accessed or invoked by any class in the same package or its subclasses, even if the subclasses are in a different package.

14

15 Using the key word super and calling superclass constructors public class Student { private String name; Student(String nm){ name=nm;} … } public class MScStudent extends Student { private String supervisor; MScStudent(String nm){ super(nm);} … }

16 Overriding methods 1 public class Student { private String name; int mark; … other methods public String toString() { return "Name: "+name+ "Mark: "+mark; }

17 Overriding methods 2 public class MScStudent extends Student { private String supervisor; MScStudent(String nm){ super(nm);} … other methods public String toString(){ String ss=super.toString(); return ss+“ supervisor: "+supervisor; } }

18 Polymorphism via inheritance, and casting public class TestPoly { public static void main(String [] args) { Student s1=new Student("Kate"); System.out.println(s1.toString()); s1=new MScStudent("Mike"); System.out.println(s1.toString()); }

19 Polymorphic The term polymorphism: “having many forms”. A polymorphic reference is a reference variable that can refer to different types of objects at different points in time An example of polymorphism is the technique by which a reference that is used to invoke a method can actually invoke different methods at different times depending on what it is referring to at the time. Polymorphism provides a means to an elegant versatility in our software. It allows us to provide a consistent approach to different but related behaviours.

20 Example public class TestPoly2 { public static void main(String [] args) { Student [] all= new Student[3]; all[0]= new Student("kate"); all[1]= new MScStudent("mike"); all[2]= new Student("Jane"); for (int i=0;i<3;i++) System.out.println(all[i].toString()); }

21 Casting 1 Casting can be used to convert an object of one class type to another within an inheritance hierarchy. There are implicit casting and explicit casting.

22 Casting 2 Student s=new MScStudent(“Mike”); assigns an MScStudent object to a reference of the Student type. This statement involves an implicit casting. This is legal because an instance of MScStudent is an instance of Student.

23 Casting 3 Explicit casting must be used when casting an object from a superclass to a subclass. Student s=new MScStudent("Mike"); s.chooseSupervisor(“Helen”); s is a reference of type Student. Since the Student class does not have the method chooseSupervisor, the compiler doesn’t know that the object pointed by s has the chooseSupervisor method. So, we have to explicitly cast s to MScStudent as follows: ((MScStudent)s).chooseSupervisor(“Helen”);

24 Must have toString in the superclass Student [] all= new Student[3]; all[0]= new Student("kate"); all[1]= new MScStudent("mike"); all[2]= new Student("Jane"); for (int i=0;i<3;i++) System.out.println(all[i].toString());

25 Summary In this section, we have explained how to derive subclasses from super classes through inheritance the use of the visibility modifiers the use of the key word super how to override methods what is casting the concept of polymorphism and the applications

26

27


Download ppt "Object-Orientated Design and Programming Unit 8: Inheritance and Polymorphism Jin Sa."

Similar presentations


Ads by Google