Hot

Sharly

Friday, August 23, 2013

Method Overriding



A subclass inherits the methods from the super class and redefines it by filling it with its own instructions. This is done by defining methods in the sub class that has the same name arguments and same return type as the super class’s methods.

This is one method of achieving polymorphism.


Methods

There are two types

Instance Methods

They work with the  object instance. An instance method is called by referring to an object name.


Ex:
            Acc1.deposit (200);


Class Methods

A class methods works on the class not on the objects. It can be called by referring to class name. Class methods are defining by using keyword static.



Class Variables



A class variable exists in only one place. It keeps a single value for the class  and all its object instance . Class variables are useful to keep  class wide information. (Information  which is common to the class and all its objects)


Ex;

Public class familyMember
            {
                        Private string FirstName;
                        Private int Age;
                        Private static string Surname = “Perera”
                        …………..
.                       ………..
            }

Member 1                                Member 2

FirstName  Ajith                       FirstName Amal
Age  27                                    Age  30
Surname  Perera                       Surname  Perera



Visibility scope of methods and variables

1.         Public

            Public methods and variables can be access from any class


2.         Protected

Protected variables and methods can be access by the defining class and the its sub classes only. In java protected variables and methods cab be access by the defining class, any class  within the package (a package is a group of related classes) and sub classes any where


3.         Private

Private variables and methods can be  access by the methods of the defining class only, to support encapsulation, instance variables are normally declared private. Methods are normally declared public. But a method can be private when it performs a support role to another method and has no direct relationship to the outside world.


4.         Package/default

This is a scope  available in java. Methods and variables  declared as a package can be access by any class within the package.

Constructors



A special method within a  class which gets  executed automatically at the time of creating the object. It is used  to store  initial values  within the objects instance variables. The name of the constructor is  same as the class’s name. A constructor is same as the class’s name. A constructor can not  return any values. It is called automatically when you create an object instance.


“this” Keyword

It is used to refer to the current object instance.

Instance variables
 Varibles that contain different values foreach object instance. An instance variable may be implemented as a primitive data type or as an object

Ex:

Public class student
            {
Instance variables
 
                        Private string name;                  
                        Private int marks;
                        …..
                        …..
            }


Student 1                                  Student 2
Name Sam                               Name Jane
Marks 80                                 Marks  90

Objects of student class




Constants, Variables, and Data Types



Constants

Fixed values that do not  change during the execution of a program. Java supports several  types of  constants.

Integer  -           An integer constant refers to a sequence of digits. There are three
types of integers.


·        Decimal integer Ex:       137
·        Octal integer                 Ex:       037
·        Hexadecimal integer      Ex:       ox2

Real                             Ex:       0.0083


Character                     Ex:       ‘X’


Variables

A variable is an identifier that denotes a storage  location used to store a data value Unlike  constants that  remain  unchanged  during the execution  of a program. A variable may take different values at different  times  during  the execution  of the program.

Some variables names are:

            Mean
            Medium
            Height


Data Types

Every variable  in Java has a data type. Data types in java  under various   categories

  • Integer Type
  • Floating Point Type
  • Character Type
  • Boolean Type