Tuesday, 11 June 2013

Block ,classes

Block
Definition: Block is a group of statements.
There are 2 types of blocks:
1.      Anonymous Blocks
Anonymous blocks are those having no name. So, we can’t call them in a program. They can be executed in main().
2.      Named Blocks
Named blocks are those that have name and perform a specific task.
Sometimes, we can call them as methods. There are 4 types of methods/blocks:
1.      Methods with no arguments and no return value.
2.      Methods with arguments and no return value.
3.      Methods with arguments and return value.
4.      Methods with no arguments but return value.
Format – Method with no arguments and no return type
access_specifier no_return_type(void) method_name()
{
}
e.g.
public void show()
{
}
Format – Method with arguments and no return type
access_specifier no_return_type(void) method_name(arg1, arg2, …)
{
}
e.g.
public void show(int x, int y)
{
}
Format for calling a method
object_name.method_name();
Format – Method with arguments and return type
access_specifier return_type method_name(arg1, arg2, …)
{
            //Statements;
            //return statement;
}
Format – Method with no arguments but return type
access_specifier return_type method_name()
{
            //Statements;
            //return statement;
}
Class
Definition - Class is a container which contains data and methods.
Note:
·         Class can be called as an extended part of structure
·         Structure =  User defined
·         Structure can be used to declare different kinds of variables
·         Default access specifier for structure is public
·         Dynamic initialization à Initialization at the time of declaration

Constructors
Constructors are special member functions used to initialize the member variables of the class. They can’t return any value. Even we can’t specify void with them. They are created automatically when we create an object of the class.
They are of 2 types:
1.      Default – those in which we do not pass arguments
2.      Parameterized – those in which we can pass arguments
Note:
Constructors have the same name as class name.
Note (Difference b/w functions and constructors):
·         Need to call functions
·         Need to create objects for each constructor
Nested Functions
Definition: Nested function refers to calling function inside another function.

Array
Definition: Array is a group of elements sharing a common name.
Note:
Array indices always start from 0.
Syntax:
datatype []array_name = new datatype[size];
or
datatype array_name[] = new datatype[size];
e.g.
int []rollno = new int[5];
Here,
·         int àInteger type array
·         [] à 1-d array
·         rollno à array name
·         5 à static array
Dynamic Array
Definition: Dynamic array refers to initialization at the time of declaration.
e.g.
int []rollno = {10,20,30,40};
Loop
for(int i=0;i<rollno.length;i++)
{
}
e.g.
Ques. WAP to:
·         Create an array having 10 elements
·         Find the greatest and smallest number in it.

Ques. WAP to:
·         Enter marks of 3 students in 3 subjects
·         Find total marks and average of each student
·         Find minimum and maximum marks of each student and also tell the subject

Ques. WAP to:
·         Enter 4 cities and their minimum and maximum temperatures
·         List the extreme minimum and maximum temperatures with their city names

Inheritance
Inheritance is the process in which we have more than one classes. We can get the properties and methods of 1 class into another class. 1st class is known as parent class and 2nd is known as child class. It provides the feature of reusability.
In java, we can do 3 types of inheritance:
1.      Single
2.      Multi-level
3.      Multiple

1.      Single
In this, we have 1 parent and 1 child class. The child class gets the properties and methods of the parent class.
Format:
class demo
{
}
class demo1 extends demo
{
}
class maindemo
{
public static void main(String aa[])
{
            demo1 obj = new demo1();     //object of child class is made
}
}
Constructor in Single Inheritance
Note:
We can’t inherit constructors.
Default constructor in Inheritance:
In single inheritance, when we use default constructor in classes, parent class default constructor always executes 1st. It is because child class default constructor always indicates to parent class default constructor.
Parameterized constructor in Single Inheritance
Note:
When we call the parameterized constructor of the child class, compiler will execute the default constructor of base class and parameterized constructor of child class. If we want to execute the parent parameterized constructor, then we need to use super keyword. When we use super keyword, it calls parameterized constructor of parent class and the compiler will not execute the default constructor of parent class.
super statement is used as 1st statement always and it’s used in parameterized constructor of child class.

Ques. WAP to:
·         Show single inheritance with 2 classes named principal and teacher.
·         Principal class has variables – name and salary
·         Teacher class has variables – designation, subject and class
·         Inherit principal class in teacher class.

No comments:

Post a Comment