Things to Know Part 5

Subclasses and Superclasses:

        -subclasses extend a superclass by using the keyword extends

                public class SuperClass{}

                public class SubClass extends SuperClass{}

                //SubClass gets all of SuperClass's public and protected members

        -superclasses can be instantiated as subclasses but not the other way around

                SuperClass super = new SubClass(); //OKAY

                SubClass sub = new SuperClass(); //NOT OKAY

Method Overriding:

        -use super.method() to call same method of superclass

        -use super() to call a superclass constructor.

-final methods cannot be overridden.        

Method Hiding:

        -when a subclass overrides a static method superclass with a static method with the same signature

        -allows writing a subclass method without calling the superclass method.


Variable Hiding:

        -defining a variable in a subclass with the same name and type as the superclass.

Overriding vs Overloading:

        -overriding a method is when a subclass has a method with the same signature as the superclass.

-overloaded methods have a different signature.

-overloaded methods can happen in the same class.

Abstract Classes:

        -cannot be instantiated

        -cannot be marked final

        -can have abstract and non-abstract methods

        -abstract methods labeled with abstract don't have a body

        -abstract methods cannot be final, protected, or private

        -can have member variables

        -subclasses extending class must define abstract methods

Interfaces:

        -cannot be instantiated.

        -can use default to define a default method body.

-cannot be final

-can only have 0 or more abstract methods not defined by abstract keyword

-methods cannot be final, protected, or private.        

-can have member variables that are public, static, and final.

-inherit interfaces using implements

-inheriting classes must define all interface methods

Polymorphism:

        -ability of an object to take on many forms

        -when a subclass extends a super class and implements from interfaces, an object from the subclass can be used as an object of the superclass and interfaces.

        public class SuperClass {}

        public interface IntFace1 {}

        public interface IntFace2 {}

public class SubClass extends SuperClass implements IntFace1, IntFace2 {}        

        public class Tester {

                public static void main(String[] args){

                        SubClass sc = new SubClass();

                        method1(sc);

method2(sc);

method3(sc);

// sc can be passed into all three methods because of polymorphism

                }

                public static void method1(SuperClass s){}

                public static void method2(IntFace1 i){}

                public static void method3(IntFace2 i){}

        }

Reference Casting:

        -casting from subclass to superclass is automatic

        -casting from superclass to subclass requires an explicit cast

                SuperClass c = new SuperClass();

                SubClass s = (SubClass)c;

        -cannot cast if types are unrelated

        -runtime exceptions with be thrown if type miscast happens

source:

Boyarsky, Jeanne, and Scott Selikoff. OCA Oracle Certified Associate. Study Guide. John Wiley & Sons, 2015.