15.08.2018

Exam. Java Basic

1. Static import
import static java.lang.Math.PI;
import static java.lang.Math.pow;
//or all the static members of a class:

import static java.lang.Math.*;


2. Java supports multiple type inheritance but not multiple state inheritance. Because interfaces specify only what the class is doing, not how it is doing it. The problem with multiple inheritance is that two classes may define different ways of doing the same thing, and the subclass can't choose which one to pick. See Oracle Java docs

3. What is polymorphism ?
public interface Vegetarian{}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}
Now, the Deer class is considered to be polymorphic since this has multiple inheritance. Following are true for the above examples − A Deer IS-A Animal A Deer IS-A Vegetarian A Deer IS-A Deer A Deer IS-A Object When we apply the reference variable facts to a Deer object reference, the following declarations are legal −
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;
All the reference variables d, a, v, o refer to the same Deer object in the heap.
It makes the code more reusable.
It protects the code by preventing extension.

4. initialization order with inheritance

1a - Parent's static class variable pstatic initialized.
1b - Parent's static initialization block ran.
2a - Child's static initialization block ran.
2b - Child's static class variable cstatic initialized.
3 - Parent's non-static class variable initialized.
4 - Parent constructor ran.
5 - Child's non-static class variable initialized.
6 - Child constructor ran.

5. sout float or double
print 0.0 instead 0.0f

Комментариев нет:

Отправить комментарий