Thursday, May 29, 2008

Basic OO concepts

1. What is abstraction, what is encapsulation? What is the difference?

A: pine tree, apple tree, oak tree....
Abstraction: Tree
They are all trees.
Another way to express the same concepts is generalization and specialization.Capacitor, resister, wire, box, CRT, ...
Encapsulation: television
You don't care what are inside the black box, and how they interact with each other, as long as the television works.
Another way to express the same concepts is information hiding.

2. Why we need to construct a Vector first, then we can call addElement method?

A:

Since addElement(Object) is an instance method of class Vector. If we don't construct a Vector instance first, we cannot call instance method. You may askWhy we define addElement(Object) as instance method?
The answer should be obvious. You might use different Vectors in different parts of your application. Each Vector might add very different Objects. Define addElement as static (class) method does not make any sense.Give you a simple analog, you build a home, which is an instance of Home class, then add a TV to your living room. The addTV() method must be called after you construct your home, in other words, an instance method. Do you want you TV to be a class static variable and used by all home owners. No, you don't, and it is also impossible in reality too.

3. When an instance of subclass is created, is an instance of base class created also? How many objects are created during the process?

A:

Truth: FordCar ISA or "is a" Car.Think:
Which one is generalization/specialization? (Answer: Car is generalization)
Which one should be the base/sub class? (Answer: Car should be the base class)Fact: A FordCar was just made.Questions:
true/false? A Car was just made. (Answer: true)
How many objects (Cars) were created during this process? (Answer: one)
~~~~~~~~~~~~~~~~~~~~~There is only one object, but it contains all of the variables for the entire hierarchy. The reason being that the execution of a constructor causes calls to the constructors all the way back up the hierarchy to java.lang.Object - as each constructor executes, it gets the memory and sets the variables as required for that class.Where I can learn more about this topic?Reading some good Java / C++ / OO books, pay attention to the inheritance (or generalization/specialization) part. This is a big and extremely important topic. Yes, it is more important than learning Java. "Nothing can replace the technique of reading a good book from front to back." Here is an excellent free book:
Bruce Eckel's Thinking in Java, 2nd Edition

4. When we should use static methods, when we should use instance methods? Which one is better?

A: This should depend on what problem you need to solve. If the method is a long complicated scientific calculation, static method is the right way to do it. It makes the method available and easy to be used everywhere. That is exactly what Java Math class does. Go there, and see it.If the method is an individual behavior, such as putOnMakeUp() , it is very cultural/personal dependent. Putting it as static method would be almost meaningless. Of course, you can make a static putOnMakeUp() work fine if you are willing to pass 15 parameters to it, or pass a large struct including all cultural/personal information needed. Ah, we drive our time machine and go back to that magnificent procedural/structural paradigm era...

No comments: