We have two interfaces and both have same method declaration, if any class implements these two interfaces which interface method will be executed?
package com.util;
public class Test3 implements Inter1, Inter2 {
public void fun(){
System.out.println("manikesh");
}
public static void main(String[] args) {
Test3 t3 = new Test3();
//System.out.println(t3.i);
t3.fun();
}
}
interface Inter1{
void fun();
//int i=6;
}
interface Inter2{
void fun();
//int i=8;
}
Can you guess the answer? Yes you got it right it will print manikesh.
Interfaces do not execute or define any method they just declare it so the implementer class really does not care which interface method it is.
All what compiler looks for is whether the method declared in interface has been defined in class or not.Both the interfaces can have any number of same type of method and implementer class has to define only one method.
But if we have same type of instance variable, can you guess the answer if I uncomment the commented code.
Can you guess the answer now? No you guessed it wrong it will throw error saying variable “I” is ambiguous because I has one values associated with that and both the interfaces cannot use the same.
Let’s see how we can write a code to accommodate such situation. I am just modifying the main method rest code would remain same.
public static void main(String[] args) {
//Test3 t3 = new Test3();
Inter1 I1 = new Test3();
Inter2 I2 = new Test3();
I1.fun();
I2.fun();
System.out.println(I1.i);
System.out.println(I2.i);
//t3.fun();
}
This will solve our problem, same we can do for any number of methods.
Sunday, April 11, 2010
Exception
Exception:
In our real life, we often say that exceptions are everywhere, mean something really unusual which does not take place often and come rarely.
No matter how well prepare you are but you might face some problem for which you were not ready and you will say it came exceptionally.
Same thing happens in java programming, it hardly matters how good program you write but you don’t control all, some method might go weird and you will not have idea on that.
In real life if we plan for something then some problem we expect and some will be unexpected, normally we are ready for expected problem like going on outing and carrying medicine, first aid box,extra bed sheet etc, but there might be problem which we dint think of and it will be unexpected like vehicle breaks down completely, heavy rain in mid way or we lost some of our friends etc…
Java defines expected problem as “Checked” and unexpected one as “Unchecked”, these are the two types of exception java has.
For checked exception we write code to handle that as we expect that, something like carrying medicine in outing.
We don’t do anything for unchecked as we dint know this, something like natural disaster or vehicle break down.
How to handle checked exception:
Java provide try/catch block to handle exception, it basically tells complier that you writing some risky program and it might break. Compiler will not do anything in this case.It just knows that you doing something risky and you will handle it if something wrong happens.
“Life is short and we have to take risk but at the same time we should be ready to handle if something goes wrong, that’s what java says, you ‘try’ for risky things but be ready to ‘catch’ it”
try{
risky code goes here///
}catch(Exception ex){
}
All risky code you have to write inside try block, if something goes wrong it will be caught meaning control will come to catch block and it will tell what went wrong. Once there is any problem no other statement will be executed inside try block,
try{
Xxxxx – if exception came here rest lines will not be executed and control will go directly to catch section where it tells what the prob. is
Yyyyyy
Zzz
} catch (){}
There is another block called as finally, if you want your statement to get executed, regardless of whether there is an exception is in your program or not then you put that statement in finally block,
try{
xxxx/// if exception came here it will go to catch block
yyyyy
zzzzz
}catch(){
// some statement –this will not get executed until there is an exception in try block
{
finally {
// some statement … this statement will be executed even there is no exception in try block.
We make use of this for closing the DB connections, nullifying the collection object as we have to execute these statements regardless of exception.
}
Note:
1. Except System.exit(0), rest all statement will be executed written inside finally block.
2. Best thing is that, if try or catch block has return statement even though finally will execute.
3. We can have multiple no. of catch block along with finally block.
4. We should make sure subclasses should come before super classes in multiple catch blocks.
Throws and Throw clause:
If you don’t want to handle an exception you can tell your method to throw it and exception will be taken care by calling method.
Let’s see this in real life…. Suppose for outing you want to use Manikesh’s car and Manikesh’s car has got some problem so while giving the car Manikesh told you that car might give this problem.
Since you are going to use his car now it’s your responsibility to handle that expected problem, Manikesh just told and left, rest all is your job to do.
Same thing happens in java, you can declare your method like this
Public void fun() thorws xyzException
That’s it your job is done now whoever is going to use this method has to take care of this exception.
In some cases if you want to throw some user friendly exception, something which user can understand for such demand java has “throw” clause.
This will help you in throwing your own exception, user defined exception, like below.
Public void fun(){
Throw new Exception(“---“);
}
In above line I mentioned about some unexpected problems like nature disaster or vehicle breaks down, in java language we call it as run time exception, this is something compiler does not care about.
This also called as “Unchecked” exception, you can handle these exceptions as well but compiler really does not care about this.
Points to note:
1. A method can throw any number of exceptions.
2. Exceptions are polymorphic is nature.
3. You can declare exception using super type and can throw with sub types as well but vice versa is not possible.
Eg. public void fun() throws grandfatherException{
throw fatherException,
throw grandSonException
etc..
}
4. You can catch exception using super type but it’s advisable to catch specific exception.
5. Multiple catch blocks should be in ascending order, meaning subtype and then super type.
6. Making use of catch or finally block without try is impossible.
7. No code should be put between try and catch block.
In our real life, we often say that exceptions are everywhere, mean something really unusual which does not take place often and come rarely.
No matter how well prepare you are but you might face some problem for which you were not ready and you will say it came exceptionally.
Same thing happens in java programming, it hardly matters how good program you write but you don’t control all, some method might go weird and you will not have idea on that.
In real life if we plan for something then some problem we expect and some will be unexpected, normally we are ready for expected problem like going on outing and carrying medicine, first aid box,extra bed sheet etc, but there might be problem which we dint think of and it will be unexpected like vehicle breaks down completely, heavy rain in mid way or we lost some of our friends etc…
Java defines expected problem as “Checked” and unexpected one as “Unchecked”, these are the two types of exception java has.
For checked exception we write code to handle that as we expect that, something like carrying medicine in outing.
We don’t do anything for unchecked as we dint know this, something like natural disaster or vehicle break down.
How to handle checked exception:
Java provide try/catch block to handle exception, it basically tells complier that you writing some risky program and it might break. Compiler will not do anything in this case.It just knows that you doing something risky and you will handle it if something wrong happens.
“Life is short and we have to take risk but at the same time we should be ready to handle if something goes wrong, that’s what java says, you ‘try’ for risky things but be ready to ‘catch’ it”
try{
risky code goes here///
}catch(Exception ex){
}
All risky code you have to write inside try block, if something goes wrong it will be caught meaning control will come to catch block and it will tell what went wrong. Once there is any problem no other statement will be executed inside try block,
try{
Xxxxx – if exception came here rest lines will not be executed and control will go directly to catch section where it tells what the prob. is
Yyyyyy
Zzz
} catch (){}
There is another block called as finally, if you want your statement to get executed, regardless of whether there is an exception is in your program or not then you put that statement in finally block,
try{
xxxx/// if exception came here it will go to catch block
yyyyy
zzzzz
}catch(){
// some statement –this will not get executed until there is an exception in try block
{
finally {
// some statement … this statement will be executed even there is no exception in try block.
We make use of this for closing the DB connections, nullifying the collection object as we have to execute these statements regardless of exception.
}
Note:
1. Except System.exit(0), rest all statement will be executed written inside finally block.
2. Best thing is that, if try or catch block has return statement even though finally will execute.
3. We can have multiple no. of catch block along with finally block.
4. We should make sure subclasses should come before super classes in multiple catch blocks.
Throws and Throw clause:
If you don’t want to handle an exception you can tell your method to throw it and exception will be taken care by calling method.
Let’s see this in real life…. Suppose for outing you want to use Manikesh’s car and Manikesh’s car has got some problem so while giving the car Manikesh told you that car might give this problem.
Since you are going to use his car now it’s your responsibility to handle that expected problem, Manikesh just told and left, rest all is your job to do.
Same thing happens in java, you can declare your method like this
Public void fun() thorws xyzException
That’s it your job is done now whoever is going to use this method has to take care of this exception.
In some cases if you want to throw some user friendly exception, something which user can understand for such demand java has “throw” clause.
This will help you in throwing your own exception, user defined exception, like below.
Public void fun(){
Throw new Exception(“---“);
}
In above line I mentioned about some unexpected problems like nature disaster or vehicle breaks down, in java language we call it as run time exception, this is something compiler does not care about.
This also called as “Unchecked” exception, you can handle these exceptions as well but compiler really does not care about this.
Points to note:
1. A method can throw any number of exceptions.
2. Exceptions are polymorphic is nature.
3. You can declare exception using super type and can throw with sub types as well but vice versa is not possible.
Eg. public void fun() throws grandfatherException{
throw fatherException,
throw grandSonException
etc..
}
4. You can catch exception using super type but it’s advisable to catch specific exception.
5. Multiple catch blocks should be in ascending order, meaning subtype and then super type.
6. Making use of catch or finally block without try is impossible.
7. No code should be put between try and catch block.
Sunday, March 7, 2010
Why System.out.println()?
I wrote a programe which will let you knwo why we always use System.out.print() to print something in java. This simple program will give you clear idea...
package com.util;
import java.io.*;
class Print extends PrintStream
{
public void it(String a) // this method is same as print()
{
super.println(a);
}
Print()
{
super(System.out);
}
}
class Please
{
static Print print=new Print();
}
public class Test1 {
public static void main(String[] args) {
Please.print.it("hi");
}
}
Hope this helps ;)
package com.util;
import java.io.*;
class Print extends PrintStream
{
public void it(String a) // this method is same as print()
{
super.println(a);
}
Print()
{
super(System.out);
}
}
class Please
{
static Print print=new Print();
}
public class Test1 {
public static void main(String[] args) {
Please.print.it("hi");
}
}
Hope this helps ;)
Monday, March 1, 2010
Session
Let’s see how web application can be real mess without session.
Manikesh makes a call to one departmental store and order some items; they don’t track user record separately, meaning no user Id for Manikesh, no unique identification to Manikesh. He calls them again and changes the order, since they did not track they could not know what Manikesh ordered earlier, same time Neeraj calls them and orders something. Now the real problem starts as they dint know who ordered what, items got replaced, meaning Manikesh got something which Neeraj ordered and he got something what Manikesh ordered
When client sends a request to server, it looks for requested result and sends it back to the client. It does not know who was the client and from where request came and all. So next time when same client sends request, server does not have any info about that client. This situation was making web application little complicated as HTTP is by design a stateless protocol. This is where session came in to picture.
As an web application server, it might get plenty of request at same time and if client details are not being traced, it’s highly possible that some confusion might take place and proper response might not reach to proper client.To avoid such mess up, Session came into picture.
Session management helps server to track record of each client when ever request came across. It will record all information related to client, what it requested last, how many times same item was request and etc.
A HttpSession object can track record of all conversation happened between multiple request from same client. It lets you track everything what came as request form client throughout the application.
The big question was how a server identifies who was the client and who made request.
Connection between client and server exists only for a single request as HTTP is state less protocol it sends the request, get the response and close off the connection.
Simple step: On every first request from client, server creates and unique session id for that client and client is supposed to send next request along with session id so that server can easily identify the client.
Multiple ways how session ID can be tracked between client and server:
1. Cookie--> it is best and simplest way to track the session ID. When a client sends first request to server , server creates cookie at client machine and store the session id there, next time when
Same client makes request, server looks for the session ID from cookie in request. You have to write a one line code and rest all will be taken care automatically.
HttpSession session = request.getSession();
getSession(Boolean value) this method takes Boolean as parameter, it’s by default true, means it will create session for you if no pre existing session available.\
if you want only pre existing session then pass false, it will give you only existing session, it will not create new one for you.
Note: Cookies are not only meant for session handling, it can be used to exchange the name/value string pairs b/w server and client. Cookies get lost if browser closes of but you can retain it even if browser shuts down.
2. URL Rewriting --> what will happen if someone disables the cookie? How we will track session in this case. Server has another option as URL rewriting to track session.
In your requested URL you just have to add “ jsessionID=?????????”
Note: You should encode URL before you start sending it for response, and URL rewriting will work only when Cookies are failing.
Eg. response.encodeRedirectURL(“/xxxx.do”);
HttpSession Interface: This interface has got some really helpful methods which will help you to track session.
1. getCreationTime()
2. getLastAccessedTime()
3. setMaxInactiveInterval()
4. getMaxInactiveInterval()
5. invalidate() this method will help you if you want to get rid of session, just call this method as session.invalidate(), it will destroy the session, other way to time out a session is to set in web.xml file.
15
This is all I had to share on session, there is much more info to be shared… hope this helps.
Manikesh makes a call to one departmental store and order some items; they don’t track user record separately, meaning no user Id for Manikesh, no unique identification to Manikesh. He calls them again and changes the order, since they did not track they could not know what Manikesh ordered earlier, same time Neeraj calls them and orders something. Now the real problem starts as they dint know who ordered what, items got replaced, meaning Manikesh got something which Neeraj ordered and he got something what Manikesh ordered
When client sends a request to server, it looks for requested result and sends it back to the client. It does not know who was the client and from where request came and all. So next time when same client sends request, server does not have any info about that client. This situation was making web application little complicated as HTTP is by design a stateless protocol. This is where session came in to picture.
As an web application server, it might get plenty of request at same time and if client details are not being traced, it’s highly possible that some confusion might take place and proper response might not reach to proper client.To avoid such mess up, Session came into picture.
Session management helps server to track record of each client when ever request came across. It will record all information related to client, what it requested last, how many times same item was request and etc.
A HttpSession object can track record of all conversation happened between multiple request from same client. It lets you track everything what came as request form client throughout the application.
The big question was how a server identifies who was the client and who made request.
Connection between client and server exists only for a single request as HTTP is state less protocol it sends the request, get the response and close off the connection.
Simple step: On every first request from client, server creates and unique session id for that client and client is supposed to send next request along with session id so that server can easily identify the client.
Multiple ways how session ID can be tracked between client and server:
1. Cookie--> it is best and simplest way to track the session ID. When a client sends first request to server , server creates cookie at client machine and store the session id there, next time when
Same client makes request, server looks for the session ID from cookie in request. You have to write a one line code and rest all will be taken care automatically.
HttpSession session = request.getSession();
getSession(Boolean value) this method takes Boolean as parameter, it’s by default true, means it will create session for you if no pre existing session available.\
if you want only pre existing session then pass false, it will give you only existing session, it will not create new one for you.
Note: Cookies are not only meant for session handling, it can be used to exchange the name/value string pairs b/w server and client. Cookies get lost if browser closes of but you can retain it even if browser shuts down.
2. URL Rewriting --> what will happen if someone disables the cookie? How we will track session in this case. Server has another option as URL rewriting to track session.
In your requested URL you just have to add “ jsessionID=?????????”
Note: You should encode URL before you start sending it for response, and URL rewriting will work only when Cookies are failing.
Eg. response.encodeRedirectURL(“/xxxx.do”);
HttpSession Interface: This interface has got some really helpful methods which will help you to track session.
1. getCreationTime()
2. getLastAccessedTime()
3. setMaxInactiveInterval()
4. getMaxInactiveInterval()
5. invalidate() this method will help you if you want to get rid of session, just call this method as session.invalidate(), it will destroy the session, other way to time out a session is to set in web.xml file.
This is all I had to share on session, there is much more info to be shared… hope this helps.
Saturday, January 16, 2010
Ye Dil Maange More ... Java 1.6
1.Collections framework has something extra now, lets find out
a. Deque- if you expand it then it will be something like this
D + E +Que ==> Double + Ended + Queue =>Add or remove from either side,what an Idea sir ji :)
Its an interface known as double ended queue and extends Queue.
want to know more? ==>http://java.sun.com/javase/6/docs/api/java/util/Deque.html
b. BlockingDeque- This is little advanced Deque which will not allow deque to be
empty while retreiving data and look for space while adding data. Its again an
interface which extends Deque and BlockingQueue interfaces.
want to know more? ==> http://java.sun.com/javase/6 /docs/api/java/util/concurrent/BlockingDeque.html
c. navigableSet: As the name suggest, it will allow you to navigate in a
sortedSet. Yes it is little extra for SortedSet,it will let you find near by
element for given search targets. It lets your traverse in ascending and
descending order.
want to know more? ==> http://java.sun.com/javase/6
/docs/api/java/util/NavigableSet.html
d. NavigableMap- Its same as above for SortedMap.
want to know more? ==> http://java.sun.com/javase/6 /docs/api/java/util/NavigableMap.html
Note: LinkedList,TreeSet,TreeMap have been modified to accomodate new changes as Deque,NavigableSet and NavigableMap respectively.
2. Things which got added newly
a. ArrayDeque- A new dynamic array to support Deque,they can grow upto any size
and faster than stack and linked list if used as stack and linkedlist.
want to know more? ==> http://java.sun.com/javase/6
/docs/api/java/util/ArrayDeque.html
b. newSetFromMap(Map) - This is a powerfull method added in Collections utility
class,it lets you create one full flegded Set from a full fledged Map. No need
to iterate and do it manually.
want to know more? ==> http://java.sun.com/javase/6 /docs/api/java/util/Collections.html#newSetFromMap (java.util.Map)
The Arrays utility class now has methods copyOf and copyOfRange that can efficiently resize, truncate, or copy subarrays for arrays of all types.There are other classes and interfaces which have been added but I noted only those what we might use often.
hope it helps :)
a. Deque- if you expand it then it will be something like this
D + E +Que ==> Double + Ended + Queue =>Add or remove from either side,what an Idea sir ji :)
Its an interface known as double ended queue and extends Queue.
want to know more? ==>http://java.sun.com/javase/6/docs/api/java/util/Deque.html
b. BlockingDeque- This is little advanced Deque which will not allow deque to be
empty while retreiving data and look for space while adding data. Its again an
interface which extends Deque and BlockingQueue interfaces.
want to know more? ==> http://java.sun.com/javase/6 /docs/api/java/util/concurrent/BlockingDeque.html
c. navigableSet: As the name suggest, it will allow you to navigate in a
sortedSet. Yes it is little extra for SortedSet,it will let you find near by
element for given search targets. It lets your traverse in ascending and
descending order.
want to know more? ==> http://java.sun.com/javase/6
/docs/api/java/util/NavigableSet.html
d. NavigableMap- Its same as above for SortedMap.
want to know more? ==> http://java.sun.com/javase/6 /docs/api/java/util/NavigableMap.html
Note: LinkedList,TreeSet,TreeMap have been modified to accomodate new changes as Deque,NavigableSet and NavigableMap respectively.
2. Things which got added newly
a. ArrayDeque- A new dynamic array to support Deque,they can grow upto any size
and faster than stack and linked list if used as stack and linkedlist.
want to know more? ==> http://java.sun.com/javase/6
/docs/api/java/util/ArrayDeque.html
b. newSetFromMap(Map) - This is a powerfull method added in Collections utility
class,it lets you create one full flegded Set from a full fledged Map. No need
to iterate and do it manually.
want to know more? ==> http://java.sun.com/javase/6 /docs/api/java/util/Collections.html#newSetFromMap (java.util.Map)
The Arrays utility class now has methods copyOf and copyOfRange that can efficiently resize, truncate, or copy subarrays for arrays of all types.There are other classes and interfaces which have been added but I noted only those what we might use often.
hope it helps :)
Thursday, January 14, 2010
some Important and basic questions
Q: Can we write a constructor in servlet? if yes why and how?what are the
advantages and disadvantages?
A: Servlets are nothing but a java class with .java extension.As per java rules each class should have a constructor, if you dont have java will create one non arg constructor for you.You can always implement your constructor in your way.Servlets are different class than normal java classes as JVM will not have control over it, only a web container can control servlets, how ever a servlet will not be servlet until container calls it inits method and intilize it, so even though you have defined your constructor it will not have access to any servlet specific objects/functionality, so it will be useless to have constructor in servlet class.
Q.How to handle(increase/decrease) heap size of JVM?
A.You can use command line argument to do so.
java -Xms=256MB some argument
-Xms ==> initial heap size alloted to JVM
-Xmx ==> maximum heap size alloted to JVM
a tool named as jconsole in java 1.5 + series, with this tool you can find how much memory your program is taking and how many threads are running.
Q. Why every action in servlet programming goes to web.xml, where it is defined?
A.web.xml which is also known as deploy descriptor is the center point for a servlet programming as all mapping we do there only.There is file known as context.xml in conf directory, where we define path of web.xml and content goes like this..
Context
---WatchedResource
-- WEB-INF/web.xml
---/WatchedResource
/Context
Q.A man has a dog as his best friend. how to write this in java ?
A. This question lets you know, how much you knwo in java and this is best example to relate java with real life.
now lets write it in java...
if you watch this question carefully then what is the catchy word for you?
you got it right, "has a" is the catchy word in this..
there are two classes involved in this, one is Man and the other one is Dog, we are referring dog object as best friend of man.
lets write the code now...
public class man{
private Dog bestFriend;
}
how simple was it? its so easy to relate to real life. Man has a dog, referring as best friend and the code expalins it very well.
Q. What is initial capacity of an Array list?
A. 10
Q. What is initial capacity of hash map/table?
A. 16
advantages and disadvantages?
A: Servlets are nothing but a java class with .java extension.As per java rules each class should have a constructor, if you dont have java will create one non arg constructor for you.You can always implement your constructor in your way.Servlets are different class than normal java classes as JVM will not have control over it, only a web container can control servlets, how ever a servlet will not be servlet until container calls it inits method and intilize it, so even though you have defined your constructor it will not have access to any servlet specific objects/functionality, so it will be useless to have constructor in servlet class.
Q.How to handle(increase/decrease) heap size of JVM?
A.You can use command line argument to do so.
java -Xms=256MB some argument
-Xms ==> initial heap size alloted to JVM
-Xmx ==> maximum heap size alloted to JVM
a tool named as jconsole in java 1.5 + series, with this tool you can find how much memory your program is taking and how many threads are running.
Q. Why every action in servlet programming goes to web.xml, where it is defined?
A.web.xml which is also known as deploy descriptor is the center point for a servlet programming as all mapping we do there only.There is file known as context.xml in conf directory, where we define path of web.xml and content goes like this..
Context
---WatchedResource
-- WEB-INF/web.xml
---/WatchedResource
/Context
Q.A man has a dog as his best friend. how to write this in java ?
A. This question lets you know, how much you knwo in java and this is best example to relate java with real life.
now lets write it in java...
if you watch this question carefully then what is the catchy word for you?
you got it right, "has a" is the catchy word in this..
there are two classes involved in this, one is Man and the other one is Dog, we are referring dog object as best friend of man.
lets write the code now...
public class man{
private Dog bestFriend;
}
how simple was it? its so easy to relate to real life. Man has a dog, referring as best friend and the code expalins it very well.
Q. What is initial capacity of an Array list?
A. 10
Q. What is initial capacity of hash map/table?
A. 16
Java VS Real Life
we always treat java as programming languauge, but if you keenly watch, its same as our real life.
Lets take some example:
class Person {
private String name;
priavte int age;
private int height;
public boolean doEat(){
return true;
}
public boolean doSleep(){
return true;
}
public boolean doBreath(){
return true;
}
}
public class Manikesh extends Person{
}
Above example is a normal java class, now lets relate this to our real life.
for a human being, person would be most generic form, when we say person, it might be he or she or anything. So in java,a person would be super class which defines rules.
a Person should have states or attribute as name, age, height etc.... , and person should have behaviuor as well like jumping,eating, sleeping, talking etc...states will have some value and behavour will describe what he does or what he does not.
so now if you see the Person class, the member variables of the class acts as states of a person and all member methods acts as behaviour of Person.
Manikesh is a class which extends Person in simple words Manikesh Is A Person.
when Manikesh extends Person then he will have same states and behaviour.
name=manikesh
age= 28
height= 5.7
same will go for behaviour.so any class which extends Person is a human being and will have same states and behavious where values might differ.
so here Manikesh is one instance of Person.
There are two simple key words in java IsA and HasA .
IsA examples:
Manikesh IsA/extends Person
Mango IsA/extends Fruits
Car IsA/extends Vehicle
hasA examples:
Manikesh hasA Car/bike/nose etc...
Car hasA wheels/engine etc...
Wrong combinations for IsA and hasA
car extends garage
manikesh extends car
in other words
car isA garage |-- These two statements are wrong, they dont clear isA test.
manikesh isA car |
Lets take some example:
class Person {
private String name;
priavte int age;
private int height;
public boolean doEat(){
return true;
}
public boolean doSleep(){
return true;
}
public boolean doBreath(){
return true;
}
}
public class Manikesh extends Person{
}
Above example is a normal java class, now lets relate this to our real life.
for a human being, person would be most generic form, when we say person, it might be he or she or anything. So in java,a person would be super class which defines rules.
a Person should have states or attribute as name, age, height etc.... , and person should have behaviuor as well like jumping,eating, sleeping, talking etc...states will have some value and behavour will describe what he does or what he does not.
so now if you see the Person class, the member variables of the class acts as states of a person and all member methods acts as behaviour of Person.
Manikesh is a class which extends Person in simple words Manikesh Is A Person.
when Manikesh extends Person then he will have same states and behaviour.
name=manikesh
age= 28
height= 5.7
same will go for behaviour.so any class which extends Person is a human being and will have same states and behavious where values might differ.
so here Manikesh is one instance of Person.
There are two simple key words in java IsA and HasA .
IsA examples:
Manikesh IsA/extends Person
Mango IsA/extends Fruits
Car IsA/extends Vehicle
hasA examples:
Manikesh hasA Car/bike/nose etc...
Car hasA wheels/engine etc...
Wrong combinations for IsA and hasA
car extends garage
manikesh extends car
in other words
car isA garage |-- These two statements are wrong, they dont clear isA test.
manikesh isA car |
Subscribe to:
Comments (Atom)