Saturday, 24 September 2016
Tuesday, 30 August 2016
Monday, 29 August 2016
Aggregation in Java
Aggregation in Java
If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationship.
Consider a situation, Employee object contains many informations such as id, name, emailId etc. It contains one more object named address, which contains its own informations such as city, state, country, zipcode etc. as given below.
- class vipul{
- int id;
- String name;
- Address address;//Address is a class
- ...
- }
When use Aggregation?
- Code reuse is also best achieved by aggregation when there is no is-a relationship.
- Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects involved.
- aggregation is the best choice.
Example
- class one{
- int square(int n){
- return n*n;
- }
- }
- class two{
- Operation op;//aggregation
- double pi=3.14;
- double area(int radius){
- op=new one();
- int rsquare=op.square(radius);//code reusability (i.e. delegates the method call).
- return pi*rsquare;
- }
- public static void main(String args[]){
- two c=new two();
- double result=c.area(5);
- System.out.println(result);
- }
- }
Sunday, 28 August 2016
Inheritance in Java
Inheritance in Java
Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.
The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.
Syntax
public class vipul
{
}
public class Agravat extends vipul
{
}
The extends keyword 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 super class. The new class is called a subclass.
class vipul{
float salary=200;
}
class Agravat extends vipul
{
int bonus=100;
public static void main(String args[]){
Agravat A=new Agravat();
System.out.println("Programmer salary is:"+A.salary);
System.out.println("Bonus of Programmer is:"+A.bonus);
}
}
OUTPUT
Programmer salary is:200
"Bonus of Programmer is:100
Saturday, 27 August 2016
This keyword in java
This keyword in java
There can be a lot of usage of java this keyword. In java, this is a reference variable that refers to the current object.
Usage of java this keyword
Here is given the 6 usage of java this keyword.
- this keyword can be used to refer current class instance variable.
- this() can be used to invoke current class constructor.
- this keyword can be used to invoke current class method (implicitly)
- this can be passed as an argument in the method call.
- this can be passed as argument in the constructor call.
- this keyword can also be used to return the current class instance.
If there is ambiguity between the instance variable and parameter, this keyword resolves the problem of ambiguity.
Friday, 26 August 2016
Java Naming conventions, CamelCase
Java Naming conventions
Java naming convention is a rule to follow as you decide what to name your identifiers such as class, package, variable, constant, method etc.
But, it is not forced to follow. So, it is known as convention not rule.
All the classes, interfaces, packages, methods and fields of java programming language are given according to java naming convention.
Advantage of naming conventions in java
By using standard Java naming conventions, you make your code easier to read for yourself and for other programmers. Readability of Java program is very important. It indicates that less time is spent to figure out what the code does.
class name
---------->should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc.
interface name
--------------->should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc.
method name
--------------->should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc.
variable name
----------------->should start with lowercase letter e.g. firstName, orderNumber etc.
package name
------------------>should be in lowercase letter e.g. java, lang, sql, util etc.
constants name
----------------->should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.
CamelCase in java naming conventions
Java follows camelcase syntax for naming the class, interface, method and variable.
If name is combined with two words, second word will start with uppercase letter always e.g. actionPerformed(), firstName, ActionEvent, ActionListener etc.
Thursday, 25 August 2016
OOP(Object Oriented Programming System)
OOPs (Object Oriented Programming System)
Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:
- Object
- Class
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
Object
Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike,Car etc. It can be physical and logical.
Class
Collection of objects is called class. It is a logical entity class is also the name start with class.
class is a an Entity.exmple
public class vipul
{
}
Inheritance
When one object acquires all the properties and behaviours of parent objecti.e. known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
Example
public class vipul
{
}
public class agravta extends vipul
{
}
Polymorphism
When one task is performed by different ways i.e. known as polymorphism. For example: to convense the customer differently, to draw something e.g. shape or rectangle etc.
In java, we use method overloading and method overriding to achieve polymorphism.
================overriding =======================
public class vipul
{
public void a(){
}
}
public class Agravat
{
public void a();
}
================overloading =======================
public class vipul
{
public void a(int a)
{
}
}
public class Agravat
{
public void a(int a,int b){
}
}
Abstraction
Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing.
In java, we use abstract class and interface to achieve abstraction.
abstraction class vipul
{
}
Encapsulation
Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.