You are on page 1of 36

CLASSES , OBJECTS, METHODS

• A class is a group of objects that has common properties.


• Class is a template or blueprint from which objects are
created. So an object is an instance(result) of a class.
class Box{
double width;
called data members or
double height;
instance variables
double depth;
Called method ,similar to
double volume(){ function
double v=width*height*depth;
return v;
}
Declaring an Objects
• Creating a Class means creating a new data
type.
• Object creation is a two step process:
1. Declaring a variable of class type. This variable
does not define an object. Instead, it simply a
variable that can refer to an object.
Eg: Box mybox;//declare a reference to an
//object of type Box
2. Acquire an actual, physical copy of object and
assign to that variable. This is done by using the
new operator
Eg: mybox=new Box();
• The new operator dynamically(during run time)
allocates memory for an object and returns a
reference to it. Reference means address in
memory of the object allocated by new. This
reference is stored in the variable.

• Combining two steps:

Box mybox = new Box();


class Box{
double width;
double height;
double depth;

double volume(){
double v=width*height*depth;
return v;
}
void setDim ( double w , double h , double d){
width=w;
height=h;
depth=d;
}
}
Class BoxDemo {
public static void main(String args []){
Box mybox1=new Box();
Box mybox2=new Box();
double vol;
mybox1.SetDim(10,20,15);
mybox2.SetDim(3,6,9);
vol=mybox1.volume();
System.out.println (“Volume of Box_1:”+vol);
vol=mybox2.volume();
System.out.println (“Volume of Box_2 :”+vol);
}
}
Constructors
• A constructor initializes an object immediately
upon creation.
• It has the same name as the class
• Constructor is automatically called immediately
after the object is created, before the new operator
completes.
• Constructors have no return type , not even void.
class Box{
double width;
double height;
double depth;
Box()
{
width=10;
height=10;
depth=10;
}
double volume(){
double v=width*height*depth;
return v;
}
}
Class BoxDemo {
public static void main(String args []){
Box mybox1=new Box();
Box mybox2=new Box();
double vol;
vol=mybox1.volume();
System.out.println (“Volume of Box_1:”+vol);
vol=mybox2.volume();
System.out.println (“Volume of Box_2 :”+vol);
}
}
Parameterized Constructors
• In the previous example, all boxes have the same
dimension.
• What is needed is a way to construct a Box
objects of various dimensions.
• Solution: Add parameters to the constructors.
class Box{
double width;
double height;
double depth;
Box( double w , double h , double d)
{
width=w;
height=h;
depth=d;
}
double volume(){
double v=width*height*depth;
return v;
}
}
Class BoxDemo {
public static void main(String args []){
Box mybox1=new Box(10,20,30);
Box mybox2=new Box(20,12,10);
double vol;
vol=mybox1.volume();
System.out.println (“Volume of Box_1:”+vol);
vol=mybox2.volume();
System.out.println (“Volume of Box_2 :”+vol);
}
}
Method overloading
• It is possible to define two or more methods
within the same class that share the same
name, as long as their parameter
declarations are different.
• Such methods are said to be overloaded
and the process is called method
overloading.
• Method overloading is one of the ways that
java implements polymorphism.
class OverloadDemo {
void test()
{
System.out.println(“No Parameters”);
}
void test( int a )
{
System.out.println(“a :”+ a);
}

void test( int a , int b)


{
System.out.println(“a and b:”+ a + “ ”+ b);
}
Double test(double a)
{
System.out.println(“ a:”+a);
return a*a;
}
class Overload{
public static void main(String args []){
OverloadDemo ob= new overloadDemo ();
double result;
ob.test ();
ob.test (10);
ob.test(10,20);
result=ob.test(2.5);
System.out.println(“Square of 2.5:”+result)
}
}
Constructor overloading
class Box{
double width;
double height;
double depth;
Box( double w , double h , double d)
{
width=w;
height=h;
depth=d;
}
Box( )
{
width=0;
height=0;
depth=0;
}
Box(double a)
{
width=height=depth=a;
}
double volume(){
double v=width*height*depth;
return v;
}
}
Class BoxDemo {
public static void main(String args []){
Box mybox1=new Box(10,20,30);
Box mybox2=new Box();
Box mybox3=new Box(4);
double vol;
vol=mybox1.volume();
System.out.println (“Volume of Box_1:”+vol);
vol=mybox2.volume();
System.out.println (“Volume of Box_2 :”+vol);
vol=mybox3.volume();
System.out.println (“Volume of Box_3 :”+vol);

}
}
Access Control
• Encapsulation provides an important attribute:
access control.
• Through encapsulation, we can control what parts
of a program can access the members of a class.
• How a member can be accessed is determined by
the access specifier.
• Java’s access specifiers are public, private
,protected.
• Protected is applied only when inheritance is
involved.
• Default access specifier is public.
• When a member of a class is specified as public,
then the member can be accessed by any other
code.
• When a member of a class is specified as private,
then that member can be accessed by other
members of its class.
• Example:
Class test{
int a;
public int b;
private int c;
void set_c (int i)
{
c = i;
}
int getc()
{
return c;
}
class Access{
public static void main(String args[])
{
test ob=new test();
ob.a=10;//this is ok,no error
ob.b=20;// this is ok,no error
//ob.c=100, Error! .accessing private
ob.set_c(100);
int k=ob.getc();
System.out.println(k);
}
this keyword
• In java, this is a reference variable that refers to
the current object.
• Eg:

Box( double w , double h , double d)


{
this.width=w;
this.height=h;
this.depth=d;
}
this can be used to avoid the problem of instance
variable hiding. When a local variable has the same
name as an instance variable, the local variable
hides the instance variable.
For Eg:,
Consider a class Box with three instance
variables : width, height , depth.
Box( double width , double h, double d){
this.width=width;
height=h; formal parameters
depth=d;
}
instance variables
INHERITANCE

• It can be defined as the process where one class


acquires the properties( variables and methods) of
another.
• It allows the creation of hierarchical classification.
• The class which inherits the properties of other is
called subclass(derived class or child class) and the
class whose properties are inherited is known as
superclass(base class, parent class).
• Code Reusability is one of the main uses of
inheritance.
• extends is the keyword used to inherit the
properties of a class.

• Consider a program, that creates a superclass A


and subclass B.

• Example:
class A{
int i,j;
void show( )
{
System.out.println (“ i and j: ”+ i “ ” + j);
}
}
class B extends A{
int k;
void sum()
{
System.out.println (“ i +j+ k: “+( i+ j+ k));
}
}
class simple{
Public static void main(String args [])
{
A ob_a = new A( );
B ob_b = new B( );
ob_a.i=10;
ob_a.j=12;
System.out.println(“Contents base class’s object:”)
ob_a.show( );
//subclass has access to all public members of its //superclass
ob_b.i=7;
ob_b.j=8;
System.out.println(“Contents of child class’s object:”)
ob_b.show();
ob_b.k=5;
ob_b.sum();
}
}
Output:
Contents base class’s object:
i and j: 10 12
Contents of child class’s object:
i and j: 7 8
i+ j+ k = 25
-----------------------------------------------------------------
Types of inheritance
On the basis of class, there can be three types of
inheritance in java: single, multilevel and
hierarchical.
In java programming, multiple and hybrid inheritance
is supported through interface only.
Types
Multiple Inheritance
• Multiple inheritance(a class inherits multiple classes,
ie, more than one parent classes ) is not supported in
java through class.

• To reduce the complexity and to simplify the language,


multiple inheritance is not supported in java.
• Consider a scenario where A, B and C are three
classes. The C class inherits A and B classes. If A
and B classes have same method and you call it
from child class object, there will be ambiguity to
call method of A or B class.
METHOD OVERRIDING

• If subclass (child class) has the same method as


declared in the parent class, it is known
as method overriding in java. ,ie, the method in
the subclass is said to override the method in the
super class.
• Method overriding is used to provide specific
implementation of a method that is already
provided by its super class.
• Method overriding is used for runtime
polymorphism.
class Vehicle{  
  void run(){
System.out.println("Vehicle is running");}  
}  
class Bike extends Vehicle{  
  void run(){
System.out.println(“Bike is running");}  
}  
  }  
Class method {
public static void main(String args []){  
Bike obj = new Bike();  
obj.run();   Output
}  
Bike is running
}
Rules for Java Method Overriding

• method must have same name as in the parent


class
• method must have same parameter as in the
parent class.
• must be IS-A relationship (inheritance).
Diff. between method overloading and method
overriding

You might also like