0% found this document useful (0 votes)
6 views

OOP

OOP special

Uploaded by

Gouranga Mandal
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

OOP

OOP special

Uploaded by

Gouranga Mandal
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Object Oriented Programming

Inheritance
 “is-a” relationship
 Single inheritance:
 Subclass is derived from one existing class
(superclass)
 Multiple inheritance:
 Subclass is derived from more than one superclass
 Not supported by Java
 A class can only extend the definition of one class

Java Programming: Program Design Including Data Structures


Inheritance (continued)

modifier(s) class ClassName extends ExistingClassName


modifier(s)
{
memberList
}
3
Inheritance:
class Circle Derived from
class Shape
public class Circle extends Shape
{
.
.
.
}
Inheritance
 Allow us to specify relationships between types
 Abstraction, generalization, specification
 The “is-a” relationship
 Examples?

 Why is this useful in programming?


 Allows for code reuse
 More intuitive/expressive code
Code Reuse
 General functionality can be written once and
applied to *any* subclass
 Subclasses can specialize by adding members and
methods, or overriding functions
Inheritance: Adding Functionality
 Subclasses have all of the data members and methods
of the superclass
 Subclasses can add to the superclass
 Additional data members
 Additional methods
 Subclasses are more specific and have more
functionality
 Superclasses capture generic functionality common
across many types of objects
public class Person {
String name;
char gender;
Date birthday;

int getAge(Date today) {



}
}

public class Student public class Professor


extends extends Person {
Person {
Vector<Paper> papers;
Vector<Grade> grades;
int getCiteCount() {
double getGPA() { …
… }
} }
}
Access Control
 Access control keywords define which classes can
access classes, methods, and members

Modifier Class Package Subclass World


public Y Y Y Y
protected Y Y Y N
none Y Y N N
private Y N N N
Polymorphism
 “Many forms”
 allow several definitions under a single method name
 Example:
 “move” means something for a person object but means
something else for a car object
 Dynamic binding:
 capability of an implementation to distinguish between the
different forms during run-time
Polymorphism
 Methods having identical names but different
implementations
 Overloading
 Several argument lists for calling the method
 Example: MessageBox.Show method
 Overriding
 Refers to a class that has the same method name as its
base class
 Method in subclass takes precedence
Constructors and
Destructors
 Constructor
Constructors
• A constructor is a special member function whose
task is to initialize the objects of its class.
• It is special because its name is same as the class
name.
• The constructor is invoked whenever an object of
its associated class is created.
• It is called constructor because it constructs the
values of data members of the class.
Constructor - example
class add • When a class contains a
{ constructor, it is guaranteed that
int m, n ; an object created by the class
public : will be initialized automatically.
add (void) ;
• add a ;
------
}; • Not only creates the object a of
add :: add (void) type add but also initializes its
{ data members m and n to zero.
m = 0; n = 0;
}
Constructors
continue …
• There is no need to write any statement to invoke
the constructor function.
• If a ‘normal’ member function is defined for zero
initialization, we would need to invoke this
function for each of the objects separately.
• A constructor that accepts no parameters is called
the default constructor.
• The default constructor for class A is A : : A ( )
Characteristics of Constructors
• They should be declared in the public section.

• They are invoked automatically when the objects


are created.

• They do not have return types, not even void and


they cannot return values.
Parameterized Constructors
continue …

class add • When a constructor is


{ parameterized, we must pass the
int m, n ; initial values as arguments to
public : the constructor function when
add (int, int) ; an object is declared.
------
}; • Two ways Calling:
add : : add (int x, int y) o Explicit
• add sum = add(2,3);
{
m = x; n = y; o Implicit
• add sum(2,3)
}
• Shorthand method
Multiple Constructors in a Class
• C + + permits to use more than one constructors
in a single class.

• Add( ) ; // No arguments

• Add (int, int) ; // Two arguments


Multiple Constructors in a Class
continue …

class add • The first constructor receives no


{ arguments.
int m, n ;
public :
add ( ) {m = 0 ; n = 0 ;} • The second constructor receives
add (int a, int b) two integer arguments.
{m = a ; n = b ;}
add (add & i) • The third constructor receives
{m = i.m ; n = i.n ;} one add object as an argument.
};
Multiple Constructors in a Class
continue …

class add • Add a1;


{ – Would automatically invoke the
int m, n ; first constructor and set both m
public : and n of a1 to zero.
add ( ) {m = 0 ; n = 0 ;} • Add a2(10,20);
add (int a, int b)
– Would call the second
{m = a ; n = b ;} constructor which will initialize
add (add & i) the data members m and n of a2
{m = i.m ; n = i.n ;} to 10 and 20 respectively.
};
Multiple Constructors in a Class
continue …

class complex • complex ( ) { }


{
float x, y ;
public : – This contains the empty body
complex ( ) { } and does not do anything.
complex (float a)
{x=y=a;}
complex (float r, float i) – This is used to create objects
{x=r;y=i} without any initial values.
------
};
Constructors with Default Arguments
continue …

• A::A()  Default constructor


• A : : A (int = 0)  Default argument constructor

• The default argument constructor can be called with


either one argument or no arguments.
• When called with no arguments, it becomes a
default constructor.
Dynamic Initialization of Objects
• Providing initial value to objects at run time.

• Advantage – We can provide various initialization


formats, using overloaded constructors.

This provides the flexibility of using


different format of data at run time
depending upon the situation.
Copy Constructor
•A copy constructor is used to declare and initialize an
object from another object.

integer (integer & i) ;


integer I 2 ( I 1 ) ; or integer I 2 = I 1 ;
The process of initializing through a copy constructor
is known as copy initialization.
Copy Constructor
continue …

The statement
I 2 = I 1;
will not invoke the copy constructor.

If I 1 and I 2 are objects, this statement is legal and


assigns the values of I 1 to I 2, member-by-member.
Copy Constructor
continue …

• A reference variable has been used as an argument


to the copy constructor.

• We cannot pass the argument by value to a copy


constructor.
Thank You

You might also like