Search This Blog

22 March, 2011

C# Program with "base - keyword".



class aaa
{
public void sam()
{
System.Console.WriteLine("Hello");
}
}
class bbb:aaa
{
public void sam()
{
System.Console.WriteLine("Great");
}
public void lion()
{
sam();
base.sam();
}
}
class psp
{
public static void Main()
{
bbb b=new bbb();
b.lion();
}
}
--------------------------------------------------------------------------------------------------------------------

C# Program with "as - keyword".

//C# Program with "as - keyword".


class aaa
{
public void sam()
{
System.Console.WriteLine("Hello");
}
}
class bbb
{
public void accept(object k)
{
aaa a=k as aaa;  // Null will be assigned if not castable
//aaa a=(aaa)k; //Would generate an exception if the material is not castable
if(a!=null)
{
a.sam();
}
}
}
class ccc
{
public void tom()
{
System.Console.WriteLine("good");
}
}
class psp
{
public static void Main()
{
aaa a=new aaa();
bbb b=new bbb();
b.accept(a);
ccc c=new ccc();
b.accept(c);
}
}
------------------------------------------------------------------------------------------------------

C# Program with "abstract keyword".


C# Program with "abstract keyword".






abstract class aaa
{
abstract public void sam();
}
/* class bbb:aaa                   // wrong
{
}  */
abstract class ccc:aaa         //Correct
{
}
class ddd:aaa
{
public override void sam()
{
System.Console.WriteLine("Hello");
}
}
class psp
{
public static void Main()
{
//aaa a;                              //Correct
//a=new aaa();               // Wrong , Because can't create instance or object of an abstract class . 
ddd d =new ddd();
d.sam();
}




-------------------------------------------------------------------------





12 March, 2011

A Java program using static variables, methods and blocks.

Demonstrate  
    
                   "static variables, methods, and blocks".                      
 





class UseStatic
{ 
static int a = 3; 
static int b; 
static void meth(int x)
{ 
System.out.println("x = " + x); 
System.out.println("a = " + a); 
System.out.println("b = " + b); 
} 
static
 { 
System.out.println("Static block initialized."); 
b = a * 4; 
} 
public static void main(String args[])
 { 
meth(42); 
} 
}
---------------------------------x----------------------

class StaticDemo { 
static int a = 42; 
static int b = 99; 
static void callme() { 
System.out.println("a = " + a); 
} 
} 
class StaticByName { 
public static void main(String args[]) { 
StaticDemo.callme(); 
System.out.println("b = " + StaticDemo.b); 
} 
} 

Here is the output of this program: 
a = 42 
b = 99

---------------------------------------------------------------------------------------------------


A Java program using abstarct keyword

A Simple demonstration of ........ abstract keyword ..........



abstract class A
 { 
abstract void callme();
 
// concrete methods are still allowed in abstract classes
 
void callmetoo()
 { 
System.out.println("This is a concrete method.");
 
}
 
}
class B extends A
 { 
void callme()
 { 
System.out.println("B's implementation of callme.");
 
}
 
}
class AbstractDemo
 { 
public static void main(String args[])
 { 
B b = new B();
 
b.callme();
 
b.callmetoo();
 
}
 
}

-------------------------------------------------------------------------------------



Java Program using this keyword


"A java program to demonstrate the use of this keyword "





class ThisDemo1

{

int a = 0;
int b = 0;

ThisDemo1(int x, int y)

{

this.a = x;

this.b = y;

}

public static void main(String [] args)

{

ThisDemo1 td = new ThisDemo1(10,12);

ThisDemo1 td1 = new ThisDemo1(100,23);

System.out.println(td.a); // prints 10

System.out.println(td.B); // prints 12

System.out.println(td1.a);// prints 100

System.out.println(td1.B);// prints 23

}

}



when we compile and run the program we get the output as:-

C:\Java\Concepts\DeclarationAndAccessControl>java ThisDemo1
10
12
100
23
-------------------------------------------------------------------------------------------------



Meetme@