Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, October 15, 2014

For my High Schooler - Inheritance and Package



At dinner table.

Yash: Today in Java class, we were discussing about packages. Can you please explain?
Me: Sure. You can consider package as any house in a neighborhood.  Each house has its residents – humans, animals, and non-living things. If you scale down by one level, some members of the household have their own rooms while others share rooms.  You can compare those living house as Java Classes, Interfaces, and/or Enums.  Just the way a house may have rooms, a package may have sub-package – packages in a package.
Yash: I got this. But we were also taking about inheritance.
Me: It is pretty simple.
Yash: How?
Me: Let’s create a scenario.  You have parents – one biological mother and one biological father. Correct?
Yash: Correct.
Me: Let’s assume me and your mother separate.  I decide to marry again. Now you have two mothers – one biological and the other one will be your Step Mom.  It is possible that your step Mom may decide to separate after a while and I again marry someone else, now you have three mothers.
Yash: This is complex and crazy and I hope it never happens.
Me: Let’s make it a bit more complex. Your biological mother may also decide to marry someone else. It is also possible that I and one of your step moms may decide to have baby.  To make things worse, you may decide to have baby with one of my spouse’s kid at some point in your life.
Yash: Things may really get ugly here.
Me: Yep. This complexity exists because a baby has two parents. Now assume, if a baby has only one parent. This type of complexity cannot arise. It also allows as many babies as possible from a single parent.
Yash: True.
Me: To avoid this complexity, Java does not allow multiple parents. One parent is allowed.
Yash: Ok.  Who is baby and who is parent in Java?
Me:  In Java, class is the main player. To define relationship of a parent and a baby, we use the keyword – extends.
Yash: How?
Me:  It is simple.  
class B extends A(){
}
Here class A is parent while class B is baby.
In Java terminology, class A is “super class” while class B is “sub class”.

Yash: Hmm…. I suppose, if I want to say that class C is sub class of B then I should write like
                class C extends B(){
                }
Me: Fantastic!!!
Yash: And if class D is also a sub class of B then I can write:
                class D extends B(){
                }
Me: Super!!!
Yash: Does it mean we are creating a hierarchy like class A is super class of class B and classes C & D are sub classes of class B.
 
 


Me: Perfect. This whole concept is called inheritance.
Me: Let’s mix packages and inheritance.  As it is possible that your grandparents may live in a different house and I and you live in one house. It is also possible that classes of same hierarchy belong to different packages. Let’s modify the picture little bit.
 



Yash: So it is possible that a class can live in any package irrespective of the hierarchy.
Me: Yes.
Yash: I have few more questions.
Me: I think, this is good enough for today.
Yash: Okay! Good night.


Monday, June 16, 2014

Blog: For my High Schooler - Compilation vs Interpretation



Yash: Today we were talking about Java. During discussion instructor told that Java is compiled as well interpreted language.

Me: He is perfectly correct.

Yash: But what is Compile and Interpreted?

Me: It is very easy to understand.

Me:  Let’s take a scenario.  Recently elected Prime Minister of India, Narendra Modi receives a letter of congratulations from Japanese Prime Minister, Shinzo Abe.   Japanese PM has letter in Japanese. Indian PM does not understand Japanese, so one of translator in Indian PM Office translates the thank you note and pass on to Modi.
If you notice, translation from Japanese to Hindi has happened prior to letter reaches on the Modi’s desk.

Now consider second scenario. Modi is travelling to Japan. There is meeting of Modi and Abe. As we know Abe does not know Hindi and Modi have no clue of Japanese. So during meeting there will be a translator, who will translate Hindi to Japanese and vice versa in real time. 

In this case, translation is happening in nearly real time.

Yash: Ok, I got it. First scenario is Compilation while second is Interpretation.  
Me: Fantastic. Now my question is why Java has both compilation and interpretation?

Yash: This easy.  Java take benefit of compilation by translating English into something intermediately language.

Me: This intermediate language is called Byte Code.

Yash: Yep. Since we have concept of virtual machine, each platform has its own virtual machine, which acts as real time translator (interpreter), java become platform independent.

Me: Excellent. We will be discussing about Virtual Machine tomorrow.

Friday, June 28, 2013

Book Review: Learning Java



Book Review:  Learning Java by Patrick Niemeyer and Daniel Leuck: Publisher- O'Reilly: ISBN- 13: 978-1-4493-2306-6


Learning Java (Fourth Edition) is book for Java practitioner as reference book. This covers lot of topics.  

This is an excellent book for someone who knows basics of programming. This book is not beginners. This book lacks examples and exercises which may disappoint few people.

Book has 24 chapters covering almost all of basic Java.  The chapter one talks about historical aspects. Second chapter is brief introduction of java but it assumes that reader is aware of programming, OOP, threading etc which is difficult for any beginner.  

Disclaimer: I did not get paid to review this book, and I do not stand to gain anything if you buy the book. I have no relationship with the publisher or the author. I got electronic format of book from publisher for review.

Further reading: 


One can get more information about book and related topics from:

  1. Amazon: http://www.amazon.com/Learning-Java-Patrick-Niemeyer/dp/1449319246
  2. Publisher -- Oreilly http://shop.oreilly.com/product/0636920023463.do

Friday, May 8, 2009

Book Review: Practical API Design: Confessions of a Java Framework Architect

Book Review: Practical API Design: Confessions of a Java Framework Architect by Jaroslav Tulach: Publisher- Apress: ISBN- 13: 978-1-4302-0973-7

Before start reading Practical API Design, I googled and not able to find much of stuff on API design. Only worth while to mention are discussion on Artima forum dated 2005 and a paper by Brian Ellis, Jeffrey Stylos, Brad Myers. Certainly Effective Java Joshua Bloch also discusses the topic. I was surprised and started reading the book.

Once I started the book I was hooked to it. This book is written clearly demarcated three parts: Theory, Design aspects and Advice characterized as Theory & Justification, Practical Design and daily Life.

As confessed by Tulach book covers only Java language specifics but still book is must read for non java professionals.

Book primarily focuses on few topics: Evolve-ability, Backward & forward compatibility, architecture by committee and lessons learnt from NetBeans API development.

The book is serious readers and those who like to read book with notebook and pencil. This book is not easy read which you can read during your lunch. It needs serious time and efforts to read and then grasps. It discusses various aspects of API design with respect to backward & forward compatibility, runtime behavior, testability, separation of API & SPI, mingling with other APIs (here certainly I recommend reads to look into Struts 2.x and Grails architecture, design and source, because these frameworks/API uses third party libraries extensively), interface vs. abstract classes etc.

Tulach has recommended not to rewrite because of breaking old loyal clients. Personally, I also subscribed to this line of thought but certainly Struts 2.x violets this line and still very successful. It has to be studied in details.

Though book is comprehensive but it has not covered few topics:

1. Effect of hard coding of SUID (static final long serialVersionUID =xx)
2. Performance
3. How to make API accessible from different programming languages (say C#)

Tulach is maintaining books web presence at http://www.apidesign.org, I hope he will take care of the different views and uncharted topics in future.

I am hoping for similar book by other veterans of industry both open source & proprietary frameworks and APIs (Eclipse, Spring, Hibernate, jUnit, Glassfish, Ant, Struts, Groovy on the Grails) as well as non Java frameworks and APIs (Ruby on the Rails , AspectDNG, Netron graph library, NAnt).

Practical API Design must be in your bookshelf if you are java architects veterans or aspiring one. It is also a good reference book for non java folks as principles discussed are very much applicable in other languages but must be tweaked to adjust with language specifics.

Disclaimer: I did not get paid to review this book, and I do not stand to gain anything if you buy the book. I have no relationship with the publisher or the author.

Further reading: A competing book is Effective Java by Joshua Bloch

One can get more information about book and related topics from:

1. Book’s web presence at http://apidesign.org
2. Purchase book at Amazon
3. Publisher -- Apress
4. Interview with Author
5. Review at 72 Miles
6. 90th Percentile Blog
7. The Factory Pattern in API Design: A Usability Evaluation by Brian Ellis, Jeffrey Stylos, Brad Myers
8. Discussion on API Design
9. Java Language Specification, Chapter 13: Binary Compatibility