JSON to Javascript Class

Just assign to an instance: static from(json){ return Object.assign(new Student(), json); } So you can do: const student = Student.from({ name: “whatever” }); Or make it an instance method and leave away the assignemnt: applyData(json) { Object.assign(this, json); } So you can: const student = new Student; student.applyData({ name: “whatever” }); It could also be … Read more

Difference between dependency and composition?

The difference can be seen in the two constructors: Dependency: The Address object comes from outside, it’s allocated somewhere else. This means that the Address and Employee objects exists separately, and only depend on each other. Composition: Here you see that a new Engine is created inside Car. The Engine object is part of the … Read more

Uninitialized variables and members in Java

The language defines it this way. Instance variables of object type default to being initialized to null. Local variables of object type are not initialized by default and it’s a compile time error to access an undefined variable. See section 4.12.5 for SE7 (same section still as of SE14) http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5

Static Binding and Dynamic Binding

Your example is dynamic binding, because at run time it is determined what the type of a is, and the appropriate method is called. Now assume you have the following two methods as well: public static void callEat(Animal animal) { System.out.println(“Animal is eating”); } public static void callEat(Dog dog) { System.out.println(“Dog is eating”); } Even … Read more

Interesting OOPS puzzle [closed]

Assuming you meant B A C on three lines (plus no typo on main method name): namespace ConsoleApplication1 { public class BaseHome { static BaseHome() { Console.WriteLine(“B”); AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit); } public static void Main() { Console.WriteLine(“A”); } private static void OnProcessExit(object sender, EventArgs e) { Console.WriteLine(“C”); Console.Read(); } } }

call parent constructor in ruby

Ruby doesn’t have constructors, therefore it’s obviously not possible to call them, parent or otherwise. Ruby does have methods, however, and in order to call the parent’s method with the same name as the currently executing method, you can use the super keyword. [Note: super without arguments is a shortcut for passing the same arguments … Read more

What is the most efficient way to initialize a Class in Ruby with different parameters and default values?

The typical way to solve this problem is with a hash that has a default value. Ruby has a nice syntax for passing hash values, if the hash is the last parameter to a method. class Fruit attr_accessor :color, :type def initialize(params = {}) @color = params.fetch(:color, ‘green’) @type = params.fetch(:type, ‘pear’) end def to_s … Read more