Stats
Monday, 22 February 2016
Java Bean
What is Java Bean?
Java bean is a simple java class with instance variables (all private) and each instance variable having getter and setter methods (public) for them.
Example :
class Person { private int id; private String name; private String address; public int getId () { return id; } public String getName () { return name; } public String getAddress () { return address; } public void setId (int id) { this.id = id; } public void setName (String name) { this.name = name; } public void setAddress (String address) { this.address = address; } }
Java and its Features in brief
What is Java?
Java is a Object Oriented Programming Language.
Main Features of Java are :
Class :- Template
Object :- Instance with Specific Values
Inheritance :- Creating new classes by extending the features of existing classes
Polymorphism :- A single variable of class type can take multiple forms i.e. a variable can be assigned to objects of different types along the class hierarchy
Class :
Definition of classclass <name>
{
variables;
constructors;
methods;
}
class X{
//variables
int x;
//constructors
X(int x) { this.x = x; }
//methods
int getX() { //body }
void setX(int x) { //body}
}
Object :
Using keyword new we create an instance i.e. object of a class.X temp = new X(5);
Inheritance :
Another class YClass Y extends X{
//variables
int y;
//constructors
Y(int x,int y) { super(x); this.y = y; }
//methods
int getY() { //body }
void setY(int y) { //body}
}
Here the method getX() and setX(int x) are inherited in class Y and can be called from class Y.
Polymorphism :
Another class Z instatiates X and Y classes.class Z{
int z;
p.s.v.m.(){
X temp = new X(5);
System.out.println("X : " + temp.getX());
temp = new Y(5,10);
System.out.println("X : " + temp.getX()); // cannot call temp.getY() as temp is variable of type X which does
// not have method getY()
}
}
Same variable can take multiple forms i.e. it can be assigned an instance of X or Y. Hence same variable temp takes multiple forms.
This is polymorphism.
Subscribe to:
Posts (Atom)