Showing posts with label packages. Show all posts
Showing posts with label packages. Show all posts

Monday, 5 February 2018

Kotlin: Packages

Package is a name space that provides a way to organise interfaces, classes, functions in a way to avoid naming collisions.

Packages are like Directories. For Example, you can keep all the audio files in one directory, video files in other directory and text files in one directory. Similarly, A project can contain several files, so it is always good to organise them in consistent way. 

Benefits of Packages
1. Packages offers modularity, like we can logically group the things, for Example, all file related classes and interfaces can be in package named “file”, all the system related classes and interfaces in package named “system”, all the Graphical user interfaces classes can go to ‘userInterface’ package etc.,

2.We can apply access restrictions using access specifiers protected,private, internal and public.

3. Name Space Collisions avoided

4. Programmers or users can easily determine which classes are related based on package grouping

How to Create a Package
By using package keyword, you can define a package.

Syntax
package packageName;

for example, you can place all the data structure related classes and interfaces in package named ‘collections’.

Ex
package collections;

The package statement must be the first line in the source file.

How to create a package in Eclipse?
Right click on Kotlin project -> New -> Package
It opens below window, give the package name as ‘com.sample.model’.
Click on Finish button. You can see the updated project structure.
Define a class in the package ‘com.sample.model’
Right click on the package 'com.sample.model' -> New -> Other
Select Kotlin -> Kotlin Class
After that it opens a window, there give the class name as Employee and press Finish button.

How to import the package into other classes?
By using ‘import’ statement we can import other packages.

Ex
import com.sample.model.Employee

Find the below working application.

Employee.kt

package com.sample.model

class Employee {
 lateinit var firstName: String
 lateinit var lastName: String
 lateinit var id: String
}

HelloWorld.kt

import com.sample.model.Employee

fun main(args: Array<String>) {
 var obj = Employee()

 obj.firstName = "Chamu"
 obj.lastName = "Gurram"
 obj.id = "Id1234"

 println("First Name : ${obj.firstName}")
 println("Last Name : ${obj.lastName}")
 println("Id : ${obj.id}")

}

Output

First Name : Chamu
Last Name : Gurram
Id : Id1234


Previous                                                 Next                                                 Home

Wednesday, 26 March 2014

Packages

Package is a name space provides a way to organize interfaces, classes in a way to avoid naming collisions.

Packages are similar to Directories. For Example, All the audio files in one directory, video files in other directory and text files in one directory. Similarly A project can contain several files, so it is always good to organize them in consistent way. Packages used to Organize the classes and interfaces.

Prevoius                                                 Next                                                 Home

Friday, 7 February 2014

How to define a method

A method is a programmed procedure that is defined as part of a class. A class (and thus an object) can have more than one method.

Syntax of method definition
    modifier returnType methodName(parameters) Exception List{
        Method Body
    }

Method definition composed of six components :

modifiers : like private, public, protected or default. We have already seen private, public and default. Will discuss more about these in the packages section.

ReturnType : The type of the value written by the method. If you don't want to return any value make the return type as void. Return type will be any primitive type or reference type.

MethodName : The name given to the method

parameters: Method is defined with parameters or with out parameters. A method can be defined with any number of parameters.

Exception List: A Method can throw any number of exceptions, will discuss about this on ExceptionHandling section

Method Body: The actual implementation of the method goes here

Example
    1. public int sum(int a, int b){
        return (a+b);
        }

        modifier : public
        returnType : int
        methodName : sum
        parameters : a,b
        Exception List : none
        Method body : calculates the sum of the two parameters and return the sum.

2. void sayHello(){
        System.out.println(“Hello”);
    }

    modifier : default
    returnType : void
    methodName : sayHello
    parameters :none
    Exception List : none
    Method body : simply prints the message “Hello” to the console.

If no access specifier (modifier) specified to a variable or method, it assumes default access specifier(modifier).

Naming a Method
By convention method names starts with a small character and starting character of subsequent words is capitalized.

    Example:
    walk()
    sleep()
    setColor(String s)
    getHeight()

Local Variables
Variables declared inside a method are called local variables. These are not accessible out side of the method. You must initialize local variables before using.

Some Points to remember
Trying to use local variables before initializing causes compile time error

Example
class LocalVariable{
 int sum(int a, int b){
  int c;
  System.out.println(c);
  return (a+b);
 }
} 

Since “c” is a local variable, when your program tries to access local variable, before initialization, compiler will throw the error.
 
For the above program, you will get below error
LocalVariable.java:5: error: variable c might not have been initialized
            System.out.println(c);
                                          ^
1 error





Instance methods                                                 More about methods                                                 Home