These are the inheritance slides. They are slides just like all the other AP CS slides. But they are...

15
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. Inheritance

Transcript of These are the inheritance slides. They are slides just like all the other AP CS slides. But they are...

Page 1: 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.

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.

Inheritance

Page 2: 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?

Bell Ringer

Page 3: 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.

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

Reminder: What’s a class

Page 4: 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.

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 -> HouseAnimal -> DogVehicle -> TrainElectronic Device -> MP3 PlayerPerson -> Student

What is inheritance?

Page 5: 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.

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.

How does it work

Page 6: 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.

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;

}

}

Page 7: 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.

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;

}

}

Page 8: 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.

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.

Some things to note

Page 9: 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.

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.

Overriding Methods

Page 10: 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.

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.

Creating Parent and Child Classes

Page 11: 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.

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; }

The constructor of the Sub Class

Page 12: 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.

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

Cosmic SuperClass

Page 13: 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.

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<Person> crowd = new ArrayList<Person>();crowd.add(new Student());crowd.add(new Teacher());crowd.add(new Parent());

Collections

Page 14: 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.

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

ExPerson 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.

instanceof

Page 15: 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.

Think of real life inheritance