Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,

Similar presentations


Presentation on theme: "Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,"— Presentation transcript:

1 Class Inheritance

2 The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers, this saves us a mountain of time. We do not need to build things from scratch as we develop new programs. We can “add on” to each individual layer, one building on top of each other.

3 Science classification system: Animal Vertebrates Mammals Primates Homo Sapiens

4 Animals (has certain characteristics) Vertebrates takes on all characteristics of Animals. That means the all Vertebrates are animals. Not all animals are necessarily Vertebrates. Vertebrates is a child or subclass of Animals. Animals is a parent or superclass of Vertebrates. Primates takes on all characteristics of Mammals. That means the all Primates are Mammals. Not all Mammals are necessarily Primates. Primates is a child or subclass of Mammals. Mammals is a parent or superclass of Primates. Both Mammals and Primates are subclasses of Vertebrates. Animals is the superclass of all these classes.

5 Video Games Consider the game: Counter-Strike: Gun Assault Rifles Automatic AK-47

6 Guns (has certain characteristics) Assault rifles takes on all characteristics of Guns. That means that all assault rifles are guns. Not all guns are necessarily assault rifles. Assault rifles is a child or subclass of Guns. Guns is a parent or superclass of assault rifles. Automatics takes on all characteristics of assault rifles. That means that all automatics are assault rifles. Not all assault rifles are necessarily automatics. Automatics is a child or subclass of assault rifles. Assault rifles is a parent or superclass of automatics. Both automatics and assault rifles are subclasses of guns. Guns is the superclass of both automatics and assault rifles.

7 class person { // body of code } class student extends person { // body of code } /* class student takes on all the properties of class person. Person is the superclass of student and student is the subclass of person. */

8 class person { String gender; int height; int weight; public person(char g, int h, int w) { gender = g; height = h; weight = w; } The constructor

9 class student extends person { String gender; int height;  All inherited from person int weight; // however, constructor is not. // constructor is never inherited automatically String name; int identification; public student(char g, int h, int w, String n; int ID) { super(g,h,w);// you can manually call superclass constructor name = n; identification = ID; } }

10 A subclass inherits all the methods and variables of its superclass except for the constructor. A subclass can add new private/public variables A subclass can add new private/public methods A subclass must define its own constructors A subclass can explicitly call its superclass constructor using the reserved word super() within its own constructor method.

11 class person { String gender; int height; int weight; public person(char g, int h, int w) { gender = g; height = h; weight = w; } class student extends person { String gender; int height;  All inherited int weight; String name; int identification; public student(char g, int h, int w, String n; int ID) { super(g,h,w); name = n; identification = ID; }

12 class person { String name; } What about private data? Can a subclass of person change the private data of person?

13 class person { String name; } class student extends person { public void setname(); //suppose this sets name } void main() { person newperson = new person(); newperson.setname(); }

14 A SUBCLASS CANNOT CHANGE THE PRIVATE VALUES OF ITS PARENT CLASS. IT MAY HOWEVER CALL ITS PARENTS’ MUTATOR/ACCESSOR METHODS.

15 Let’s deal with the constructor issue class X { int x,y; public X(int a, int b) { x = a; y = b; } class Y extends X { int s; public Y(int c) { s = c; }

16 void main { int num1 = 10; Y myclass = new Y(num1); } What’s the problem? The problem is that when you declare an instance of class Y, you have also declared an instance of class X, since Y inherits X. X is expecting two variables to use its constructors, which you will need to deal with.

17 Correct Implementation class X { int x,y; public X(int a, int b) { x = a; y = b; } class Y extends X { int s; public Y(int a, int b, int c) { super(a,b); s = c; }

18 Example 1: Is this ok? class X { int x,y; } class Y extends X { int s; public Y(int a, int b, int c) { x = a; y = b; s = c; }

19 Example 2: Is this ok? class X { int x,y; } class Y extends X { int s; public Y(int a, int b, int c) { super(a, b); s = c; }

20 Example 3: Is this ok? class X { int x,y; public X() { x = 0; y = 0; } class Y extends X { int s; public Y(int a, int b, int c) { s = c; x = a; y = b; }

21 Polymorphism and Method Overloading

22 class X { int x,y; public X() { x = 0; y = 0; } class Y extends X { public Y() { x = 5; y = 5; }

23 class X { int x,y; public X() { x = 0; y = 0; } public void method(int a, int b) { // some action } class Y extends X { double x; double y; public Y() { x = 11.5; y = 12.3; } public void method(double a) { // some action }

24 A subclass can override its parents’ methods and variables. However, you need to be careful when doing so. However, we need to understand that a subclass cannot change the values of its parents’ private data, it simply overrides it for its own instance. This is called method overriding. You can have multiple methods that carry the same name but take different parameters. This is called method overloading. In object oriented languages, when object can take different forms and shapes, we refer to that as polymorphism. It means that object can pass the “more than IS-A” test.

25 Exercise 1 Build a class called Person. Give it four private variables: String name int age double height double weight Develop a constructor that will take four values as its parameters and initialize these values. Develop a method called Country. Country will ask what country the person lives in and return a string from the desired input. Develop a method called City that will also intake a string and return that string.

26 Exercise 2 Develop a class called Student that extends Person. Student is to have four variables. int ID double GPA String city String country Build a constructor with no parameters that asks what the GPA is and asks what the ID is. Run the Country method from you superclass and set it equal to country. Run the City method from your superclass and set it equal to city.

27 Exercise 3 Develop a new methods called Country and City under class Student.. Country now takes two parameters, String name, String CapitalCity, combines both of them together separated by a comma, and returns the values as one string. City now takes two parameters, String name and int population. It no longer returns a value but sets the value of name to city and returns the value of population as an integer.


Download ppt "Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,"

Similar presentations


Ads by Google