Runtime, Compile time

Polymorphism

1) Method Overloading
More than one method with the same name but different data types of parameters
> Static
> Compile time

2) Method Overriding
Child class Overriding a certain method of the parent class
> Dynamic
> Runtime

Binding

1) Static binding (Simple)
Early binding where methods to call depends on the type of reference and signature of the method
Since static methods cannot be overridden, it is done in compiled time
> compile time

2) Dynamic Binding
Late binding where methods to call depends on the instance type and not variable
Example of an instance variable
  1. class Page {
  2. public String pageName;
  3. // instance variable with public access
  4. private int pageNumber;
  5. // instance variable with private access
  6. }
A method is determined at runtime by the type of object
>RunTime


//early binding:
public create_a_foo(*args) {
 return new Foo(args)
}
my_foo = create_a_foo();

//late binding:
public create_something(Class klass, *args) {
  klass.new_instance(args)
}
my_foo = create_something(Foo);
Types

1) Type Inference
Inferring the type of a variable whose type is not specific.
The compiler is able to infer on what type your referring to event we did not declare it
e.g
ArrayList <String> stringList = new ArrayList<>();
We did not declare the type in the underlined portion yet the compiler is able to figure out what is the typing
>compile time

2) Type Erasure
Removing the types for generics during compilation. Its a translation from generic Java source code back to regular Java code.
>compile time

3) Type checking
Checking if the conversion of type is possible.
Failure will flag it as a compile error.
>compile time

4) Typecasting
> Both
Depending on the situation, if we cast and string to an integer, this will happen during compile time and it will throw an error. However, we can cast the string to an object, the code will be able to compile but it might throw an error during run time if not used properly

Checking
1) Accessibility checking
> Both
Compiling code will try to access inaccessible objects or method
During runtime, JVM also checks the access

Example:
 We have two classes
public class Test1 {
    public static void main(String[] args) throws Exception {
        Test2.hello();
    }
}

public class Test2 {
    static void hello() {
        System.out.println("hello");
    }
}
After compiling, it is ok. But if we rewrite test2 as:
private static void hello() {
    System.out.println("hello");
}
and compile the only test2.java instead of changing test1.class,
running test1 will throw a runtime error


<Prev Parallel                             Next Streams and Functional Prog>

No comments:

Post a Comment