150 likes | 157 Views
These slides provide an overview of inheritance in Java, explaining how it allows the reuse of code from existing classes to create subclasses. The slides cover topics such as subclass creation, method overriding, and the use of inheritance in collections.
E N D
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.
Bell Ringer • What are some collections that occur in life?
Reminder: What’s a class • 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
What is 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 theobjects are created. • We have sub classes in real life: • Building -> House • Animal -> Dog • Vehicle -> Train • Electronic Device -> MP3 Player • Person -> Student
How does it work • 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; } }
Some things to note • 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.
Overriding Methods • 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.
Creating Parent and Child Classes • 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 • 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; }
Cosmic SuperClass • 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
Collections • 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());
instanceof • 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(thisGuyinstanceof 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.