Saturday, 15 September 2018
Thursday, 13 September 2018
What exactly does @Override do in Java?
@Override is annotation which is used to make compiler check all rules of overriding.If we don't write @Override over the method, then compiler will apply all overriding rules only if below three point are :
1. There are super class and sub class relation.
2. Method names are same.
3. Parameters are same.
But if we have written as @Override on sub class method, then compiler must apply all rules irrespective of above three points.
Below are the examples which will explain the use of @Override annotation.
1. Example without @Override annotation.
In the above example, there is no compile time and run time error. We will get output.
Child is called.
2. Example with @Override annotation.
In the above example, there is compile time error when we use @Override annotation that is we can not override private method.
1. There are super class and sub class relation.
2. Method names are same.
3. Parameters are same.
But if we have written as @Override on sub class method, then compiler must apply all rules irrespective of above three points.
Below are the examples which will explain the use of @Override annotation.
1. Example without @Override annotation.
In the above example, there is no compile time and run time error. We will get output.
Child is called.
2. Example with @Override annotation.
In the above example, there is compile time error when we use @Override annotation that is we can not override private method.
Sunday, 9 September 2018
Can we Override static methods in Java
The process of implementing the super class method in sub class is called as method overriding. As per Java principle, classes are closed for modification. so if we want to modify a method in any class, changing existing method is not a good idea instead of that we should extend that class and override method in the child class.If we try to override the method which is static, compiler won't give any error but we will get the output that does not satisfy the definition of overriding.
For example, we have two classes Animal and Cat, in Animal class, we have declared two methods, one is static and another is not static and in Cat class, we have overridden the Animal class methods.
From the output of the below programs, we can say that static methods can not be overridden.
Output :
Animal class : The static method in Animal
Cat Class : The instance method in Cat
Saturday, 8 September 2018
Can we Override private methods in Java.
We can not override the private methods because scope of the private access specifier is only within the class. By the definition of overriding,when we want to override something, then there should be inheritance concept means we should have parent and sub class.When super class method is private that will not be visible to subclass, whatever the methods we are writing in sub class will be treated as new method and not a overridden method.
For example, We have written a private method in parent class and same method, we are trying to access in child class but compiler giving the error, thus we can say that we can not override private method.
For example, We have written a private method in parent class and same method, we are trying to access in child class but compiler giving the error, thus we can say that we can not override private method.
Saturday, 1 September 2018
How to create SharedPreference in Android.
Create one class
here is complete code how to create SharedPreference
here is complete code how to create SharedPreference
public class SessionManager { SharedPreferences pref; SharedPreferences.Editor editor;// Editor for Shared preferences Context _context; int PRIVATE_MODE = 0; private static final String PREFERENCES = "Preferences"; public static final String UNAME = "uname"; public static final String PNAME = "pname"; public static final String TIME = "time"; public static final String DATE = "date"; public static final String ADDRESS = "address"; private static final String PREF_NAME = "newApp";
public SessionManager(Context context) { this._context = context; pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); editor = pref.edit(); } public void saveDEtails(String uname, String pname, String time,
String date,String address) {
editor.putString(UNAME, uname); editor.putString(PNAME, pname); editor.putString(TIME, time); editor.putString(DATE, date); editor.putString(ADDRESS, address); editor.commit(); } public HashMapgetUserDetails(){ HashMap user = new HashMap ();
user.put(UNAME, pref.getString(UNAME, null)); user.put(PNAME, pref.getString(PNAME, null)); user.put(TIME, pref.getString(TIME, null)); user.put(DATE, pref.getString(DATE, null)); user.put(ADDRESS, pref.getString(ADDRESS, null));
return user; } }
// And where you want to stored the data in SharedPreference
mSession.saveDEtails(nameu,namep,time,weddingdate,add);SessionManager mSession;mSession = new SessionManager(getApplicationContext());//Here you can get value from SharedPreference
SessionManager mSession;HashMapuser; mSession = new SessionManager(getApplicationContext()); user = mSession.getUserDetails(); String username = user.get(SessionManager.UNAME); // get name String partnername = user.get(SessionManager.PNAME); // get email String time = user.get(SessionManager.TIME); String date = user.get(SessionManager.DATE); String address = user.get(SessionManager.ADDRESS);
Creating JDBC Connection in Java
Following are the steps to create JDBC connection in Java.
2. Create connection object.
3. Create statement object.
4. Executing queriesoswd
5. Close connection.
Below are the code :
import java.sql.*; class MysqlCon{ public static void main(String args[]){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","admin"); //here test is database name, root is username and admin is password
Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()) { System.out.println(rs.getInt(1) + ":" + rs.getString(2) + ":" + rs.getString(3)); } con.close(); }catch(Exception e){ System.out.println(e);} } }
Subscribe to:
Posts (Atom)