Posts

Showing posts from 2014

Method overloading,overriding and need of it,Concept of object

Image
Method overloading : Let me get this straight,method overloading simply means same method with different parameters either of different data-type or different parameter size.Return type must be same. Here's the example: Valid overloading of methods: void foo():nothing in the parameters list void foo(int num):one  parameter of type int void foo(int a,int b):two parameters of type int void foo(float a,float b): two parameters of type float void foo(int num,float tot): two parameters of type float and int In-Valid overloading of methods: If the parameters of the at-least one method is exactly the same invalid overloading. Need of overloading methods?? Method overloading is needed because there are situations where a single method have to do several kind of work.Suppose there is a method name calc_area that calculates area of different figures such as: Circle:single parameters with float value  calc_area(float r). Triangle:Two parameters(base and ht,) with float va

Exceptions and exceptional handling

Exceptions: What are exceptions you may ask,exceptions are flaws in the programs that are to be taken care of.They disturb the normal flow of the program and creates serious problems if not handled properly.They can be run-time or compile time or you may say unchecked or checked exceptions.But what are errors then,exceptions that cannot be recovered  are called errors.For example:Programmer has declared an array of objects of string class of size n,but has inserted greater than n value,its an array-index-out-of-bound exception,or if called the method of a class that neither exist on the package.There are lots of predefined exceptions in java including them.  There are basically two types of exceptions as I said,checked and unchecked exceptions. Checked exceptions are notified while compiling the program,i.e. the compiler checks that it would cause further problem,that's why they are called checked exceptions.Examples are:IOExeption,sqlException. Unchecked exceptions are non-

Inheritance,its requirement,use and implementation

Image
Inheritance Inheritance example:a tree Inheritance is the most interesting feature of java and other Object oriented programming language.It is the only reason why i hate "C". Firstly,lets know what inheritance is??As the word suggest means taking the features....but of what??A class or an interface.But what are the features???The variables and the method the class or interface contains. Inheritance takes the principle of re usability.Using the previously written code for a class without rewriting it again. Just extend the class,that's it.The class which is taking the feature of another class is called child class and the class from which features are taken is called parent or base class. It per the rules of humanity that child cannot be born parents,thus child class constructor is executed after the parent class constructor by default(discussed later). Remember single class can extend single class through inheritance. Requirement No one likes to rewrit

Abstract class,Interfaces and its implementations

Image
Abstract class We need abstract class for the sense of simplicity.Learn it this way,suppose there is a college administration with over 100 of persons working in different sections namely,Head of department,teachers of diff. subjects,accounts department staff,mess staff,cleaners etc. Each have their own role in the college.But,here's the situation,what if the different department(classes) want the similar thing,for example,retrieving student info or getting the salary.We have to make method with similar motive,for each and every class we make,WHAT A WASTE!!! Abstract class comes to the rescue.  Abstract class is nothing but a normal class with some restrictions. Well,these restrictions are: At least one of the method/s must be abstract. You cannot make an object of an abstract class,i.e.,no new keyword before the class name.But the sub-classes of an abstract class is allowed. An abstract method MUST NOT HAVE A BODY.For example:void method(arguments);// there is no braces

The mystery keyword static,final

The keyword static Most of the new java developers are often confused or find difficulty to understand what  keyword static really is. Java API is full of static methods and variables. I will explain it clearly, There are three main reasons why "static" is used: To does not bother about creating an object and accessing methods or data,directly calling the variable or method. To  make the instance variables belonging to whole class rather than binding to a particular object,i.e,if a Class A has static variable a,and A has two instance a1 and a2 with instance variable value then the values are same. To allow sharing of data to all the objects accessing static variable. For c ount the number of objects cr eated in the p rogram as static variables are share d among objects. Where static keyword are used: For making methods static For making class variables static also called static variables For making nested class static. 1.Making methods static:Type stati

Concept of access specifiers,Simple programs in java

Access specifiers in java: What are access specifiers:They are keywords that restricts the access of the instance variable(data) and methods within a package,inherited classes or within the class There are basically 4 access specifiers in java: public:Gives access is in the package,other classes,within the class but cannot be accesses in another package.Free access to the variable in the whole project to be summed. private:Most restrictive,can access variable within the class only.Used for saving critical data. protected:Data and methods are access within the classes that are inherited to the current class only and within class access. default:Data and methods are accessible withing your package/project Simple programs in java Program to find the largest between two numbers: Code: class A { public static void main(String args[]) { int a=5; int b=10;           if(a>b)             {            System.out.println("a is greatest");             }    

Control Statements in java

Image
Control Statements in java There are three control statements in java: Selection  statements Iteration statements or looping statements Jump statements Break statement 1.Selection statements: if-else statements switch case statements Which path to choose:your choice 1.if-else statements: With if only: Syntax:if(condition) { statement1; statement2; statement3; statement4; .......... } With if-else statements: Syntax: if(condition) { statement1; statement2; statement3; statement4; .......... } else { statement1; statement2; .......... }  Here condition is a conditional statement containing the condition operators only such as <,>,>=,<=,==,!=. Example:if(age>=18) {//if the age is greater than 18 then print this System.out.println("You are allowed to vote"); } else { System.out.println("Grow up!!wait for some years.."); } Take care of the curly brackets that