Inheritence Put classes into a hierarchy derive a




![public static void main(String[] args) { Circle x = new Circle(4. 2); System. out. public static void main(String[] args) { Circle x = new Circle(4. 2); System. out.](https://slidetodoc.com/presentation_image_h2/ad67ff3ceb7a6392f1ae88e214006fc5/image-5.jpg)






![Coolness: Animal[] an_arr = new Animal[3]; an_arr[0]= an_x; an_arr[1] = a_dog; an_arr[2] = a_wolf; Coolness: Animal[] an_arr = new Animal[3]; an_arr[0]= an_x; an_arr[1] = a_dog; an_arr[2] = a_wolf;](https://slidetodoc.com/presentation_image_h2/ad67ff3ceb7a6392f1ae88e214006fc5/image-12.jpg)
![Only methods and fields in superclass can be accessed automatically: Animal[] an_arr = new Only methods and fields in superclass can be accessed automatically: Animal[] an_arr = new](https://slidetodoc.com/presentation_image_h2/ad67ff3ceb7a6392f1ae88e214006fc5/image-13.jpg)





















- Slides: 34


Inheritence • Put classes into a hierarchy • derive a new class based on an existing class • with modifications or extensions. • Avoiding duplication and redundancy • Classes inthe lower hierarchy is called a subclass (or derived, child, extended class). • A class in the upper hierarchy is called a superclass (or base, parent class). • Place all common variables and methods in the superclass • Place specialized variables and methods in the subclasses • redundancy reduced as common variables and methods are not repeated in all the subclasses.

• In java, you use the word “extends” in the class definition to indicate a class is a subclass of another class, e. g. , • class Goalkeeper extends Soccer. Player {. . . } • class Chem. Student extends Student {. . . } • class Cylinder extends Circle {. . . }

public class Circle { private double radius; private double circumference; private double area; public Circle() { this(3. 1); } public Circle(double rad) { radius = rad; circumference = get. Circ(); area = get. Area(); } public double get. Rad() { return radius; } public void set. Rad(double rad) { radius = rad; circumference = get. Circ(); area = get. Area(); } } public double get. Circ() { return (2. 0 *radius * Math. PI); } public double get. Area() { return(radius * Math. PI); } public class Cylinder extends Circle { private double height; // Private field for only Cylinder objects public Cylinder(double radius, double h) { // Constructor super(radius); // invoke superclass' constructor height = h; } public Cylinder(double h) { super(); height = h; } public double get. Height() { return height; } public void set. Height(double h) { height = h; } public double get. Volume() { return get. Area()*height; // Use Circle's get. Area() } } public static void main(String[] args) { Circle x = new Circle(4. 2); System. out. format("Circle Area: %5. 2 fn", x. get. Area()); System. out. format("Circle Circumference: %5. 2 fn", x. get. Circ()); Cylinder y = new Cylinder(3. 0, 2. 0); System. out. format("Cylinder Area: %5. 2 fn", y. get. Area()); System. out. format("Cylinder Circumference: %5. 2 fn", y. get. Circ()); System. out. format("Cylinder Volume: %5. 2 fn", y. get. Volume()); }
![public static void mainString args Circle x new Circle4 2 System out public static void main(String[] args) { Circle x = new Circle(4. 2); System. out.](https://slidetodoc.com/presentation_image_h2/ad67ff3ceb7a6392f1ae88e214006fc5/image-5.jpg)
public static void main(String[] args) { Circle x = new Circle(4. 2); System. out. format("Circle Area: %5. 2 fn", x. get. Area()); System. out. format("Circle Circumference: %5. 2 fn", x. get. Circ()); Cylinder y = new Cylinder(3. 0, 2. 0); System. out. format("Cylinder Area: %5. 2 fn", y. get. Area()); System. out. format("Cylinder Circumference: %5. 2 fn", y. get. Circ()); System. out. format("Cylinder Volume: %5. 2 fn", y. get. Volume()); } Circle Area: 55. 42 Circle Circumference: 26. 39 Cylinder Area: 28. 27 Cylinder Circumference: 18. 85 Cylinder Volume: 56. 55

public class Animal { public boolean isa. Pet; public String name; public Animal() { isa. Pet = true; name = "Fred"; } public Animal(boolean pet, String name) { isa. Pet = pet; this. name = name; } public void sleep() { System. out. println("Animal is sleeping"); } public void talk() { System. out. println("talking"); } } public class Dog extends Animal { public String breed; } public Dog(){ super(); breed = “Mutt” } public Dog(String name, String breed) { this(true, name, breed); } public Dog(boolean pet, String name, String breed) { super(pet, name); this. breed = breed; } public void move() { System. out. println("Frolicking forward"); } public class main. Animal { public static void main(String[] args) { Animal an_x = new Animal(); System. out. println(an_x. name); System. out. println(an_x. isa. Pet); an_x. sleep(); an_x. talk(); } } Dog a_dog = new Dog("Spot“, ”pug”); System. out. println(a_dog. name); System. out. println(a_dog. isa. Pet); System. out. println(a_dog. breed); a_dog. sleep(); a_dog. talk(); a_dog. move(); What fields and methods are part of an_x? What fields and methods are part of a_dog?

Do you see a problem? class A { public A(int x) { } } class B extends A { public B() { } } class C { public static void main(String[] args) { B b = new B(); } }

public class Animal { public boolean isa. Pet; public String name; public Animal() { isa. Pet = true; name = "Fred"; } public Animal(boolean pet, String name) { isa. Pet = pet; this. name = name; } public void sleep() { System. out. println("Animal is sleeping"); } public void talk() { System. out. println("talking"); } } public class Dog extends Animal { public String breed; } public Dog(){ super(); breed = “Mutt” } public Dog(String name, String breed) { this(true, name, breed); } public Dog(boolean pet, String name, String breed) { super(pet, name); this. breed = breed; } public void move() { System. out. println("Frolicking forward"); } public void talk() { System. out. println(“bark"); } public class main. Animal { public static void main(String[] args) { Animal an_x = new Animal(); System. out. println(an_x. name); System. out. println(an_x. isa. Pet); an_x. sleep(); an_x. talk(); // what does this line do? } } Dog a_dog = new Dog("Spot“, ”pug”); System. out. println(a_dog. name); System. out. println(a_dog. isa. Pet); System. out. println(a_dog. breed); a_dog. sleep(); a_dog. talk(); // what does this line do? a_dog. move(); What methods and fields does a_dog have? What happens when a_dog. talk() is executed?

Overriding! • When in a subclass you write a method that overrides a method with the same name in its parent class. • In essence, you’ve got a default method in the superclass • And then you have a more specific (and accurate) method belonging to the subclass • Every subclass can have its own default method that overrides the superclass’s method

public class Animal { public boolean isa. Pet; public String name; public Animal() { isa. Pet = true; name = "Fred"; } public Animal(boolean pet, String name) { isa. Pet = pet; this. name = name; } public void sleep() { System. out. println("Animal is sleeping"); } public void talk() { System. out. println("talking"); } } public class Dog extends Animal { public Dog(){ super(); } public Dog(String name) { super(true, name); } public Dog(boolean pet, String name) { super(pet, name); } public void move() { System. out. println("Frolicking forward"); } public void talk() { System. out. println("bark"); } } public class Wolf extends Dog { public Wolf(){ super(false, "no. Name"); } public void move() { System. out. println("running intently"); } public void stalk() { System. out. println("stalking my prey"); } public void talk() { System. out. println("howl"); super. talk(); } } public class main. Animal { public static void main(String[] args) { Animal an_x = new Animal(); // what methods and fields are available to an_x? Dog a_dog = new Dog("Spot"); // what methods and fields are available to a_dog? Wolf a_wolf = new Wolf(); // what methods and fields are available to a_wolf? } }

public class Animal { public boolean isa. Pet; public String name; public Animal() { isa. Pet = true; name = "Fred"; } public Animal(boolean pet, String name) { isa. Pet = pet; this. name = name; } public void sleep() { System. out. println("Animal is sleeping"); } public void talk() { System. out. println("talking"); } } public class Dog extends Animal { public String breed; public Dog(){ super(); breed = “Mutt”; } public Dog(String name) { super(true, name); breed = “Mutt”; } public Dog(boolean pet, String name, String breed) { super(pet, name); this. reed = breed; } public void move() { System. out. println("Frolicking forward"); } public void talk() { System. out. println("bark"); } } public class Wolf extends Dog { public Wolf(){ super(false, "no. Name“, “wolf”); } public void move() { System. out. println("running intently"); } public void stalk() { System. out. println("stalking my prey"); } public void talk() { System. out. println("howl"); super. talk(); } } public class main. Animal { public static void main(String[] args) { Animal an_x = new Animal(); System. out. println(an_x. name); System. out. println(an_x. isa. Pet); an_x. sleep(); an_x. talk(); Dog a_dog = new Dog("Spot"); System. out. println(a_dog. name); System. out. println(a_dog. isa. Pet); a_dog. sleep(); a_dog. talk(); a_dog. move(); } } Wolf a_wolf = new Wolf(); System. out. println(a_wolf. name); System. out. println(a_wolf. isa. Pet); a_wolf. sleep(); a_wolf. talk(); a_wolf. move(); a_wolf. stalk();
![Coolness Animal anarr new Animal3 anarr0 anx anarr1 adog anarr2 awolf Coolness: Animal[] an_arr = new Animal[3]; an_arr[0]= an_x; an_arr[1] = a_dog; an_arr[2] = a_wolf;](https://slidetodoc.com/presentation_image_h2/ad67ff3ceb7a6392f1ae88e214006fc5/image-12.jpg)
Coolness: Animal[] an_arr = new Animal[3]; an_arr[0]= an_x; an_arr[1] = a_dog; an_arr[2] = a_wolf; for (int i = 0; i < 3; i++) { an_arr[i]. talk(); if (an_arr[i]. isa. Pet) { System. out. println(an_arr[i]. name); } } // what gets printed? // could I do an_arr[i]. breed? (breed is a field in the dog class) // can I do an_arr[1]. breed?
![Only methods and fields in superclass can be accessed automatically Animal anarr new Only methods and fields in superclass can be accessed automatically: Animal[] an_arr = new](https://slidetodoc.com/presentation_image_h2/ad67ff3ceb7a6392f1ae88e214006fc5/image-13.jpg)
Only methods and fields in superclass can be accessed automatically: Animal[] an_arr = new Animal[3]; an_arr[0]= an_x; an_arr[1] = a_dog; an_arr[2] = a_wolf; for (int i = 0; i < 3; i++) { an_arr[i]. talk(); if (an_arr[i]. isa. Pet) { System. out. println(an_arr[i]. name); } } Dog tempd = (Dog)an_arr[1]; System. out. println(tempd. breed);

Overriding public class Musician { public void play() { . . . Musician musician = new Guitarist(); musician. play(); System. out. println(“silence”); } } public class Drummer extends Musician { public void play() { System. out. println(“boom”); What is the output? } } twang public class Guitarist extends Musician { public void play() { System. out. println(“twang”); } }

Overriding public class Musician { public void play() { System. out. println(“silence”); } . . . Musician musician = new Guitarist(); musician. play(); musician = new Drummer(); musician. play(); } public class Drummer extends Musician { public void play() { System. out. println(“boom”); } What is the output? } public class Guitarist extends Musician { public void play() { System. out. println(“twang”); super. play(); } } twang silence boom

Will this work? public class Animal { public boolean isa. Pet; public String name; public boolean isa. Wolf; } public class Dog extends Animal { boolean isa. Dog = true; public Dog(){ super(); } public Animal() { public Dog(String name) { this(true, "Fred", false); super(true, name); } } public Animal(boolean pet, String name) { public Dog(boolean pet, String name) { this(pet, name, false); super(pet, name); } } public Animal(boolean pet, String name, boolean isawolf) { public void move() { isa. Pet = pet; System. out. println("Going forward"); this. name = name; } this. isa. Wolf = isawolf; public void talk() { } System. out. println("bark"); public void sleep() { } System. out. println("Animal is sleeping"); } } public void talk() { System. out. println("talking"); } How can we fix this? public class Wolf extends Dog { public Wolf(){ super(false, "no. Name", true); } public void move() { System. out. println("running intently"); } public void stalk() { System. out. println("stalking my prey"); } public void talk() { System. out. println("howl"); super. talk(); } } … Wolf x = new Wolf(); Animal x = new Wolf();

public class Animal { public boolean isa. Pet; public String name; public boolean isa. Wolf; } public class Wolf extends Dog { public Wolf(){ super(false, "no. Name", true); } public void move() { System. out. println("running intently"); } public void stalk() { System. out. println("stalking my prey"); } public void talk() { public Dog(boolean pet, String name, boolean isawolf) { System. out. println("howl"); super(pet, name, isawolf); super. talk(); } } public void move() { } System. out. println("Going forward"); } public void talk() { System. out. println("bark"); … } Wolf x = new Wolf(); Animal x = new Wolf(); public class Dog extends Animal { boolean isa. Dog = true; public Dog(){ super(); } public Dog(String name) { super(true, name); } public Dog(boolean pet, String name) { super(pet, name); } public Animal() { this(true, "Fred", false); } public Animal(boolean pet, String name) { this(pet, name, false); } public Animal(boolean pet, String name, boolean isawolf) { isa. Pet = pet; this. name = name; this. isa. Wolf = isawolf; } public void sleep() { System. out. println("Animal is sleeping"); } public void talk() { System. out. println("talking"); } }

Public/Private and inheritance • Public: • Anything in the superclass that is declared public is available to all subclasses (and everything else) • Private – only the superclass has access to it • Subclasses don’t have access

public class Animal { public boolean isa. Pet; public String name; public Animal() { this(true, "Fred", false); } public Animal(boolean pet, String name) { this(pet, name, false); } public void talk() { System. out. println("talking"); } } This works public class Dog extends Animal { boolean isa. Dog = true; public Dog(){ super(); } public Dog(String name) { super(true, name); } public Dog(boolean pet, String name) { super(pet, name); } public void talk() { System. out. println(name+“ says bark"); } }

public class Animal { public boolean isa. Pet; private String name; public Animal() { this(true, "Fred", false); } public Animal(boolean pet, String name) { this(pet, name, false); } public void talk() { System. out. println("talking"); } } public class Dog extends Animal { boolean isa. Dog = true; public Dog(){ super(); } public Dog(String name) { super(true, name); } public Dog(boolean pet, String name) { super(pet, name); } public void talk() { System. out. println(name+“ says bark"); } } This doesn’t work – use a getter

public class Animal { public boolean isa. Pet; private String name; public Animal() { this(true, "Fred", false); } public Animal(boolean pet, String name) { this(pet, name, false); } public String get. Name() { return(name); } public void talk() { System. out. println("talking"); } } This works public class Dog extends Animal { boolean isa. Dog = true; public Dog(){ super(); } public Dog(String name) { super(true, name); } public Dog(boolean pet, String name) { super(pet, name); } public void talk() { System. out. println(get. Name()+“ says bark"); } }

How many problems do you see? public class Circle { public class B extends Circle { double radius; double length; public Circle(double radius) { public B(double radius, double length) { radius = radius; Circle(radius); } } public double get. Radius() { public double get. Area() { return radius; return get. Area() * length; } public double get. Area() { return radius * Math. PI; } }

Abstract Methods • Suppose you have many class behaviors that have a common first step and a common last step • The middle step is different • Wouldn’t it be nice if you could write this code once: public void common. Behavior() { //. . . code that does first step do. Middle. Step(); //. . . code that does last step }

You can! public void common. Behavior() { //. . . code that does first step do. Middle. Step(); //. . . code that does last step } public abstract void do. Middle. Step();

Abstract Classes • Declared using the abstract keyword • abstract class • Cannot be instantiated (can’t make an object from this class) • Must be a superclass to other classes • fields, methods and constructors are accessed in the same way as with the other subclasses. • abstract methods • methods without any implementation • must be overridden by a subclass // forces you to write a definition of the method in the subclass • Abstract methods can be implemented within the abstract class • A class must be abstract if it has abstract methods

What is output? abstract class Animal { public boolean isa. Pet; public String name; public Animal() { this(true, "Fred"); } public Animal(boolean pet, String name) { isa. Pet = pet; this. name = name; } public String get. Name() { return name; } abstract void talk(); public void talking() { System. out. print(“The animal says "); talk(); System. out. println(“ and then it is quiet. ”) } } public class Dog extends Animal { boolean isa. Dog = true; public Dog(){ super(); } public Dog(String name) { super(true, name); } public Dog(boolean pet, String name) { super(pet, name); } public void talk() { System. out. println("bark"); } } public class Main{ public static void main(String[] args) { Dog a_dog = new Dog("Spot"); a_dog. talking(); } } … The animal says: bark and then it is quiet

Which of these is legal? class A { abstract void unfinished() { A } public class abstract A { abstract void unfinished(); } D abstract class A { void unfinished(); } E } B C class A { abstract void unfinished(); } abstract class A { void unfinished() { } }

Interfaces • contains only constants and abstract methods • Unlike abstract classes they cannot have fields (properties) or implemented methods (methods with code in them)

Define an Interface To distinguish an interface from a class, Java uses the following syntax to define an interface: public interface Interface. Name { constant declarations; //constants!! Things you give a value to and never touch again other //than to use the value method signatures; } Example: public interface Edible { /** Describe how to eat */ public abstract String how. To. Eat(); public abstract String how. To. Drink(); public abstract Boolean is. Edible(Food fooditem); } 29 29

Why Interfaces? • Remember, all subclasses of an interface must implement (make code for) each method in the interface

Laziness • All fields in an interface MUST BE public static (e. g. , constants) • All methods MUST BE public abstract For this reason, these modifiers can be omitted, as shown below: • A constant defined in an interface can be accessed using syntax Interface. Name. CONSTANT_NAME • e. g. , T 1. K 31 31

public interface Animal. Interface { abstract public String talks(); abstract public String eats(); abstract public String moves(); } class Cat implements Animal. Interface { public Cat() { } public String talks(){ return("meow"); } public String eats(){ return("Eats mice"); } public String moves(){ return("prowls"); } } public class Bunny implements Animal. Interface{ public Bunny() { } public String talks(){ return("no idea"); } public String eats(){ return("Eats hay"); } public String moves(){ return("hops"); } } public class Cow implements Animal. Interface{ public Cow() { } public String talks(){ return("moo moo"); } public String eats(){ return("Eats grass"); } public String moves(){ return("rarely runs"); } } public static void main(String[] args) { Animal. Interface[] arr = new Animal. Interface[3]; arr[0] = new Cow(); arr[1] = new Cat(); arr[2] = new Bunny(); for (Animal. Interface x: arr) { System. out. println(x. talks()); System. out. println(x. moves()); System. out. println(x. eats()); } }

Using interfaces • Interfaces are not part of the class hierarchy • A class may implement many different interfaces • Polymorphism still holds • An instance of class X that implements interface Y can be used as the base interface type: Y my. Y = new X();

public interface Animal. Interface { abstract public String talks(); abstract public String eats(); abstract public String moves(); } public class Cow implements Animal. Interface, Farm. Things{ public Cow() { } public String talks(){ return("moo moo"); } public String eats(){ public interface Farm. Things { return("Eats grass"); abstract public boolean isa. Farm. Tool(); } abstract public String produces(); public String moves(){ } return("rarely runs"); } public boolean isa. Farm. Tool(){ return false; } public String produces(){ return "milk"; }