Class Rectangle

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

class Rectangle

                int length;           //instance members

                int breadth;

Every object has its seperate(own) copy of instance members.

class Demo

                public static void main(String [] args)

                {

                                Rectangle  r1 = new Rectangle();

                                Rectangle  r2 = new Rectangle();

                                r1.length = 4;

                                r2.length = 7;

                                S.o.p(r1.length);              //4

                                S.o.p(r2.length);              //7

                }

--------------------------------------------------------

class Rectangle
{

                static int length;               //class member

class Demo

                public static void main(String [] args)

                {

                                Rectangle  r1 = new Rectangle();

                                Rectangle  r2 = new Rectangle();

                                r1.length = 4;

                                r2.length = 7;

                                S.o.p(r1.length);              //7

                                S.o.p(r2.length);              //7

                }

---------------------------------------------------------

Constructors

class Rectangle

                int length;           //instance members

                int breadth;


 

                Rectangle( )

                {

                                System.out.println("My Constructor \n");

                                length = 5;

                }

class Demo

                public static void main(String [] args)

                {

                                Rectangle  r1 = new Rectangle();

                                Rectangle  r2 = new Rectangle();                             

                                new Rectangle();                            

                                S.o.p(r1.length);              //5

                                S.o.p(r2.length);              //5

                }

---------------------------

class Rectangle
{

                int length;           //instance members

                int breadth;

                Rectangle(int l ,int b)

                {

                                System.out.println("My Constructor \n");

                                length = l;

                breadth = b;

                }

public class ConstructorDemo { 

public static void main(String [] args)

                Rectangle  r1 = new Rectangle(5 , 8);

                Rectangle  r2 = new Rectangle(6 , 3);                     

                Rectangle  r3 = new Rectangle();

            

        System.out.println(r1.length +" " + r1.breadth);

        System.out.println(r2.length +" " + r2.breadth);

                }   

If user provides parameterised constructor; then compiler does not provide the default
constructor.

---------------------------------
Constructors can be overloaded

class Rectangle

                int length;           //instance members

                int breadth;

        Rectangle()

        {

            System.out.println("Default Constructor ");

        }

                Rectangle(int l ,int b)

                {

                                System.out.println("My Parameterised Constructor \n");

                                length = l;

                breadth = b;

                }

public class FunctionDemo {         

public static void main(String [] args)

                Rectangle  r1 = new Rectangle(5 , 8);

                Rectangle  r2 = new Rectangle(6 , 3);                     


                Rectangle  r3 = new Rectangle();

            

        System.out.println(r1.length +" " + r1.breadth);

        System.out.println(r2.length +" " + r2.breadth);

                }   

-------------------------

class Rectangle

                int length;           //instance members

                int breadth;

        static int m_s;

       

        {

            System.out.println("Instance Initialiser Block");

            length = 1;

            breadth = 1;

        }

        

        static

        {

            System.out.println("Static Initialiser Block");

            m_s = 4;           

        }

        Rectangle()
        {

            System.out.println("Default Constructor ");

        }

                Rectangle(int length ,int b)

                {

                                System.out.println("My Parameterised Constructor \n");

                                this.length = length;

                breadth = b;

                }

        static void statfunc()

        {

            //length = 5; //does not work

            m_s = 1;

            System.out.println("Static Function");

        }

        void nonstatfunc()

        {

            m_s = 2;

            length = 5; //works

            System.out.println("Non - Static Function");

           

        }      

You might also like