Search This Blog

Showing posts with label java program. Show all posts
Showing posts with label java program. Show all posts

16 June, 2011

Simple Java program related to inheritance - part 4

class aaa
{
public void sam()
{

System.out.println("hello");
}
public void tom()
{


}
}
class bbb extends aaa
{
public void tom()
{
System.out.println("good");
}
}
class psp
{
public static void main(String gg[])
{
aaa a;
a=new aaa();
a.tom();                 // tom will run of aaa class


aaa p;
p=new bbb();
p.tom();              // tom will run of bbb class.
}
}


Note:- But if this code is of C++ then (p.tom();) line runs tom of
           aaa class because in c++, the object of aaa can't store the 
           address of bbb class. 
----------------------------------------------------------------------------------

15 June, 2011

Simple Java program related to inheritance - part 2

In case of inheritance a base class pointer is capable of storing address of an object created from its derived class but through that pointer the methods of the derived class can't be accessed unless they are part of base class. And vice-versa is not possible.


class aaa 
{
}
class bbb extends aaa
{
}
class sss
{
public static void main(String gg[])
{
aaa a;
a=new bbb(); // Correct
bbb b;
b=new aaa();  // Incorrect
}
}


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

Simple Java program related to inheritance - part 1

A Simple Java program related to inheritance:-


class aaa
{


}
class bbb
{


}


class ccc
{
public static void main(String mm[])
{
aaa a;
a=new bbb();    // Incorrect not compiled because both aaa
bbb b;                 // and bbb don't have relation ship between  
b=new aaa();     // each other.
}
}


continue with - part 2
-------------------------------------------------------------------------

Simple Java program 10

10. Simple Java programs to know what 
    output  will be.


class aaa
{

private int a;

public void setwattage(int e)

w=e; 

}

public void printinfo()

{
System.out.println("Wattage is  "+w)
}
}
class psp
{
public static void main(String aa[])
{
aaa x;
x=new aaa();
x.setwattage(60);

x.printinfo();

}



O/P:- Wattage is 60


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

Simple Java program 9

9. Simple Java programs to know what 
    output  will be.


class aaa
{
static final private int a;

private int b;
public void sam()

System.out.println(a); 

}

static                    // Static constructor
{
a=7;
}
}
class psp
{
public static void main(String aa[])
{
aaa x;
x=new aaa();
x.aaa();

}



Note:- If we don't want to declare the value to static
            property at the point of declaration then we can 
            also assign value for that static property in static 
            constructor. It will get compiled easily.


Output:- 7

Simple Java program 8

8. Simple Java programs to know what 
    output  will be.


class aaa
{
static final private int a=10; //Assignment is necessary at 

                                                 // the point of declaration.
private int b;
public void sam()

System.out.println(a); 

}

}

class psp
{
public static void main(String aa[])
{
aaa x;
x=new aaa();
x.aaa();

}

Simple Java program 7

7. Simple Java programs to know what 
    output  will be.


class aaa
{
final private int a;

private int b;
public void sam()

System.out.println(a); 

}

aaa()
{
a=7;
}
aaa(int e)

{

x=9;
b=e;
}
}

class psp
{
public static void main(String aa[])
{
aaa x;
x=new aaa();
x.aaa();

}



O/P:- 7    // Easily will get compiled because both constructor has value of x.

Simple Java program 6

6. Simple Java programs to know what 
    output  will be.


class aaa
{
final private int a;

private int b;
public void sam()

System.out.println(a); 

}

aaa()
{
a=7;
}
aaa(int e)

{
b=e;
}
}

class psp
{
public static void main(String aa[])
{
aaa x;
x=new aaa();
x.aaa();

}



O/P:- Error will generate because x is not assigned in parameterized constructor.
----------------------------------------------------------------------------------------------

Simple Java program 5

5. Simple Java programs to know what 
    output  will be.


class aaa
{
final private int a;
public void sam()

System.out.println(a); 

}

aaa()
{
a=7;
}


}

class psp
{
public static void main(String aa[])
{
aaa x;
x=new aaa();
x.aaa();

}



To execute above program:-
C:\jdk1.6\bin>javac h.java     (h is saved name of this program)
C:\jdk1.6\bin>java psp

O/P:- 7  

Simple Java program 4

4. Simple Java programs to know what output will be.


class aaa
{
final private int a=10;  // If object variable is final then the value should be assigned at the declaration time.
public void sam()
{

System.out.println(a);

}
}
class psp
{
public static void main(String aa[])
{
aaa x;
x=new aaa();
x.aaa();

}
}



To execute above program:-
C:\jdk1.6\bin>javac h.java     (h is saved name of this program)
C:\jdk1.6\bin>java psp

O/P:- 10  

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

Simple Java program 3

3. Simple Java programs to know what output will be.


class aaa
{
final private int a;
public void sam()
{

a=10;
System.out.println(a);
}
}
class psp
{
public static void main(String aa[])
{
aaa x;
x=new aaa();
x.aaa();

}
}



O/P:-  Compile error because cannot assign a value to final variable a such type in

            method because that can be called more times.
-------------------------------------------------------------------------

Meetme@