You are on page 1of 8

Class − A class is a template/blueprint

describes the behavior/state that the object and its type


A class is a user defined blueprint or prototype
from which objects are created.
represents the set of properties or methods that are common to all
objects of one type.

Objects have states and behaviors.

Example:
A dog has states - color, name, breed
and behaviors – wagging the tail, barking, eating.
Class dog
{
String color; // declared next to define
String name;
String breed;
wagtail();
bark();
eat();
}
Class myProject{
Class specialDog()
{
int cost;
String color; // declared next to define
String name;
String breed;
wagtail();
bark();
eat();
}

specialdog( )
{
int Cost = 50,000;
string name = “labrador”;
string eat=”foodstuff”;
}

Public static void main(String args[ ] )


{
specialdog objectname = new specialdog( );
type data;
int abc; abc = 50

Object is an instance of a class.


Declaring Objects (called instantiating a class)
Class xyz

Member var;

Main( )

Classname xyz1;

//Classname xyz2;

//Classname xyz3;

this.abc( );
When an object of a class is created, the class is said to be instantiated.

All instances share the attributes and the behavior of the class.

values of those attributes, i.e. the state are unique for each object.

A single class may have any number of instances.

As we declare variable as name , what is the data type of that variable

i.e type name; string name

This tells the compiler that we will use name to refer to data whose type is of
type.

With a primitive variable i.e int number;

This declaration reserves the memory for the variable.

So for reference variable, type must be a class name

public classname (mem var + fucntions)

classname object = new constructor ( )

mem var = 20; int var = 20; 4 bytes of memory


In general, we can’t create objects of an abstract class or an interface.

Example

Dog lab;

If we declare reference variable (lab), its value will be undetermined until an


object is created and assigned to it.

Simply declaring a reference variable does not create an object.

The new keyword instantiates a class by allocating memory for an object and
returning a reference to that memory.

The new operator also invokes the class constructor.

Example

public class Dog


{
// Instance Variables
String name; // dog’s name
String breed; // Breed of the dog
int age; // age of the dog
String color; // color of the dog

// Constructor declaration
//same name as Class name
//variables are initiliased in the constructor

public Dog(String name, String breed, int age, String color)


{
this.name = name;
this.breed = breed;
this.age = age;
this.color = color;
}

THIS is a reference variable in Java that refers to the current object.

used inside the Method or constructor of Class


used to refer instance variable of current class.
used to invoke or initiate current class constructor.
can be passed as an argument in the method call

public String getName() // method 1


{
return name;
}

public String getBreed() // method 2


{
return breed;
}

public int getAge() // method 3


{
return age;
}

public String getColor() // method 4


{
return color;
}

public String toString()


{
return("Hi my name is "+ this.getName()+ "\nMy breed, age and color
are " + this.getBreed()+"," + this.getAge()+","+ this.getColor());
}

public static void main(String[ ] args)


{
Dog lab = new Dog("lab", “breed”, 5, "white");
System.out.println(lab.toString());
}
}
}

public loginform extends WebDriver

String login;

String pwd;

login( );

pwd( ); show pwd or hide pwd or put stars

In general, class declarations can include these components, in order:

1. Modifiers : A class can be public or has default access.


2. Class name: The name should begin with an initial letter (capitalized by
convention).
3. Superclass(if any): The name of the class’s parent (superclass), if any,
preceded by the keyword extends. A class can only extend (subclass) one
parent.
4. Interfaces(if any): A comma-separated list of interfaces implemented by
the class, if any, preceded by the keyword implements. A class can
implement more than one interface.
5. Body: The class body surrounded by braces, { }.

Constructors are used for initializing new objects. Fields are variables that
provides the state of the class and its objects, and methods are used to
implement the behavior of the class and its objects.