Docsity

Java Inheritance and Composition: Debugging Examples, Lecture notes of Java Programming

Partial preview of the text

Download Java Inheritance and Composition: Debugging Examples and more Lecture notes Java Programming in PDF only on Docsity!

super

Car Vehicle

class Vehicle { String brand; String color; double weight; double speed; void move() { System.out.println("The vehicle is moving"); } } public class Car extends Vehicle { String licensePlateNumber; String owner; String bodyStyle; public static void main(String... inheritanceExample) { System.out.println(new Vehicle().brand); System.out.println(new Car().brand); new Car().move(); } }

Bedroom LivingRoom House

import java.util.HashSet; public class CharacterBadExampleInheritance extends HashSet { public static void main(String... badExampleOfInheritance) { BadExampleInheritance badExampleInheritance = new BadExampleInheritance(); badExampleInheritance.add("Homer"); badExampleInheritance.forEach(System.out::println); } import java.util.HashSet; import java.util.Set; public class CharacterCompositionExample { static Set set = new HashSet<>(); public static void main(String... goodExampleOfComposition) { set.add("Homer"); set.forEach(System.out::println); }

CharacterCompositionExample

HashSet

class IndexOutOfBoundsException extends RuntimeException {...} class ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException {...} class FileWriter extends OutputStreamWriter {...} class OutputStreamWriter extends Writer {...} interface Stream extends BaseStream<T, Stream> {...}

IndexOutOfBoundsException RuntimeException

Cat

class Animal {} class Mammal {} class Dog extends Animal, Mammal {} class Animal {} class Mammal extends Animal {} class Dog extends Mammal {} interface Animal {} interface Mammal {} class Dog implements Animal, Mammal {}

super

public class SuperWordExample { class Character { Character() { System.out.println("A Character has been created"); } void move() { System.out.println("Character walking..."); } } class Moe extends Character { Moe() { super(); } void giveBeer() { super.move(); System.out.println("Give beer"); } } }

Character super

Character move()

super

super

public class CustomizedConstructorSuper { class Character { Character(String name) { System.out.println(name + "was invoked"); } } class Barney extends Character { // We will have compilation error if we don't invoke the constructor explicitly // We need to add it Barney() { super("Barney Gumble"); } } }

ClassCastException

public class CastingExample { public static void main(String... castingExample) { Animal animal = new Animal(); Dog dogAnimal = (Dog) animal; // We will get ClassCastException Dog dog = new Dog(); Animal dogWithAnimalType = new Dog(); Dog specificDog = (Dog) dogWithAnimalType; specificDog.bark(); Animal anotherDog = dog; // It's fine here, no need for casting System.out.println(((Dog)anotherDog)); // This is another way to cast the object } } class Animal { } class Dog extends Animal { void bark() { System.out.println("Au au"); } }

Animal Dog

Animal

Animal

Animal animal = new Animal(); Dog dogAnimal = (Dog) animal;

Dog Animal Dog

ClassCastException

Dog

Dog dog = new Dog();