Skip to content

Instantly share code, notes, and snippets.

@avermeulen
Last active September 16, 2020 08:57
Show Gist options
  • Save avermeulen/fee5dea7a225b97b4d15fbc35d42983c to your computer and use it in GitHub Desktop.
Save avermeulen/fee5dea7a225b97b4d15fbc35d42983c to your computer and use it in GitHub Desktop.
Java OCA preperations, tips and tricks

Building blocks

To pass the OCA exam your really need to know these Java concepts.

  • Classes & Interfaces

    • methods on classes
    • the affect of access modifiers on classes
    • the affect of interfaces on classes
    • class constructors
  • Classes, Interfaces & Inheritence

    • How to inherit behaviour
    • Reference types vs Object Types
    • Which interface can be used as a reference type
    • What can be casted into what
  • Exceptions

    • How to create an Exception
    • How to use (instantiate & trigger) an Exception
      • Instantiate
      • throw
    • How to handle Exceptions
      • catch
      • throws
    • Types of exceptions

Access modifiers

The access in modifiels are:

  • public
  • private
  • protected
  • default access (no access modifier)

By using access modifiers you can control access to methods and variables.

Access modifier Assessible from here
public inside and outside the class
private only inside the class
protected from anywhere in the same package, in sub class in any package
default (no access modifier) from anywhere in the same package

Can a class inherit a private method

Will this code compile? If not why not?

// in Base.java
package oca;
public class Base {
    private void low(){
    }
}
// in Sub.java
package oca;
public class Sub extends Base {
    void now () {
        low();
    }
}

Can a class inherit a protected method

Will this code compile? If not why not?

// in Base.java
package oca;
public class Base {
    protected void low(){
    }
}
// in Sub.java
package oca;
public class Sub extends Base {
    void now () {
        low();
    }
}

Can a class inherit a default access level method in the same package?

Will this code compile? If not why not?

// in Base.java
package oca;
public class Base {
    void low(){
    }
}
// in Sub.java
package oca;
public class Sub extends Base {
    void now () {
        low();
    }
}

Can a class inherit a default access level method from a class in a different package?

Will this code compile? If not why not?

// in Base.java
package oca;
public class Base {
    void low(){
    }
}
// in Sub.java
package oca.pkg;
public class Sub extends Base {
    void now () {
        low();
    }
}

Can a class inherit a protected method from a class in a different package?

Will this code compile? If not why not?

// in Base.java
package oca;
public class Base {
    protected void low(){
    }
}
// in Sub.java
package oca.pkg;
public class Sub extends Base {
    void now () {
        low();
    }
}

Overloading and Overriding methods

Overloading method

Overloading methods is when a class have methods with the same name but different parameters.

Overriding methods

Overriding methods is when a sub class create it's own copy of a method in a super class - with the same method name, return type & method signature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment