Posts

Access Modifiers in JAVA

Access Modifiers are used to Control the access and to maintain the confidentiality of the classes and it's members. There are two access levels in JAVA where access modifiers are applicable. namely, class level member level In Java we have four access modifiers. namely, Public Protected Default Private Class Level Only Public and Default access modifiers are applicable for class level declaration. public This can be access from anywhere either from the inside of the package or the outside of the package . If a public class needed to be accessed from the outside of the package, it is a must to import that package which contains that class. package test; import access_modifiers . PublicClass ; TestPublic{     PublicClass pc = new PublicClass(); } package access_modifiers ; public class PublicClass { } We are not using the import statement when accessing a class inside the same package. default There is no keyword for the default ...

Keyword 'final' in JAVA

The general meaning of the word 'final is given below as in the google definitions. final ˈfʌɪn(ə)l/ adjective adjective:  final 1 . coming at the end of a series. "the final version of the report was presented" synonyms: last , closing, concluding, finishing,  end ,  ending , terminating,  terminal ,culminating,  ultimate ,  eventual ,  endmost "their final year of study" antonyms: first ,  initial reached or designed to be reached as the outcome of a process or a series of actions and events. "the final cost will easily run into six figures" allowing no further doubt or dispute. "the decision of the judging panel is final" synonyms: irrevocable ,  unalterable ,  absolute ,  conclusive ,  irrefutable , incontrovertible ,  indisputable ,  unappealable ,  unchallengeable , binding ;  More antonyms: provisional ...

Overriding equal method

In java when we use equals method to compare two objects it returns true only if these referring to same memory location. Code public class OverrideEqual{ public static void main(String[] args){ Person p1= new Person("John",17); Person p2= new Person("John",17); if(p1.equals(p2)){ System.out.println("p1 equals to p2"); }else{ System.out.println("p1 is not equals to p2"); } } } class Person{ String name; int age;         Person(String name,int age){ this.name=name; this.age=age; } } Output:  p1 is not equals to p2 So if we want to compare two objects according to the states or values then we have to override the equals method. Code: public class OverrideEqual{ public static void main(String[] args){ Person p1= new Person("John",17); Person p2= new Person("John",17); if(p1.equals(p2)){ System.out.println("p1 equals to p2"); }else{ System.out.println("p1 is not ...

Simple Sorting and Searching Algorithms

Bubble Sort   In bubble sort we compare a adjacent pair of items and swap if they are in incorrect order until all the items are sorted out. Code: public class BubbleSort{ public static void main(String args[]){ int []arr={99,34,23,54,32,10}; bubbleSort(arr); System.out.println("\nSorted Array:"); printArray(arr); } static void bubbleSort(int []arr){ int temp; for(int k=arr.length;k>1;k--){ for(int i=0;i<arr.length;i++){ if(arr[i]>arr[i+1]){ temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } printArray(arr); } } static void printArray(int []arr){ for(int i:arr){ System.out.print(i+", "); } System.out.println(); } } Output: 34, 23, 54, 32, 10, 99, 23, 34, 32, 10, 54, 99, 23, 32, 10, 34, 54, 99, 23, 10, 32, 34, 54, 99, 10, 23, 32, 34, 54, 99, Sorted Array: 10, 23, 32, 34, 54, 99, Linear Search   In linear search we gonna take value by value sequentially until we find the item or until all the items a...

JAVA

Image
Both the program and related documents on that program together is considered as a software. There is three basic stages when we are developing a program in any kind of language. Design Develop Test To develop computer programs we should select a programming language for that. There are many kind of languages that has been evaluated until now. If we categorize these languages as, machine oriented (Low level Languages) problem oriented (High level languages) In first generations of programming languages they used binary values to program software. So the programmer should have very good knowledge about hardware implementation of the system and how does circuits operates. In second generation languages we used assembly language which used mnemonics (Simple English words) to represent simple instructions such as add, store instead on binary instructions which used in first generation. But still the programmer needed better understanding on machines. Third generation languages...

Polymorphism

The ability of an object to behave differently in different forms is called as polymorphism. So the methods have multiple implementations and the compiler or in runtime java will decide which method to execute. If the compiler can decide it, we called it static polymorphism (example: method overloading) while if it is determined in runtime we called it dynamic polymorphism (example: method overriding). Below code is an example for method overriding. Code: public class testPolymorphism{ public static void main(String []args){ Person p1; p1= new Person("John",20); p1.eat(); p1.sleep(); p1= new Student("Merry",16); p1.eat(); p1.sleep(); } } class Person{ String name; int age; Person(String name,int age){ this.name=name; this.age=age; System.out.println(this.name+":"+"I'm a Person"); } void eat(){ System.out.println(this.name+":"+"Eating..."); } void sleep(){ System.out.println(this.nam...

Inheritance

Inheritance is a concept which allows to create an " is-a relationship " in between two classes. It's similar to partent-child relationship. As example think of a student. Every Student is a Person . So Student inherits every behaviour and attribute which a person have. (this depends on access specifier. As example if the access specifier is private then those fields or methods cannot be inherited by its sub classes.) Code: public class TestInheritance{ public static void main(String []args){ Person p1 = new Person("John",20); p1.eat(); p1.sleep(); Student s1= new Student("Merry",16); s1.eat(); s1.sleep(); s1.study(); } } class Person{ String name; int age; Person(String name,int age){ this.name=name; this.age=age; System.out.println(this.name+":"+"I'm a Person"); } void eat(){ System.out.println(this.name+":"+"Eating..."); } void sleep(){ System.out.println(this.name+...