Seite 1 von 1

Mid-term exam, question 3-a (Linearization)

Verfasst: 19. Jul 2015 00:25
von ddimitrov
Hi everybody,

the linearization of 3-a is {DHWS, S, HP, H, P}, but the super call for the set method is DHWS,S,H,P. Why is HP missing when it is a part of the linearization? Or I did write it down wrong from the black board?!

Re: Mid-term exam, question 3-a (Linearization)

Verfasst: 19. Jul 2015 10:25
von TBA
Probably because HP does not implement the set method.

Re: Mid-term exam, question 3-a (Linearization)

Verfasst: 19. Jul 2015 11:18
von ddimitrov
TBA hat geschrieben:Probably because HP does not implement the set method.
DHWS does not implement it as well, but inherits it. :( Do I miss something?

Re: Mid-term exam, question 3-a (Linearization)

Verfasst: 19. Jul 2015 11:31
von FabianG
Hey,
how do you know, that HashedPassword is not part of set's call chain? I was curious about this as well, so i added some printing to the different set methods and the result was as expected imho. Here is the code i used for that, maybe i did something wrong there:

Code: Alles auswählen

abstract class Password {
  private[this] var password = "password"
  def getPw: String = { return password }
  def set(pwd: String) = {println("set: Password = "+pwd); password = pwd }
}

trait Hash extends Password {
  abstract override def set(pwd: String) = { println("set: Hash"); super.set(pwd.hashCode.toString) }
}

trait Salt extends Password {
  val salt = "xyz"
  abstract override def set(pwd: String) = { println("set: Salt"); super.set(salt + pwd) }
}

class HashedPassword extends Password with Hash {
  override def set(pwd: String) = { println("set: HashedPassword"); super.set(pwd) }
}

class DoubleHashWithSalt extends HashedPassword with Salt with Hash {
  override def set(pwd: String) = { println("set: DoubleHashedWithSalt"); super.set(pwd) }
}

object Main2 extends App {
  val dhws = new DoubleHashWithSalt
  dhws.set("lala")
  println(dhws.getPw)
}
The result i get is:

Code: Alles auswählen

set: DoubleHashedWithSalt
set: Salt
set: HashedPassword
set: Hash
set: Password = -1588597053
-1588597053
Regards

Re: Mid-term exam, question 3-a (Linearization)

Verfasst: 19. Jul 2015 11:41
von TBA
Well, that is not the code from the exam. You added a set method to DoubleHashWithSalt and HashedPassword... Therefore I would say the set method is called in this order: Salt, Hash, Password.

Re: Mid-term exam, question 3-a (Linearization)

Verfasst: 19. Jul 2015 12:04
von FabianG
TBA hat geschrieben:Well, that is not the code from the exam. You added a set method to DoubleHashWithSalt and HashedPassword...
Yes of course, however i just wanted to show that (if implemented) the order would indeed be the same as the linearization path. Since there are implementations missing in the linearization path (in the exam's code) these will of course be skipped. I figure that it sometimes helps (at least myself) to have some kind of visual proof of some sort to get these kind of things into your brain...

Re: Mid-term exam, question 3-a (Linearization)

Verfasst: 19. Jul 2015 12:20
von ddimitrov
Thank you for the clarification and the good example :D
TBA hat geschrieben:Well, that is not the code from the exam. You added a set method to DoubleHashWithSalt and HashedPassword... Therefore I would say the set method is called in this order: Salt, Hash, Password.
I would also say that the call order is Salt -> Hash -> Password, but on the review of the mid-term exam, the solution was DHWS -> S -> H -> P. That's why i was confused, because DHWS doesn't implement set as well but is part of the super call chain.

Re: Mid-term exam, question 3-a (Linearization)

Verfasst: 19. Jul 2015 12:39
von Boddlnagg
Salt -> Hash -> Password was also considered a correct solution in the exam (it's what I wrote), and it definitely makes more sense to me, since DHWS is not really part of the super call chain if you call the method on an instance of DHWS (DHWS is this, not super). I think the solution was presented with DHWS included because the call chain starts with DHWS.

Re: Mid-term exam, question 3-a (Linearization)

Verfasst: 19. Jul 2015 13:11
von ddimitrov
Boddlnagg hat geschrieben:Salt -> Hash -> Password was also considered a correct solution in the exam (it's what I wrote), and it definitely makes more sense to me, since DHWS is not really part of the super call chain if you call the method on an instance of DHWS (DHWS is this, not super). I think the solution was presented with DHWS included because the call chain starts with DHWS.
Very good explanation. Thank you :)