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

Java code examples and explanations for debugging inheritance and composition in java. It covers topics such as superclass and subclass relationships, method overriding, constructor invocation, and casting. Students can use this document as study notes, lecture notes, summaries, or cheat sheets to understand the concepts of java inheritance and composition.

Typology: Lecture notes

2018/2019

Uploaded on 09/03/2019

vanosward
vanosward 🇺🇬

1 document

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
super
Java Challengers #7: Debugging Java inheritance | JavaWorld https://www.javaworld.com/article/3409071/java-challenger-7-debugging...
1 of 12
8/21/2019, 3:47 PM
pf3
pf4
pf5
pf8
pf9
pfa

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();