Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Assignment-4

2.Create a class which contains an inner class. Show that inner class can use member of outer class
directly, but Outer class can use member of Inner class only through its object. Check the name of
class file, you created.

Source Code:

class Outer{
int y=100;
public void show() {
System.out.println("This is the outer class");
}
class Inner{
void display() {
show();
System.out.println("Y="+y);
System.out.println("This is the Inner Class");
}
}
}
public class Assignment4Question2 {
public static void main(String[] args) {
new Outer().new Inner().display();
}
}

Output:

This is the outer class


Y=100
This is the Inner Class

4.
Write a program to demonstrate anonymous inner class (using super class and interface).
Source Code:

class AnoClass{
void show(){
System.out.println("This is the Super class
method");
}
}
interface A1 {
public AnoClass get();
}
class B1 implements A1 {
public AnoClass get() {
return new AnoClass(){
void show() {
System.out.println("This is the overridden
method");
}
};
}
}
public class Question4 {
public static void main(String[] args) {
new B1().get().show();
}
}

Output:

This is the overridden method

5.
Show that ordinary block is executed when object is created and also the order of execution of these
blocks (for multiple blocks/ inherited block).
Source Code:

class BlockParent {
public BlockParent() {
System.out.println("BlockParent Constructor");
}
{
System.out.println("BlockParent Initializer
BlockParent");
}
{
System.out.println("BlockParent Initializer
Block2");
}
}
class Blockchild extends BlockParent{
public Blockchild() {
System.out.println("Blockchild Constructor");
}
{
System.out.println("Blockchild Initializer
BlockParent");
}
{
System.out.println("Blockchild Initializer
Blockchild");
}
}
public class BlockTest {
public static void main(String[] args) {
Blockchild ob=new Blockchild();
}
}

Output:

BlockParent Initializer BlockParent


BlockParent Initializer Block2
BlockParent Constructor
Blockchild Initializer BlockParent
Blockchild Initializer Blockchild
Blockchild Constructor

6.
Show that static block is executed at the time of class loading and also the order of execution of these
blocks (for multiple blocks/ inherited block).

Source Code:
class Arrow {
Arrow() {
System.out.println("Super constructor");
}
{
System.out.println("Super Initializer block");
}
static {
System.out.println("Super static block");
}
}//static blocks are called when a class is loaded into jvm
class Bow extends Arrow {
Bow() {
System.out.println("Sub constructor");
}
{
System.out.println("Sub Initializer block");
}
static {
System.out.println("Sub static block");
}
}
public class BlockTest2 {
public static void main(String[] args){
Bow ob= new Bow();
}
}

Output:

Super static block


Sub static block
Super Initializer block
Super constructor
Sub Initializer block
Sub constructor

7.Write a program to show the difference between ordinary block and static block.

Source Code:
public class BlockTest3 {
public static void main(String[] args) {
Block2 ob=new Block2();
}
}
class Block2 {
static int a;
int b;
Block2()
{
System.out.println("Constructor Block");
}
static{//only static fields can be accessed inside
static blocks
a=20;
System.out.println("a="+a);
}
{ //both static and non-static fields can be accessed
inside intializer blocks
a=30;
b=50;
System.out.println("a="+a+" b="+b);
}
}

Output:

a=20
a=30 b=50
Constructor Block

8.
Write a program to demonstrate the order of execution among the parent and child‟s static and non-
static blocks.

Source Code:
class Apple {
Apple() {
System.out.println("Super constructor");
}
{
System.out.println("Super Initializer block");
}
static {
System.out.println("Super static block");
}
} //static blocks are called when a class is loaded into jvm
class Banana extends Apple {
Banana() {
System.out.println("Sub constructor");
}
{
System.out.println("Sub Initializer block");
}
static {
System.out.println("Sub static block");
}
}
public class BlockTest4 {
public static void main(String[] args) {
Banana ob=new Banana();
}
}
Output:

Super static block


Sub static block
Super Initializer block
Super constructor
Sub Initializer block
Sub constructor

You might also like