Thursday, 4 October 2012

Java Practical - 24





Program Definition 24 ::

Define one class E in package epack.  In class E, three variables are defined of access modifiers protected, private & public. Define class F in package fpack which extends E and write display() method which access variables of class E.  Define class G in package gpack which has one method display() in that create one object of class E and display its variables.  Define class ProtectedDemo in package hpack in which write main() method.  Create objects of class F and G and class display method for both these objects.




// save this file in epack folder
package epack;
public class E
{
public int publicVar = 10;
protected int protectedVar = 20;
private int privateVar = 30;

}


// save this file in fpack folder
package fpack;
import epack.*;

public class F extends E
{
public void display()
{
System.out.println("from Class F : package fpack");
//System.out.println("private" + privateVar); // not accessible
System.out.println("public" + publicVar);
System.out.println("protected" + protectedVar);

}
}

// save this file in gpack folder
package gpack;
import epack.*;

public class G
{
E eObj = new E();

public void display()
{
System.out.println("from Class G : package gpack");
//System.out.println("private" + eObj.privateVar); // not accessible
System.out.println("public" + eObj.publicVar);
//System.out.println("protected" + eObj.protectedVar); // not accessible
}
}

// save this file in hpack folder
package hpack;
import gpack.*;
import fpack.*;

class ProtectedDemo
{
public static void main( String args[] )
{

F fObj = new F();
G gObj = new G();

fObj.display();
gObj.display();
}
}

/*

D:\JAVA>javac epack/*.java

D:\JAVA>javac fpack/*.java

D:\JAVA>javac gpack/*.java

D:\JAVA>javac hpack/*.java

D:\JAVA>java hpack.ProtectedDemo
from Class F : package fpack
public10
protected20
from Class G : package gpack
public10
*/

Kindly Bookmark and Share it:

0 comments :

Post a Comment

Any Query ? any suggestion ? comment here

 

Recent Post

Recent Comments

© 2010 IamLearningHere Template by MBT