Showing posts with label corejava. Show all posts
Showing posts with label corejava. Show all posts

Wednesday 16 January 2013

Inheritance in java

// siddhu vydyabhushana // Leave a Comment
INHERITANCE IN JAVA

Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object.

The idea behind inheritance is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you reuse (or inherit) methods and fields, and you add new methods and fields to adapt your new class to new situations.Inheritance represents the IS-A relationship.

Why use Inheritance?

1)For Method Overriding.

2)For Code Reusability.



class Subclass-name extends Superclass-name
{
   //methods and fields
}
The keyword extends indicates that you are making a new class that derives from an existing class. In the terminology of Java, a class that is inherited is called a superclass. The new class is called a subclass.

EXAMPLE:

As displayed in the above figure, Programmer is the subclass and Employee is the superclass. Relationship between two classes is Programmer IS-A Employee.It means that Programmer is a type of Employee.




class Employee{
 int salary=40000;

}

class Programmer extends Employee{
 int bonus=10000;
  
 Public Static void main(String args[]){
   Programmer p=new Programmer();
   System.out.println("Programmer salary is:"+p.salary);
   System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Output:Programmer salary is:40000
       Bonus of programmer is:10000
      
Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language,multiple inheritance is not supported in java.For Example:

class A{
void msg(){System.out.println("Hello");}
}

class B{
void msg(){System.out.println("Welcome");}
}

class C extends A,B{//suppose if it were
Public Static void main(String args[]){
C obj=new C();
obj.msg();//Now which msg() method would be invoked?
}
}
Read More

Tuesday 1 January 2013

Multiple Classes in java source file

// siddhu vydyabhushana // 1 comment
Hi.., happy new year after 3 months of gap i came here to post a powerful feature of java ,that is
multiple classes in a java program
Actually java is a programming language which it run in a platform.

1) Take three classes as

class a() { }

class b() { }

class multiple() {}

2)write program

3)compile ,after u will get 

a.class , b.class  ,multiple.class




PROGRAM CODE: save below code as multiple.java


class a
{
a()
{
int i=10;
System.out.println("value in class a="+i);
}
}
class b
{
b()
{
int i=5;
System.out.println("value in class b="+i);
}

}// no need to declare public access specifier
public class multiple
{
public static void main(String args[])
{
a A=new a();
b B=new b();
}
}

separately we code it as
CLASS A()

class a
{
a()
{
int i=10;
System.out.println("value in class a="+i);
}
}

CLASS B()

class b
{
b()
{
int i=5;
System.out.println("value in class b="+i);
}

}

Read More

Tuesday 4 December 2012

JComboBox in Swings

// siddhu vydyabhushana // 2 comments


jcombobox in swings

Hi ,this is siddhu vydyabhushana am going to explain about JComboBox in swings.It is one of the component in swings which gives you to select one from group but it was purely different from JRadioButton.It will provide dropdown for u ,we will take Items in string and put it into the JComboBox.

EventHandling
change in any state of object we known as event.
state changes will done by below function..........




JComboBox j;
j.addItemListener(new ItemListener()
{
public void ItemStateChanged(ItemEvent e)
{
//data what you want
}
});


jcombobox in swings

EXAMPLE:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//class name combo
public class combo
{

//constructor combo
public combo()
{

//we are accessing c1 from within inner layer so need to be declared as final
final JComboBox c1;

//frame
JFrame j=new JFrame();

//JButton
final JButton b=new JButton("what you select?");
String s[]={"javatyro","siddhu","vydyas"};

c1=new JComboBox(s);


c1.setBounds(10,10,150,30);
b.setBounds(10,50,150,30);
j.setSize(200,200);
j.setLayout(null);
j.setVisible(true);

j.add(b);
j.add(c1);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

//Event Handling

b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String str=(String)c1.getSelectedItem();
JOptionPane.showMessageDialog(null,"You select : "+str);  
}
});
}
public static void main(String args[])
{
combo c=new combo();
}
}

OUTPUT SCREENS


jcombobox in swings
Read More

Saturday 1 December 2012

Insert Data into Database using Swings,Applets part-2

// siddhu vydyabhushana // 9 comments
Good evening guys just now i described all functions of java in part 1 .In part-2  am going to give u front end code and explaination..
follow below steps

 PART-1

http://javatyro.blogspot.in/2012/12/insert-data-into-database-using.html


SCREEN SHOT OF MY PROJECT



DOWNLOAD FULL CODING
                                   download



PACKAGES


import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

JText Field component in swings used for input fileds

JTextField jt1=new JTextField(20);
JTextField jt2=new JTextField(20);
JTextField jt3=new JTextField(20);
JTextField jt4=new JTextField(20);

JLabel

JLabel jlb1=new JLabel("Username:");
JLabel jlb1=new JLabel("password:");
JLabel jlb1=new JLabel("confirm password:");
JLabel jlb1=new JLabel("email:");

JRadioButton



JRadioButton m, f;
ButtonGroup radio=new ButtonGroup();

 m=new JRadioButton("male");
radio.add(m);//adding item into JFrame

f=new JRadioButton("female");
radio.add(f);//adding item into JFrame

JButton


JButton button=new JButton("Submit");

Adjust  OBJECTS positions in JFrame

name.setBounds(int x,int y,int height,int width);

jlb1.setBounds(40,50,70,30);//adjust label to co-ordinates
jlb2.setBounds(40,90,70,30);//adjust label to co-ordinates
jlb3.setBounds(40,130,70,30);//adjust label to co-ordinates
jlb4.setBounds(40,170,70,30);//adjust label to co-ordinates

jt1.setBounds(130,50,100,30);//adjust JText Field to co-ordinates
jt2.setBounds(130,90,100,30);//adjust JText Field to co-ordinates
jt3.setBounds(130,130,100,30);//adjust JText Field to co-ordinates
jt4.setBounds(130,170,100,30);//adjust JText Field to co-ordinates

m.setBounds(40,210,100,30);//adjust JRadioButton to co-ordinates
f.setBounds(140,210,100,30);//adjust JRadioButton to co-ordinates

button.setBounds(80,250,100,30);//adjust JButton to co-ordinates

ADD OBJECTS TO JFrame

j.add(img1);
j.add(jlb1);
j.add(jt1);
j.add(jlb2);
j.add(jt2);
j.add(jlb3);
j.add(jt3);
j.add(jlb4);
j.add(jt4);
j.add(m);
j.add(f);
j.add(button);
j.add(text);

EVENT HANDLING

button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//data here
}
});

VALIDATIONS

String name=jt1.getText();
String pass=jt2.getText();
String cpass=jt3.getText();
String email=jt4.getText();
String g=text.getText();

if((name.length()<6)||(name.length()>=15))
{
JOptionPane.showMessageDialog(null,"name field lessthan 15 and greaterthan 6 ");
jt1.setText("");
}
else if(!pass.equals(cpass))
{
JOptionPane.showMessageDialog(null,"password didn't matched");
}
else if(email.length()<10)
{
JOptionPane.showMessageDialog(null,"enter your correct id");
}
else if((m.isSelected()==false)&&(f.isSelected()==false)){
JOptionPane.showMessageDialog(null,"Please select radio button");
}
}

HOW TO POP UP DIALOG


JOptionPane.showMessageDialog(null,"Please select radio button");
Read More

Insert Data into Database using Swings,Applets part-1

// siddhu vydyabhushana // 14 comments
Hi guys this is siddhu vydybhushana. Already i have blog (MY BLOG )but this time i concentrated on java so i create another blog and named JAVATYRO ..!!!!

I used  below topics to complete project

1.Swings
2.AWT
3.JDBC
4.Event Handling

Swings
swing is a part of JFC(java foundation classes).It gives smooth interface than AWT but we need to entire program in java.

JFC
java foundation classes are set of GUI(graphical user interface) components which simplifies the development of desktop applications..

AWT
adstract window toolkit used to develop GUI apps in java we can convert this to web applications also.

WHY EVENT HANDLING?
if we observe any change in state of an object we call it as EVENT . for example button pressing,mouse dragging,dialog showing .........etc
These events are provided by java.awt.event package
it will provide many event classes and listeners




now it is time to discuss about JDBC connection
already in my last post i blogged it please refer the below link to know that

                types-of-jdbc-drivers...

in next part i will go through on coding dont miss it ..

PART-2































Read More

Monday 26 November 2012

Types of JDBC Drivers

// siddhu vydyabhushana // Leave a Comment
my first post ON JDBC is 

JDBC TUTORIAL PART-1

there are four types of drivers in jdbc ,what is the use of 4 drivers, what type of driver we are going to use i will explain .


1)JDBC-ODBC bridge driver

    is used to connect to the database.the JDBC-ODBC bridge driver converts JDBC method calls into ODBC function calls.

2)NATIVE-API DRIVER

3)NETWORK PROTOCOL DRIVER


4)THIN DRIVER





Read More