These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
What are some collections that occur in life?
A class is a collection of variables and methods. A blueprint that can be used to create objects. Something that encapsulates those things that defines the aspects and behavior of things in real life. Example: Building, Animal, Vehicle, Electronic Device, Person
A way to make use of code already created in other classes by adding variables and methods to sub classes. Those variables and methods add complexity and better define the objects are created. We have sub classes in real life: Building -> House Animal -> Dog Vehicle -> Train Electronic Device -> MP3 Player Person -> Student
First you start off with a Parent, Base, or as it is sometimes called a Super Class. Then you extend that with a Child, Derived, or as it is sometimes called a Sub Class. This is done by using the keyword extends after the class name Lets take a look at the code on the next two slides.
public class Parent{ public int x; private int y; protected int z; public Parent(int x, int y, int z){ this.x = x; this.y = y; this.z = z; } public void addToY(int n){ y += n; } public String toString(){ return "x = " + x + ", y = " + y + ", z = " + z; }
public class Child extends Parent{ private int a; public Child(int a, int x, int y, int z){ super(x, y, z); this.a = a; } public void addToAll(int n){ a += n; x += n; addToY(n); z += n; } public String toString(){ return super.toString() + ", a = " + a; }
Notice that you indicate the Child class is a subclass of the Parent class by the key word extends The constructor of the Parent class is called from the Child class using the key word super. It has to be the first line of code. x and z are directly accessible from the Child class. y is not because it is declared private so the method addToY is called instead. x is public so it is accessible even outside of the class. z is protected so it is not accessible outside of the class but is accessible by all sub classes.
Child classes can redefine how methods of the Parent classes function by redefining them. They have to have the same name and the same input variables. Ex: The toString method is overridden in the Child class. public String toString(){ return super.toString() + ", a = " + a; } Notice the methods of the Parent class can still be referenced by using the key word super. Variables can be overridden in the same way and super can again be used to reference variables of the parent class.
Parent and Child Classes can be declared and created in the following way: Parent dad = new Parent(1, 1, 1); Child son = new Child(5, 5, 5, 5); Parent father = new Child(10, 10, 10, 10); The one that is NOT allowed is the following: Child sibling = new Parent(15, 15, 15); The logic behind this is that Animals can be Animals, Dogs can be Dogs, Animals can be Dogs, but when you say something is a Dog then you can’t assign a generic Animal to it.
The constructor of the sub class will many times need to call the constructor of the super class. This can be done using only the keyword supper. This also has to be the first line of code in the constructor. Ex: public Child(int a, int x, int y, int z){ super(x, y, z); //notice super is called first this.a = a; }
JAVA by default will have every class inherit from the Object class (whether you like it or not) The object class has several methods, two of which are usually overridden: String toString() – returns a String description of the object, if this is not overridden then it will return the class name and memory address boolean equals(Object other) – returns whether or not the other object is equal, if this is not overridden then it will return whether the two objects are refering to the same thing in memory
One of the more powerful things with having multiple classes inherit from a Parent class and have everything inherit from the Object class is that you can create Collections of varied classes. Ex: ArrayList crowd = new ArrayList (); crowd.add(new Student()); crowd.add(new Teacher()); crowd.add(new Parent());
Sometimes we get an object from a collection or somewhere else and we have to determine what subclass it is. We use instanceof to do this. We can then cast a Supper class as a Sub class Ex Person thisGuy = crowd.get(0); if(thisGuy instanceof Student){ ((Student)thisGuy).doHomework(); } Note that this Guy is not told to do his homework unless they are a Student and in order to do that we must first cast him as a student.