Java Inheritance

Inheritance:

package main.java; public class MyParentClass { public int myMemberVar1 = 3; public String myMemberVar2 = "test"; public void myMethod1 () { System.out.println( "Var 1 is " + myMemberVar1 + " Var 2 is " + myMemberVar2 ); } }
package main.java; public class MyChildClass extends MyParentClass { // "extends" keyword used to make child class public void myChildMethod () { System.out.println( "Parent class can't access this" ); } }
package main.java; public class MainClass { public static void main (String [] args) { MyChildClass myChildObject = new myChildClass(); myChildObject.myMemberVar1 = 5; // Can access parent variable myChildObject.myMethod1(); // Can access parent function myChildObject.myChildMethod(); } }

- A child class will have the variables and methods that a parent class has (except for those marked as "private")

- Create a child class with the "extends" keyword followed by the name of the class that it inherits from

- Inheritance is useful if you want to make many different classes that have some parts in common

Overriding a Parent Method:

package main.java; public class MyParentClass { public int myMethod1 () { System.out.println( "Text from parent method" ); } }
package main.java; public class MyChildClass extends MyParentClass { @Override // Put the "Override" annotation public int myMethod1 () { // Define a method with the same name System.out.println( "Do this instead" ); } }

- An inherited method can be modified to do something different when called from a child class

- This could be useful if you want to inheret only some of the parent methods

Constructor in child class:

package main.java; public class MyParentClass { public int myVar1; public MyParentClass (int myParam) { myVar1 = myParam; } }
package main.java; public class MyChildClass extends MyParentClass { public MyChildClass () { super(2); // Call the parent constructor // Other code unique to child constructor } }

- A constructor for a child class must use the 'super' keyword to call the constructor of the parent class

- The 'super' call must be the first line of the function

Arrays of Related Objects:

package main.java; public class MainClass { public static void main (String [] args) { MyParentClass [] objs = new MyParentClass [3]; // Create array of size 3 objs[0] = new MyChildClass1 (); objs[1] = new MyChildClass2 (); objs[2] = new MyChildClass1 (); } }

- An array that stores objects of a class can also store objects of its inherited classes

Challenge

Create a class called "Bird" which stores the weight, height, and wingspan. Make a constructor that fills those values according to parameters. Make the functions "makeSound" and "fly"; "makeSounds" should display "chirp chirp", and the function "fly" should display "flap flap". Then create two child classes "Hawk" and "Sparrow". In the hawk class, add a function called "hunt" which will display "hunting", and change the inherited "makeSound" class to display "*majestic scream*" In the sparrow class, add a function called "findSeeds" which will display "finding seeds". Now in main, create an object (instantiation) of each class and have them both perform their functions.

Quiz

Completed