9 Classes continued

Overloading, Encapsulation, Inheritance, Polymorphism

Oracle Academy Section

Section 7: Create a Java Class

  • Lesson 4: Overloading Methods

  • Lesson 5: Object Interaction and Encapsulation

  • Lesson 6: static Variables and Methods

Section 7 Quiz 2 (Lessons 4-6)

Java Foundations Certification Exam Topics

Classes and Constructors

  • Develop code that overloads constructors

Key Resources

  1. Java Tutorials:

    1. OOP Concepts

      1. What Is Inheritance?

      2. What is an Interface?

    2. Classes and Objects

      1. Controlling Access to Members of a Class

    3. Inheritance

    4. Polymorphism

  2. tutorialspoint:

    1. Inheritance

    2. Polymorphism

  3. Udemy

    1. Lecture 26: Inheritance (14:00) Machine Car start stop, wipeWindShield, override, Source -> Override in Eclipse, @Override, private not inherited, protected

    2. Lecture 28: Interfaces (19:14) generate constructor using fields, Machine, Person, bank account analogy

    3. Lecture 30: Polymorphism (10:03) Plant Tree grow shedLeaves, child can be used anywhere you could use parent

  4. Bucky's Room

    1. 49 - Inheritance (9:24) tuna potpie food eat, sub / super, override, has to be public to be inherited (actually could also be protected)

    2. 55 - Introduction to Polymorphism (8:20) tuna potpie food eat, polymorphic array, loop through

    3. 61 - Simple Polymorphic Program (7:58) Animal Dog Fish, for each

Supplemental Resources

Most Important Concepts / Code

Overloading: two or more methods in the same class with the same name but different signatures

Overriding: having the same method names in a base and in a derived class

Dynamic Method Dispatch

Inheritance:

"is a" relationship

Base / Parent / Super

Derived / Child / Sub

Polymorphism:

Components / implications:

  1. Base methods can be overriden in derived classes.

  2. Objects of a derived type can be stored in a container declared as the base type.

  3. Overriden method code will be executed even from a call that seems to be from base type.

public class Animal

{

public void showSpecies()

{

System.out.println("Regular animal");

}

public void makeSound()

{

System.out.println("Grrrrr");

}

}

public class Dog extends Animal

{

public void showSpecies()

{

System.out.println("Dog");

}

public void makeSound()

{

System.out.println("Woof");

}

}

public class Cat extends Animal

{

public void showSpecies()

{

System.out.println("Cat");

}

public void makeSound()

{

System.out.println("Meow");

}

}

public class Program

{

public static void main(String[] args)

{

Cat felix = new Cat();

Dog fido = new Dog();

Animal[] myPets = { felix, fido };

for (Animal anAnimal : myPets)

{

anAnimal.showSpecies();

anAnimal.makeSound();

}

}

}

Hour 1

  • Quiz and Concepts Review

  • Base / Derived

  • Overload / Override

    • method signature

  • Sample program http://ideone.com/9PiHjl

  • Breakpoint Debug inspect derived object

  • super

  • Derived constructor

  • Polymorphism benefits example: gas pedal

  • Access modifiers

    • private - Only the current class will have access

    • protected - Only the current class, subclasses, and same-package classes will have access (default)

    • public - Any class can access

    • Programming joke of the week

    • Why do programmers confuse Halloween and Christmas?

  • Exercises

Hour 2

  • Override constructor

  • this - parameter same as field

  • final - class that cannot be extended, method that cannot be overriden or hidden

  • abstract - no objects

  • interface - model for a class, specifies fields and methods

  • implementation - fulfills interface

  • static - belonging to class, not object

  • "Access in a static way" - reference the class name, not the object name when accessing a static member