Lectures 11 & 12

it had to come one moment … inheritance (maybe one of the the most important concepts in object -oriented  programming, along with the two other ones: polymorphism and encapsulation )

1:06 today is the day …

2:36 extends  this is what inheritance is all about, in Java. This is the keyword to mark the fact that one class will inherit or can use the methods and the members coming from other classes (well … it depends also on the type of inheritance, public, private, protected)

this will have another corresponding keyword: implements which actually determines a quite opposite behavior, but we’ll see …

a class that inherits from another one (a child class, let’s say, or a derived class, or a subclass) may use the methods from the base one (depending on the type of the inheritance as I already told you) but can also add new ones. This, I think, is the most common type of code reuse paradigm in programming.

5:29 one of the most interesting features of a subclass is that it can modify data in the superclass, or overwrite it, or overriding it

9:57 a question about overriding methods, same name, same returned value but different number of arguments or different types of arguments … it is like having different methods

10:38 can you overwrite constructors? No you cannot

15:32 how to change the constructor of a super class … using super keyword, which as far as I know is Java-specific

15:51 … but this super call has to be the very first line of the derived-class constructor. Why? becasue any other line of code that will be executed in the derived-class constructor must act on an initialized object

16:48 … and Java does not want you to allow you to execute methods on an uninitialized super class object

17:37  what is actually the difference  between using super and directly calling the super class constructor (like it is the case in this example – SList (x))? when using super the code is called in  the derived class, not in the base one, so no new base class object is created.

19:18 Java does not allow multiple inheritance

19:50 super call is incremental, you can call only the super from the immediate base class, there, depending of that super, that one will call the next one, but with super you cannot call a base-class higher than one level

when you use super to override methods, not constructors, from the base-class, it is not mandatory to be the first line in the class

well … we have all of them, public, protected and private (ordered by their degree of restrictivness, from lowest to highest) when would you declare a variable private, or protected ?

this is depending on the project on yo’re working

31:30  a very important thing to bear in mind about Java classes is that they have a single and common inheritance tree, you cannot create independent inheritance branches. All of the Java classes are inheriting from the Object class

ooh! professor a subclass of bourgeoise, my father would be delighted to know this, but both of them subclasses of worker ??!!

33:48 dynamic method lookp – heart of inheritance … I had no idea about this

35: 27 the thing that we discussed one line above refers to the fact it can be written a line of code like

Base b = new Derived();

but vice versa this cannot be done

Memorize this: 🙂

Static type of a variable is just the type you assign to it when declare the variable.

Dynamic type of the variable is the class of the variable that the object references (in this case dynamic type of b is Derived)

When we invoke an overidden method, Java calls the method for the object’s dynamic type regardless of the static type

42:58 why does it (dynamic method lookup) matter? Becasue it provides portability and code independance, as it is the case in the example that Mr Shewchuk is giving, if you want to refine your class (SList, in this case) with a new one (TailList) and you have an algorithm that worked on the previous one, all you have to do to make it work for the new class is to change one line of code.

Lecture 12 – Abstract classes

get ready for something tough: Subtilities of inheritance

1:50 I like the way Jonathan says: we should be able to call … let’s try, let’s call it … and this does not work, the compilation, the linkage, all happens on the table (I mean the board that he’s writing on :))

6:59 the cast operator can work perfectly also in ths case when someone wants  to assign back and forth variables of different types

7:12 Java is a little bit like a jealous lover  lool that was good! you do not have to miss it, just go at 7:12

what happens if Java finds out that I am a lying cheater? really good, this one also, you have to hear all of them
but what actually does this mean? this mean that the program tries to cast a Base type to a Derived one, forcing a more general type to be a specific one, which is not the case
Java is sweety, it trusts me … until run-time

interesting the explanations at 11:20, even if maybe you alreadu knew them :), good to hear them in Java’s context
12:00 a small but important information nicely inserted into the context: every String object has a toString method associated with it

12:53 casts change the static type of an expression– good to know!

15:35 instanceof operator tells the dynamic type of the object, attention this returns TRUE also for subtypes, derived types.

18: 14 Java interfaces … the notorious ones

be careful when using interface word … it may designate many things, methods and classes implementations but also …

but let’s first go through abstract classes 🙂

their biggest purpose is to be extended by some other future classes, in the inheritance tree

22:20 take notice that abstact classes may contain functions implementations (length function)

you (programmers using object-oriented languages) already should know that abstract classes cannot be instanciated, if you didn’t know that’s bad :), really

24:20 … in Java there is a workaround to avoid this, you can declare a reference to an abstract class, but as long it does not have an implementation you cannot make it point to something, it will give a compile-time error

31:00 some rules … A non-abstract class may never contain an abstract method nor may inherit one without providing an implemetation


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: