Java SE 8 OCA exam: Chapter 5

This is one of the most important chapters as it’s covers the principles of the OOP.

Note:

1. As you know, a Java fle can have many classes but at most one public class. In fact, it
may have no
public class at all. One feature of using the default package private modifier
is that you can define many classes within the same Java file

2.  The rules for applying class access modifiers are identical for interfaces. There can be at
most one
public class or interface in a Java file. Like classes, top-level interfaces can also
be declared with the
public or default modifiers.

3.  In Java, all classes inherit from a single class, java.lang.Object.  Furthermore, java.lang.Object is the only class that doesn’t have any parent classes. When we create new class it will automatically add extends java.lang.Object even thought it’s invisible:
public class Zoo {
}
public class Zoo extends java.lang.Object {
}

4.

Like the this() command that you saw in Chapter 4, the super() command may only
be used as the frst statement of the constructor. For example, the following two class defnitions will not compile:
public class Zoo {
public Zoo() {
System.out.println(“Zoo created”);
super(); // DOES NOT COMPILE
}
}

5. Super tricky. If the subclass has auto constructor it will call it’s super class constructor first, which may be also auto generated. However, when child’s auto constructor is calling the super class’s constructor AND super class’s constructor already have constructor with different signature (ex. public Father(int a)) then the child’s constructor will not compile. Because it’s superclass’s constructor becomes undefined. JAVA WTF?

Introducing Class Inheritance

Inheritance is the process by which the new child subclass automatically includes any
public or protected primitives, objects, or methods defined in the parent class.

Java supports single inheritance, by which a class may inherit from only one direct parent class.

Java also supports multiple levels of inheritance, by which one class may extend
another class, which in turn extends another class. You can extend a class any number of
times, allowing each descendant to gain access to its ancestor’s members.

Java does allow one exception to the single inheritance rule: classes may implement multiple interfaces, as you’ll see later in this chapter.

It is possible in Java to prevent a class from being extended by marking the class with
the
final modifer. If you try to defne a class that inherits from a final class, the compiler
will throw an error and not compile.

Java allows only one public class per file.

For the OCA exam, you should only be familiar with public and default
package-level class access modifiers, because these are the only ones that
can be applied to top-level classes within a Java file. The
protected and
private modifiers can only be applied to inner classes, which are classes
that are defined within other classes, but this is well out of scope for the
OCA exam.

Defining Constructors

In Java, the first statement of every constructor is either a call to another constructor
within the class, using
this(), or a call to a constructor in the direct parent class, using super().

Following three class and constructor definitions are equivalent, because the compiler will automatically convert them all to the last example:

public class Donkey {
}
public class Donkey {
public Donkey() {
}
}
public class Donkey {
public Donkey() {
super();
}
}

In this example no constructor is defned within the Elephant class, so the compiler tries
to insert a default no-argument constructor with a
super() call, as it did in the Donkey
example. The compiler stops, though, when it realizes there is no parent constructor that
takes no arguments. 

public class Mammal {
public Mammal(int age) {
}
}
public class Elephant extends Mammal { // DOES NOT COMPILE
}

In this example, we must explicitly defne at least one constructor, as
in the following code:

public class Mammal {
public Mammal(int age) {
}
}
public class Elephant extends Mammal {
public Elephant() { // DOES NOT COMPILE
}
}

This code still doesn’t compile, though, because the compiler tries to insert the noargument super() as the frst statement of the constructor in the Elephant class, and there
is no such constructor in the parent class. We can fx this, though, by adding a call to a parent constructor that takes a fxed argument:
public class Mammal {
public Mammal(int age) {
}
}
public class Elephant extends Mammal {
public Elephant() {
super(10);
}
}
This code will compile because we have added a constructor with an explicit call to a
parent constructor. Note that the class
Elephant now has a no-argument constructor even
though its parent class
Mammal doesn’t. Subclasses may defne no-argument constructors
even if their parent classes do not, provided the constructor of the child maps to a parent
constructor via an explicit call of the
super() command.
You should be wary of any exam question in which the parent class defnes a constructor
that takes arguments and doesn’t defne a no-argument constructor. Be sure to check that
the code compiles before answering a question about it.

***Reviewing Constructor Rules

Let’s review the rules we covered in this section.
Constructor Definition Rules:
1. The first statement of every constructor is a call to another constructor within the class
using
this(), or a call to a constructor in the direct parent class using super().
2. The super() call may not be used after the first statement of the constructor.

3. If no super() call is declared in a constructor, Java will insert a no-argument super()
as the first statement of the constructor.
4. If the parent doesn’t have a no-argument constructor and the child doesn’t define any
constructors, the compiler will throw an error and try to insert a default no-argument
constructor into the child class.
5. If the parent doesn’t have a no-argument constructor, the compiler requires an explicit
call to a parent constructor in each child constructor.
Make sure you understand these rules; the exam will often provide code that breaks one
or many of these rules and therefore doesn’t compile.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s