As per definition-
Interface is a kind of contract that a class must perform when it implements the interface.
Why interface can not implement interface ?
As we know that interface is a contract or in technical terms we only define methods;we do not implement them. When an interface extends an interface we are actually combining two contract that a class will perform. So if an interface implements an interface, it has to implement methods. This means contract vanishes,so is the virtue of interface !!
Why interface can not extend class ?
If an interface extends class, it will inherit class methods along with their implementation. But as per definition an interface can not contain method implementation.
Why class can not extends interface ?
When class A extends another class B, it inherits all the methods of class B. The class A can choose to override inherited methods or it can also choose to skip overriding.
If a class A extends Interface I, it will inherits all the methods of I, as per interface contracts class A has to override or implements all the methods. But this is against the meaning of "extending". So a class can not extend interface.
Tuesday, January 29, 2008
Very Basic Of Serialization
SERIALIZATION-
Serialization means saving the state of an Object or we can say that freezing the state and write down to a file:
Analogy:
Suppose we have an Object of class BankAccount. At any time Object can give us Net Amount PRESENT in account. In January Lets says balance in BankAccount was 100$. There were two activities in BankAccount:
> Deposit of 500$ in February. Net Amount = 600$
> With drawl of 300$ in March. Net Amount = 300$
> With drawl of 300$ in March. Net Amount = 300$
Now we are in March, The account holder wants to compare Net Amount in February with Net Amount in March but the issue is that we can only get to know PRESENT Net Amount, which is 300$. How to solve this issue?
Solutions
1) Declare one variable say FEB_AMOUNT, save the value of Net Amount in February to FEB_AMOUNT. Later in March we can compare FEB_AMOUNT to PRESENT Net Amount (300$).
Problem : There could be a lot of aspects of BankAccount such as holder name, Bank Name, Deposit Currency, Deposit Date etc which one wants to compare, in this case we may have to declare large number of variable to temporary store the values of different aspects.
2)In February, We can write down the value of all properties of BankAccount in the file. Later in March we can read the file compare it against PRESENT value of BankAccount.
This thing is called Serialization in JAVA, ObjectOutputStream does same, It creates one file and note down the state of the Object in that file in some encrypted form and Later OBjectInputStream can be used to read that.
Points to be remembered:
Requirement : Only those classes which implement Serializable Interface can be serialized. Serializable interface is a market interface which means - no method.
Transient: If we don't want to save any instance properties of class we can simply marked it as transient, Its value will not be written by ObjectOutputStream.
Static: Through Serialization we save the state of an OBJECT, whereas STATIC means CLASS level. so static variable can not be serialized..
Garbage Collector and finalize :
Garbage Collector and finalize :
Analogy:
The day when my room is dirty, I usually call my servant, name JVM, to clean it up. Servant JVM collects junk from every part of the house and collect all the stuff together, then throw them.
Normally the ways JVM cleans room are -
> If servant JVM is not very busy, It first collects all the objects such as paper, junk boxes etc and asks to me to check if there is anything important for me. I pick up whatever I think is usable, and then give permission to servant JVM to throw rest of stuff it collected.
> If the Servant JVM has lots of other things to do, It doesn't ask me to look into the collected junk stuff and throws it considering that everything it collected is waste.
This is the concept behined "GARBAGE COLLECTION". Here -
Room == Classes in Java
Junk stuff == Objects without references
Servant JVM == Gabage Collector
Act of asking me to check the collected junk stuff == Finalize method.
For an Object reference:
Maximum number of time finalize can be called = 1
Minimum number of time finalize can be called = 0
Analogy:
The day when my room is dirty, I usually call my servant, name JVM, to clean it up. Servant JVM collects junk from every part of the house and collect all the stuff together, then throw them.
Normally the ways JVM cleans room are -
> If servant JVM is not very busy, It first collects all the objects such as paper, junk boxes etc and asks to me to check if there is anything important for me. I pick up whatever I think is usable, and then give permission to servant JVM to throw rest of stuff it collected.
> If the Servant JVM has lots of other things to do, It doesn't ask me to look into the collected junk stuff and throws it considering that everything it collected is waste.
This is the concept behined "GARBAGE COLLECTION". Here -
Room == Classes in Java
Junk stuff == Objects without references
Servant JVM == Gabage Collector
Act of asking me to check the collected junk stuff == Finalize method.
For an Object reference:
Maximum number of time finalize can be called = 1
Minimum number of time finalize can be called = 0
Programmatic example { Copyright (c) 2011 http://jovialjava.blogspot.com } |
object initialization in java
SubClass subClass = new SubClass();
1) Static block of super class executes
2) Static block of Subclass executes
3) Inside constructor of subclass
4) call to super constructor
5) call of Object Class constructor
6) Instance Variable of super class executes
7) init blocks of super class executes,in the order in which they appear
8) Super class constructor executes
9) Instance Variable of Subclass are initialized
10) Init blocks of subclass executes, in the order in which they appear
11) Subclass constructor executes
1) Static block of super class executes
2) Static block of Subclass executes
3) Inside constructor of subclass
4) call to super constructor
5) call of Object Class constructor
6) Instance Variable of super class executes
7) init blocks of super class executes,in the order in which they appear
8) Super class constructor executes
9) Instance Variable of Subclass are initialized
10) Init blocks of subclass executes, in the order in which they appear
11) Subclass constructor executes
Encapsulation
ENCAPSULATION :
Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.
Analogy:
Let's say we had one box containing one cake. There were 4 guys who wanted to eat that cake. If we kept the box open, any one could have eaten it,result would have been - No cake for others. How this situation was avoided ?
> we hired one person (guard), name getter. The responsibility of the person was to provide exact duplicate copy of the cake.
> we put a lock on the class and gave the key to guard. so no one can directly eat the cake, one has to ask getter for cake.
Bingo ! problem solved ??, Not yet; there was another issue that happened - I got the copy of cake and found that it was not sweet enough. I added the sugar and asked the guard to replace this cake with original one. Guard said - "that's not my duty". So we took another step:
> we hired one more person (guard), name setter. The responsibility of the setter was to replace the original cake.
I gave the new cake, with enough sweetness, to setter and he replaced it. Problem Solved ??, Not yet, one guy mixed the poison into cake and asked the setter to replace it.
So we were in problem again, so we took anothe step, we gave addition responsbility to setter -
> Test the cake before replacing. Replace it if and only if it passes certain test.
OK !! This is the concept behined ENCAPSULATION.In Technical terms -
>Box act as Class
>Cake act as Field of the class
>guards act as public methods of the class
>responsibilities of guards act as action performed by methods
So Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.
/*Encapsulation - This program has been build to illustrate the concept of encapsulation. Copyright (C) 2010 http://jovialjava.blogspot.com. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/. */
Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.
Analogy:
Let's say we had one box containing one cake. There were 4 guys who wanted to eat that cake. If we kept the box open, any one could have eaten it,result would have been - No cake for others. How this situation was avoided ?
> we hired one person (guard), name getter. The responsibility of the person was to provide exact duplicate copy of the cake.
> we put a lock on the class and gave the key to guard. so no one can directly eat the cake, one has to ask getter for cake.
Bingo ! problem solved ??, Not yet; there was another issue that happened - I got the copy of cake and found that it was not sweet enough. I added the sugar and asked the guard to replace this cake with original one. Guard said - "that's not my duty". So we took another step:
> we hired one more person (guard), name setter. The responsibility of the setter was to replace the original cake.
I gave the new cake, with enough sweetness, to setter and he replaced it. Problem Solved ??, Not yet, one guy mixed the poison into cake and asked the setter to replace it.
So we were in problem again, so we took anothe step, we gave addition responsbility to setter -
> Test the cake before replacing. Replace it if and only if it passes certain test.
OK !! This is the concept behined ENCAPSULATION.In Technical terms -
>Box act as Class
>Cake act as Field of the class
>guards act as public methods of the class
>responsibilities of guards act as action performed by methods
So Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.
/*Encapsulation - This program has been build to illustrate the concept of encapsulation. Copyright (C) 2010 http://jovialjava.blogspot.com. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/. */
Learning Java with Real life Example
Hi,
I am a very frequent user to www.javaranch.com , and I like to help, and like to seek help from my fellow participant there. Most of time while replying to any post, which I able to solve, I feel that coding is hardly an issue, the issue is concept?
I have a belief that any programing concept can be learn with reference to our real life and the language we speak. Although with the introduction of Java 5, Learning java is somehow up to some extent has been replaced by Burning Java in your mind, but I still believe that we can apply "Power Of Visualization".
I always follow certain guidelines while making any software solution, Until now I have not faced any problem so I can safely assume that at least it works up to some extent-
> Decouple : Always divide the whole problem into different module. For example :- If I have to find factorial of a number. The I will probably divide into 4 module - Check the validity of number, compute the factorial of the number, display the result, and a module while will give call to other three modules in sequence.
> Clean Coding : I try my best so that none of my Java class has more than 150-200 lines. I also try to put comment everywhere i code a complex logic.
> Focus and target : I draw modular diagram on a piece of paper and set up small-small targets and deadline. I tried to stick to time line.
> Precision : A coder has to be 100% precise. There shouldn't be any scope of error.
> Back up : Whenever I write any utility class or any code which i think that it may be useful in future, I keep it in a separate folder. After 4 years of coding experience I have a lot of thing already coded in my box. This saves my time to recode of what I have already coded.
> Understanding of Business : One must understand what business is behind the software.
Lets come back to Java.Here through this blog I will try to apply power of Visualization on Java.I will try to link every possible concept of Java with some real event, and explain the same.
I am a very frequent user to www.javaranch.com , and I like to help, and like to seek help from my fellow participant there. Most of time while replying to any post, which I able to solve, I feel that coding is hardly an issue, the issue is concept?
I have a belief that any programing concept can be learn with reference to our real life and the language we speak. Although with the introduction of Java 5, Learning java is somehow up to some extent has been replaced by Burning Java in your mind, but I still believe that we can apply "Power Of Visualization".
I always follow certain guidelines while making any software solution, Until now I have not faced any problem so I can safely assume that at least it works up to some extent-
> Decouple : Always divide the whole problem into different module. For example :- If I have to find factorial of a number. The I will probably divide into 4 module - Check the validity of number, compute the factorial of the number, display the result, and a module while will give call to other three modules in sequence.
> Clean Coding : I try my best so that none of my Java class has more than 150-200 lines. I also try to put comment everywhere i code a complex logic.
> Focus and target : I draw modular diagram on a piece of paper and set up small-small targets and deadline. I tried to stick to time line.
> Precision : A coder has to be 100% precise. There shouldn't be any scope of error.
> Back up : Whenever I write any utility class or any code which i think that it may be useful in future, I keep it in a separate folder. After 4 years of coding experience I have a lot of thing already coded in my box. This saves my time to recode of what I have already coded.
> Understanding of Business : One must understand what business is behind the software.
Lets come back to Java.Here through this blog I will try to apply power of Visualization on Java.I will try to link every possible concept of Java with some real event, and explain the same.
Subscribe to:
Posts (Atom)