Lecture 13

hello again! … after a couple of weeks of complete silence 🙂

…just want to go farther with Java classes of Mr Jhonathan Shewchuk.

it is good to remember that in the last lectures in the series, Lecture 12, there were told many interesting things about Abstract Classes and Java Interfaces, how are they useful and which is the differences between them. By the way …

do you remember the difference between the two?

A Java interface cannot contain methods’ implementations, those are composed only from functions prototypes and “constants”, which in Java are defined as static final and besides this, the big advantage that interfaces brings into play is that classes can inherit from multiple interfaces, in other words they are a good way to bring multiple inheritance in Java.

To get a clearer idea about how this thing works professor Shewchuk gives an example at the beginning of Lecture 13:

there are declared two interfaces: Nukable and Comparable

class SList will inherit from the Abstract class List and will implement Nukable and Comparable.

The functions’ declarations in the interfaces need to be supplied with an implementation in the SList

I think those two lectures are quite important in the whole series because they deal with a concept which is  little bit cumbersome in the world of object oriented languages: multiple inheritance. As professor Shewchuk stressed at the end of lecture 12, this is handled differently in C++ and can led to some problems … notorious problem of diamond inheritance. This is handled in a “cleaner” way in Java and avoids such problems as the reminded one.

But let’s go back to lecture 12 and what is this about … Java Packages (generally known by the programmers having a C background as a sort of header files)

One interesting thing is pointed out at 8:51, how are Java classes embedded into java bytecode or .class files.

At 9:46 there is given part of the explanation. Different classes within the same source file are compiled into different bytecodes, meaning that there are 2 class definitions in one .java file there are generated 2 .class files, like the professor shows:  Nukable.class and SList.class.

So …  another file, like DList.java it can recognize an instance of SList class only the code is compiled, meaning only during run-time, otherwise it claims that “it cannot find Nukable member”.

One more thing … coming back to Abstract classes and interfaces, it is redundant to put abstract keyword in front of the method declarations in the interfaces, those are implicitly abstract, since no implementation is allowed in Java interfaces.

Back to the discussion about classes implementing interfaces and extending abstract classes following things are valid:

SList is a Nukable and a Comparable it can be declared a Nukable variable whose static type is Nukable or Comparable. Their dynamic type cannot be Nukable or Comparable, because those are interfaces.

A rule may emerge out of this: you cannot create an object whose dynamic type is an interface.

15:26 an interface can have sub-interfaces, and sub-interfaces can have multiple sub-interfaces, this means that an interface can indeed inherit from multiple other interfaces … obviously using extends keyword

Finally … at 18:47 .. JAVA PACKAGES:

what is a Java Package? a collection of classes and interfaces which “work together”.

Three characteristics, or advantages, of packages can be distinguished:

1. contain hidden classes that are hidden and not visible outside the package

2. classes within packages can contain members and methods visible only at the package level (I think this is actually a scope in Java, on top of the function scope and class scope)

3. there are no name conflicts if classes with the same name are present in different packages, in this way packages act somehow like some namespaces.

How can packages be used?

via import declaration, this is very useful because it shortens the call to a function inside a package. As professor points out, instead of calling

java.lang.System.out.println

you can import the corresponding package, and then call the function using:

System.out.println

because you already imported the java.lang package:

import java.lang.*;

35:23 interesting to know about package protection, actually this is what I wanted to say by package scope . No need to specify the access type, no protected, private or public keyword has to be specified in front of a class member whose class is part of the package. A class, or mostly a variable having package protection is visible to any other class with which shares the same package. It is good to know that the notion of package identifies with the one of directory. Usually each of the java classes is defined in a separate .java file. A package means the corresponding directory where the classes are located. Package visibility means directory visibility.

At the end of the lecture … good to know about the levels of protection, or visibility in a Java program. What do they signify:

public – visible everywhere

protected – visible only in a subclass

package visibility – means that any member of a class being part of a package, is visible to any other class being part of the same package; this does not have to be explicitly specified in the code, there is no package access specifier.

private – visible only within the same class

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

%d bloggers like this: