What we should know about Class #java
Class is a user defined data type, using data type we can create variables of that data type similarly we can create objects of that class, So instance of that class, object id a variable of user defined data type.
modifiers class <class name>
{
Class body ...//body of class can consist data members and methods.
}
Modifiers : that control accessibility of class , data members and methods.
Class :
a) public
b) if no modifier is defined then by default is package. but cant define explicitly. ( class can be abstract or final)
Members: a) public, private, protected
The memory for object is always dynamically allocated using new operator.
Variables in java:
a) Local variable : inside a method
b) Instance (non static) variable : outside a method
c) static variable :
use dot (.) to access variables, dot operator in java is similar to -> in c/c++
static variable also access using dot operator and need to be initialization as class loaded/used.
Method overloading: same name with different type of parameters or number of parameters or both.
Constructors: non-static methods with no return type and same name as the class. constructors can be overloaded as methods of class.
java doesnot allow two local variable with the same name inside the same scope. but formal parameter can overload the class variables so in that case we use this key word to initialize class variables.
Copy constructors : used to initialize new object using existing object.
primitive variables in java use call by value and a object reference also call by value.
Variable length arguments: a simple method take variable arguments call variable-arity method or varargs method.
static void demo ( int a, int ...a)
{
for (int i =0; i< a.length; i++)
{
System.out.println(a[i]);
}
}
Note: one method can have only one method variable length argument and variable length argument must be last parameter.
Similarly other methods we can overload varargs methods.
Java have 4 types of inner classes:
1) Static inner class:
class Outer {
String ste ="Who is the boss" ;
static class Inner {
String ste ="I am the boss" ;
void go(Outer obj) {
System.out.println("Inner class reference is: " + obj.ste);
}
void go(Inner obj) {
System.out.println("Inner class reference is: " + obj.ste);
}
}
}
public class InnerClass{
public static void main(String[] args) {
Outer.Inner n = new Outer.Inner();
n.go( n);
Outer p = new Outer();
n.go(p);
}
2) Member Inner Class:
class EagerInitialization {
private int x = 100;
public void makeInner(){
Inner in = new Inner();
in.seeOuter();
}
class Inner{
public void seeOuter(){
System.out.println("Outer x is " + x);
System.out.println("Inner class reference is " + this);
System.out.println("Outer class reference is " + EagerInitialization.this);
}
}
public static void main(String[] args) {
EagerInitialization o = new EagerInitialization();
Inner i = o.new Inner();
i.seeOuter();
o.makeInner();
}
}
3) Method-Inner class:
public class Outer {
private String x = "outer";
public void doStuff() {
class MyInner {
public void seeOuter() {
System.out.println("x is " + x);
}
}
MyInner i = new MyInner();
i.seeOuter();
}
public static void main(String[] args) {
Outer o = new Outer();
o.doStuff();
}
}
4) Anonymous class: Anonymous class is a inner class or always created inside
a class which doesnot have its own name and instance is created at time of class creation.
Anonymous class created using below two approachs.
a) Class reference variable
b) Using a interface
a) Creating a anonymous class using a class reference : it looks like we are creating
a instance of outside class but actually we are creating a anonymous class and its instance.
public class Apple {
public void print() {
System.out.println("Printed from Apple !");
}
public void printAgain() {
System.out.println("Printed from Apple again !");
}
}
package com.beingjavaguys.core;
/*
* Simple java class creation
*/
public class Implementation {
/*
* This looks like we are creating an object of Apple class, but actually we
* we have created an instance of anonymous subclass of Apple. The statement
* written below creates an anonymous inner class with an instance of it and
* the class is a subclass to Apple.
*/
Apple apple = new Apple() {
@Override
public void print() {
System.out.println("Printed from subclass of Apple !");
}
public void new()
{}
};
}
As we know we cant call methods of inner class using object of super class. So like same Apple class object cant call new method of sub-class or anonymous inner class.
b) Creating a anonymous inner class using interface: In this we create a anonymous and implement a interface and create instance of that class at that time .
package com.beingjavaguys.core;
public interface AppleInterface {
public void callMe();
}
package com.beingjavaguys.core;
public class Implementation {
/*
* Here we have created an anonymous inner class that implements
* AppleInterface and also created an instance of this class at the same
* time.
*/
AppleInterface appleInterface = new AppleInterface() {
@Override
public void callMe() {
// TODO Auto-generated method stub
}
};
}
Use of anonymous inner class :
Its good when we want to use a class but we are going to use only few properties of class, So its quick and fast respect to extending a class.
modifiers class <class name>
{
Class body ...//body of class can consist data members and methods.
}
Modifiers : that control accessibility of class , data members and methods.
Class :
a) public
b) if no modifier is defined then by default is package. but cant define explicitly. ( class can be abstract or final)
Members: a) public, private, protected
The memory for object is always dynamically allocated using new operator.
Variables in java:
a) Local variable : inside a method
b) Instance (non static) variable : outside a method
c) static variable :
use dot (.) to access variables, dot operator in java is similar to -> in c/c++
static variable also access using dot operator and need to be initialization as class loaded/used.
Method overloading: same name with different type of parameters or number of parameters or both.
Constructors: non-static methods with no return type and same name as the class. constructors can be overloaded as methods of class.
java doesnot allow two local variable with the same name inside the same scope. but formal parameter can overload the class variables so in that case we use this key word to initialize class variables.
Copy constructors : used to initialize new object using existing object.
primitive variables in java use call by value and a object reference also call by value.
Variable length arguments: a simple method take variable arguments call variable-arity method or varargs method.
static void demo ( int a, int ...a)
{
for (int i =0; i< a.length; i++)
{
System.out.println(a[i]);
}
}
Note: one method can have only one method variable length argument and variable length argument must be last parameter.
Similarly other methods we can overload varargs methods.
Java have 4 types of inner classes:
1) Static inner class:
class Outer {
String ste ="Who is the boss" ;
static class Inner {
String ste ="I am the boss" ;
void go(Outer obj) {
System.out.println("Inner class reference is: " + obj.ste);
}
void go(Inner obj) {
System.out.println("Inner class reference is: " + obj.ste);
}
}
}
public class InnerClass{
public static void main(String[] args) {
Outer.Inner n = new Outer.Inner();
n.go( n);
Outer p = new Outer();
n.go(p);
}
2) Member Inner Class:
class EagerInitialization {
private int x = 100;
public void makeInner(){
Inner in = new Inner();
in.seeOuter();
}
class Inner{
public void seeOuter(){
System.out.println("Outer x is " + x);
System.out.println("Inner class reference is " + this);
System.out.println("Outer class reference is " + EagerInitialization.this);
}
}
public static void main(String[] args) {
EagerInitialization o = new EagerInitialization();
Inner i = o.new Inner();
i.seeOuter();
o.makeInner();
}
}
3) Method-Inner class:
public class Outer {
private String x = "outer";
public void doStuff() {
class MyInner {
public void seeOuter() {
System.out.println("x is " + x);
}
}
MyInner i = new MyInner();
i.seeOuter();
}
public static void main(String[] args) {
Outer o = new Outer();
o.doStuff();
}
}
4) Anonymous class: Anonymous class is a inner class or always created inside
a class which doesnot have its own name and instance is created at time of class creation.
Anonymous class created using below two approachs.
a) Class reference variable
b) Using a interface
a) Creating a anonymous class using a class reference : it looks like we are creating
a instance of outside class but actually we are creating a anonymous class and its instance.
public class Apple {
public void print() {
System.out.println("Printed from Apple !");
}
public void printAgain() {
System.out.println("Printed from Apple again !");
}
}
package com.beingjavaguys.core;
/*
* Simple java class creation
*/
public class Implementation {
/*
* This looks like we are creating an object of Apple class, but actually we
* we have created an instance of anonymous subclass of Apple. The statement
* written below creates an anonymous inner class with an instance of it and
* the class is a subclass to Apple.
*/
Apple apple = new Apple() {
@Override
public void print() {
System.out.println("Printed from subclass of Apple !");
}
public void new()
{}
};
}
As we know we cant call methods of inner class using object of super class. So like same Apple class object cant call new method of sub-class or anonymous inner class.
b) Creating a anonymous inner class using interface: In this we create a anonymous and implement a interface and create instance of that class at that time .
package com.beingjavaguys.core;
public interface AppleInterface {
public void callMe();
}
package com.beingjavaguys.core;
public class Implementation {
/*
* Here we have created an anonymous inner class that implements
* AppleInterface and also created an instance of this class at the same
* time.
*/
AppleInterface appleInterface = new AppleInterface() {
@Override
public void callMe() {
// TODO Auto-generated method stub
}
};
}
Use of anonymous inner class :
Its good when we want to use a class but we are going to use only few properties of class, So its quick and fast respect to extending a class.
Comments
Post a Comment