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
Java Tutorials:
OOP Concepts
Classes and Objects
tutorialspoint:
Udemy
Lecture 26: Inheritance (14:00) Machine Car start stop, wipeWindShield, override, Source -> Override in Eclipse, @Override, private not inherited, protected
Lecture 28: Interfaces (19:14) generate constructor using fields, Machine, Person, bank account analogy
Lecture 30: Polymorphism (10:03) Plant Tree grow shedLeaves, child can be used anywhere you could use parent
Bucky's Room
49 - Inheritance (9:24) tuna potpie food eat, sub / super, override, has to be public to be inherited (actually could also be protected)
55 - Introduction to Polymorphism (8:20) tuna potpie food eat, polymorphic array, loop through
61 - Simple Polymorphic Program (7:58) Animal Dog Fish, for each
Supplemental Resources
Logic Ch 14 OOP
Wikipedia:
Programmr:
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:
Base methods can be overriden in derived classes.
Objects of a derived type can be stored in a container declared as the base type.
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
UML
Object
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