You are on page 1of 6

Anonymous Class (Extending class)

class Polygon {
public void display() {
System.out.println("Inside the Polygon class");
}
}

class AnonymousDemo {
public void createClass() {

// creation of anonymous class extending class Polygon


Polygon p1 = new Polygon() {
public void display() {
System.out.println("Inside an anonymous class.");
}
};
p1.display();
}
}
class anonymous_extendClass {
public static void main(String[] args) {
AnonymousDemo an = new AnonymousDemo();
an.createClass();
}
}

Anonymous Class (Implementing Interface)


interface Polygon {
public void display();
}

class AnonymousDemo {
public void createClass() {

// anonymous class implementing interface


Polygon p1 = new Polygon() {
public void display() {
System.out.println("Inside an anonymous class.");
}
};
p1.display();
}
}

public class anonymous_implementInterface {


public static void main(String[] args) {
AnonymousDemo an = new AnonymousDemo();
an.createClass();
}
}

Anonymous (Abstract Class)


abstract class Animal
{
abstract void type();
}
public class anonymous_nestedClass extends Animal {
public static void main(String args[])
{

Animal an = new Animal() { //Anonymous class created


public void type()
{
System.out.println("Anonymous Animal Class");
}
};
an.type();
}
}

Garbage Collection (Using Finalize() Method)


public class garbage_collection
{
public static void main(String[] args) //throws InterruptedException
{
garbage_collection gc1 = new garbage_collection();
garbage_collection gc2 = new garbage_collection();

gc1 = null; // Nullifying the reference variable


System.gc(); // requesting JVM for running Garbage Collector
gc2 = null; // Nullifying the reference variable
Runtime.getRuntime().gc(); // requesting JVM for running Garbage
Collector

}
protected void finalize()// throws Throwable // finalize method is called on
object once before garbage collecting it
{
System.out.println("Garbage collector called");
System.out.println("Object garbage collected : " + this);
}
}

//NOTE : There is no guarantee that any one of above two methods will definitely
run Garbage Collector.
//The call System.gc() is effectively equivalent to the call :
Runtime.getRuntime().gc()

Local Inner Class


class Outer_Class
{
int count;
public void display()
{
for(int i=0;i<5;i++)
{
//Inner class defined inside for loop
class Inner
{
public void show()
{
System.out.println("Inside Inner Class Method : "+(count++));
}
}
System.out.println("Calling Inner Class from the Outer Class");
Inner in=new Inner();
in.show();
}
}
}

class local_nestedClass
{
public static void main(String[] args)
{
Outer_Class ot = new Outer_Class();
ot.display();
}
}
Member Inner Class
class Car {
String carName;
String carType;

// assign values using constructor


public Car(String name, String type) {
this.carName = name;
this.carType = type;
}

// private method
private String getCarName() {
return this.carName;
}

// inner class
class Engine {
String engineType;
void setEngine() {

// Accessing the carType property of Car


if(Car.this.carType.equals("4WD")){

// Invoking method getCarName() of Car


if(Car.this.getCarName().equals("Crysler")) {
this.engineType = "Smaller";
} else {
this.engineType = "Bigger";
}

}else{
this.engineType = "Bigger";
}
}
String getEngineType(){
return this.engineType;
}
}
}

public class member_nestedClass {


public static void main(String[] args) {
Car car1 = new Car("Mazda", "8WD"); // create an object of the outer class
Car

Car.Engine engine = car1.new Engine(); // create an object of inner class


using the outer class
engine.setEngine();
System.out.println("Engine Type for 8WD= " + engine.getEngineType());

Car car2 = new Car("Crysler", "4WD");


Car.Engine c2engine = car2.new Engine();
c2engine.setEngine();
System.out.println("Engine Type for 4WD = " + c2engine.getEngineType());
}
}

Static Inner Class


class OuterClass
{

static int outer_x = 10; // static member


int outer_y = 20; // instance(non-static) member
private static int outer_private = 30; // private member

static class StaticNestedClass // static nested class


{
void display()
{
System.out.println("outer_x = " + outer_x); // can access static
member of outer class
System.out.println("outer_private = " + outer_private); // can
access display private static member of outer class
}
}
}
public class static_nestedClass
{
public static void main(String[] args)
{
// accessing a static nested class
OuterClass.StaticNestedClass nestedObject = new
OuterClass.StaticNestedClass();
nestedObject.display();

}
}

You might also like