Introduction to classes Sangeetha Parthasarathy 06/11/2001.

18
Introduction to classes Sangeetha Parthasarathy 06/11/2001

Transcript of Introduction to classes Sangeetha Parthasarathy 06/11/2001.

Page 1: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

Introduction to classes

Sangeetha Parthasarathy

06/11/2001

Page 2: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

OOPs Concepts OOPs encapulates data(attributes) and methods

(behaviours) into objects. Objects have the property of information hiding. This

means that implementation details are hidden within the object themselves.

Java programmers concentrate on creating their own user-defined types called classes.

Classes are also referred as programmer-defined types.

Page 3: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

Introduction to classes Each class contains data as well as the set of methods that

manipulate the data. The data components of a class are called as instance

variables. The methods or functions are called as interfaces.

Page 4: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

Class Scope

The class instance variables and methods belong to that class’s scope.

Outside a class’s scope, the members are accessible not by class name but by the object name.

The class members which are public are accessible by using the object name.

The class members which are private are accessible by public methods.

Page 5: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

Controlling access to members The are four member access modifiers: public, private,

protected and default. Public modifier: The members with this specifier are

accessible everywhere. Private modifier: The members with this specifier are

accessible only within the class. Protected modifier: Member with this modifier are

accessible in all the child classes in any package. Default: The member are accessible within the default

package.

Page 6: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

Access specifier of class There are two access specifiers for a class:

Default – The specifier as default for a class is subclasses only for the classes within the same package.

Public – This specifier is accessible anywhere.

Page 7: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

Inner class The inner class are defined within the top-level class. The top-level class have rights to all the members of the

inner class irrespective of the access specifier. The top-level class have rights to the class irrespective of

the class being public, private, protected or default. The inner class can be static or final. The inner class members can be accessed by using the

outer class object.inner class objectname.inner class variable name.

A static inner class need not be instantiated. But an object should be specified.

Page 8: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

The structure of the inner class is: public class outerclass{//Start of outerclass innerclass i //Data member of outerclass public class innerclass {//Start of innerclass

int a=10; }//End of innerclass

public static void main(String args[]) {

outerclass o=new outerclass();o.i.a=10;

} }//End of outerclass

Page 9: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

An example of inner class

public class outerClass

{

// private innerClass x; The private innerClass methods can be accessed in main by using the outer class object

// protected innerClass x; The private innerClass methods can be accessed in main by using the outer class object

// public innerClass x; The public innerClass methods can be accessed in main by using the outer class object

innerClass x;

Page 10: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

public class innerClass

{

//private int a; The private innerClass data members can be accessed in main by using the outer class object

//public int a; The public innerClass data members can be accessed in main by using the outer class object

//protected int a; The protected innerClass data members can be accessed in main by using the outer class object

private int a; //The default innerClass data members can be accessed in main by using the outer class object

innerClass()

{

a=9;

}

public void print()

{

System.out.println("\n\n\t\tA in inner class is: "+a);

}

}

Page 11: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

outerClass() {

x=new innerClass(); //x.a=10; - This is possible as outer class can have access to inner class //data members whether private, public or protected.

} public static void main(String args[]) {

outerClass o=new outerClass();o.x.print();o.x.a=10;o.x.print();//x.a=10 - This is not possible as x is a non-static reference to a

member//x.print(); - This is not possible as x is a non-static reference to a

member

}}

Page 12: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

Embedded Class A class can be embedded in another class. The structural diagram of an embedded class is:

public class topClass

{

public static void main(String args[])

{

}

}

class embeddedClass

{

}

Page 13: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

Embedded classes cannot be public, private or protected. It can only be default as there can be only one class with the access specifiers.

Embedded class is local to the top-level class. If the class in which the embedded class exists is inherited

from, the entire is the parent for the child class. A class should be embedded only if this is applicable to the

top-level class.

Page 14: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

An Embedded class examplepublic class embedClass{ innerClass i; public void print() {

i.print(); } public static void main(String args[]) {

embedClass o=new embedClass();o.print();

}}

Page 15: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

class innerClass //Embedded class cannot be protected, private or public.

//Only the class that contains main can have other access specifiers.

{ static int a=9; innerClass() {

a=11; } public static void print() {

System.out.println("\n\n\t\tA in inner class is: "+a); }

}

Page 16: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

Anonymous class Anonymous class is the object of the class passed in the

function. The structural diagram is:

m.java

public class m

{

}

Page 17: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

m1.javapublic class m1{ public void print( m x)

{}

public static void main(String args[]){ m1 x1=new m1(); x1.print(new m());

}}

Page 18: Introduction to classes Sangeetha Parthasarathy 06/11/2001.

Anonymous class can also be an embedded class in the top-level class.