Java Classes Tutorial Part-1

 Java Class Basics

 Part-2 

coding-zon-java-classes-part1

Table of Contents

  1. Introduction of Oops
  2. Class definition
  3. Class Members
  4. Creating Objects in Java
  5. Using methods
    1. Using methods with Parameters
  6. Using Constructors
    1. 'this' keyword in Constructor

 

 

 Introduction

In Object-Oriented programming, all the programs are written, in the form of classes.
Using the classes, objects are created.


Basically, there are 4 Oops concepts are available.
In every object-oriented programming languages, you will find these concepts.
All these concepts are implemented in the classes.


1. Abstraction------Hiding the complexity.
2. Encapsulation----Binding data and function together in a s single entity(class).
3. Polymorphism-----Many forms.
4. Inheritance.-----Reusability.

 

Class definition:

Class is a collection of attributes and methods.
 

More about Class

 

  1.  A class also called as a user defined datatype.
  2. Class is a template for an object.
  3. Class is virtual, objects contains actual data.
  4. Class starts with the reserved word called 'class'.
  5. Class name must start with a capital letter.
  6. A Class contains a state and behavior. Where,
    1. State represents attributes of  the class
    2. Behavior  means methods of the class

 

Menu Top

Structure of the Class:


class Classname{
    
    attributes
    methods
}

 Attributes are used to store data
 Methods are used to perform some operations
 

Empty Class

syn:

class Classname{}

 
Ex:

class Person{}

 

Class Members

Along with  attributes and methods, a class can have the other elements such as
 

  •  Attributes
  •  Methods
  •  Constructors
  •  Blocks
  •  Nested class and Interface

 

Class Diagram:

  

coding-zon--Java-class-diagram.jpg

 

Class example:


class Person
 

    String  pname; // instance variable

    String  pcity; // instance variable

}

Instance variable in Java

By default, attributes defined in the class are  instance variable. Instance variable doesn't take  memory at compile time. It gets memory at runtime, when an object/instance is created. That is the reason it is called as an instance variable.


 Menu

Creating Objects in Java:

Also called instantiating class.

new operator:

Used to create an object.
 
 

ex:

Person p1 = new Person (); 

or

 

Person p1;                       //object reference.

p1 =   new Person ();     //Object in memory created by new and pointed by ref. p1.

 

Diagram:
 


 System.out.println(p1.pname);  //null
 System.out.println(p1.pcity);   //null
 

code example:

Printing attributes using object.

 

 
class Person{ 

    String pname; // instance variable
    String pcity; // instance variable
 

public static void main(String args[])
{ 

    Person p1 = new Person ();
 
    System.out.println(p1.pname); // null
    System.out.println(p1.pcity) ; //null 
}

}  
 
 
output:
null
null 
 

Using methods

Methods will perform some operation on data members(attributes) such as setting data to attributes, printing data attributes etc.

The below code example shows how to display attributes using methods.


class Person{ 

    String pname; // instance variable
    String pcity; // instance variable
 

void displayName(){
    System.out.println(pname);
}
 

void displayCity (){
    System.out.println(pcity);
}

public static void main(String args[])
{ 

    Person p1 = new Person ();
    p1.displayName () ; // null
    p1.displayCity () ; //null 
}

 

 


 Move to Menu

Using methods with Parameters

In order to set the data to an attributes using methods you need to pass parameters to a methods.

In the below code 

setName() method -- taking name parameter and assigning it to an attribute pname. Next

displayName() method -- displays the pname. similarly 

setCity(), displayCity() works on attribute pcity to set the value and display the value.

 
 
class Person{
  
 
  String pname;
  String pcity;

  // method to set the name 
  void setName(String name) {
    pname= name;
   

  }
 // method to display pname
  void displayName() { 
    System.out.println(pname);

  }

  // method to set the pcity
  void setCity(String city){
    pcity= city;
   

  }
  // method to display pcity
  void displayCity() { 
    System.out.println(pcity);

  }

  public static void main(String[] args) {

// create an object of Person

    Person p1 = new Person();

// using object, invoking methods.

    	p1.setName("John"); 
	p1.displayName();
 
       p1.setCity("CA");
	p1.displayCity();
  }
}
 
output:
C:\javabasics>java Person
 
John
CA 
 
 
  

Using Contructors

  • Constructors are used to create, initialize the objects. 
  • Constructors are nothing but a method but having class name.
  • Constructors called at the time of object creation with new operator.
  • Constructors and doesn't have return type void, int..etc.
  • Each class will have one default constructor.
 

Example:

This is example shows, calling different constructors by sending different values with an object. 

 
class Test
{
    int a;
    int b;
    int c;
    int d;
public Test()
{
    a = 0;
    b = 0;
    c = 0;
    d = 0;     
}
public Test(int x, int y, int z)
{
    a = x;
    b = y;
    c = z;
 
    d = a + b + c;
    System.out.println("Sum of 3 numbers = "+d);
 
 
 }
public Test(int x, int y)
{
    a = x;
    b = y;
    d = a + b; 
    System.out.println("Sum of 2 numbers = "+d);
}
 
public static void main(String args[])
{
    Test t = new Test(87,23,90);
    Test t1 = new Test(87,23); 
}
}
 
 

 Using 'this' keyword in Constructor

    
 

this indicates the current object variables.

The same above example we can rewrite using this operator.

 
Example: 
 
class Test
{
    int a;
    int b;
    int c;
    int d;
public Test()
{
    a = 0;
    b = 0;
    c = 0;     
    d = 0;     
}
public Test(int a, int b, int c)
{
    this.a = a;
    this.b = b;
    this.c = c;  
    d = a + b + c; 
    System.out.println("Sum of 3 numbers = "+d); 
}
 
public Test(int a, int b)
{
    this.a = a;
    this.b = b;
    d = a + b;  
    System.out.println("Sum of 2 numbers = "+d);
}
 
public static void main(String args[])
{
    Test t1 = new Test(87,23,90);
    Test t2 = new Test(87,23); 
}
} 
 
 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP