Sunteți pe pagina 1din 8

10.

3 INHERITANCE

Inheritance is the major mechanism in object –oriented languages that


allows the sharing of data and operations among classes .A class B can inherit some or
all of the instance variables and methods of another class A by declaring that class in its
definiton.

Public class B extends A


{
……..
}

In java,B inherits all the instance variables and methods of A and B is


called subclass of A and super class of B.A deque as an extention of a queue that adds
the operations addFont and removeRear.A deque can inherit from a queue in java. A
deque is a extention in class queue,and all the of the operations that apply to a queue
can also apply to the deque.

Queue is a super type of deque , and all the objects of a subclass such as
deque obey the subtype principle:

Queue q;
Deque d;

Now q.deque() and d.deque() perform exactly the same operation.the


difference is that d.deleteRear() is a legal operation.the difference is that d.deleteRear()
is a legal operation.
Assinging q to d above is inherently suspicious, even when q does contain a
deque object

D=q; //a compile-time error in java

D=(Deque)q: //now no compiler error


Public class Complex
{
//..previous code as before
//must keep Object class definition for overiding;
{
Complex C=(complex) obj;
return re==c.realpart()
&&im==c.imaginarypart();
}
Public String toString()
{
Return re + “ + “ + im + “ I “;
}
}
Now given the following code:
Complex z=new complex(1,1);
Complex x=new complex(1,1);
If(x.equals(z))
System.out.println(“ok!”);
System.out.println(z);

The java interpreter will print


Ok!
+ 1.0i

Inheritance establishes a parent child dependency relationship between


superclasses,so that the class inheritance relationship can be viewed as an inheritance
graph.
CLOSEDFIGURE
/ \
RECTANGLE CIRCLE

Inheritance provides many oppurtunities for plymorphism,since any method


that applies to a base class can apply to a subclass.this notion of polymorphism is in
fact subtype polymorphism.language one might choose to define both integers and
reals as subclasses of a class number
NUMBER
/ \
INTEGER REAL 1
In each situation of the cases we have examined, the inheritance graph is a tree.
This situation is called single inheritance.it is also possible to have multiple inheritance,
in which a class may be inherit from two or more superclasses.

LinkableObject or complex can appear.in particular,if x and y are two objects of


class LinkableComplex,then it is possible both to link them and add them:
x.add(y);
x.linkTo(y);

Java has a mechanism for defining such constraints called an interface,which is


like a class except yhat is only specifies the names and types of methods, but gives no
implementation,no instance variables or constructors. We could therefore define a
LinakbleObject as an interface:

Public interface LinkableObject


{
LinkableOject next();
Void linkTo(LinkableObject p);
}
10.4 DYNAMIC BINDING

Depending on the language,this dynamic allocation of objects


may be under the manaul control of the programmer(as in c++ new and delete
operations),or it may be fully automatic(as in most functional languages),or it may be a
hybrid of the two.dyamic binding is one of the main sources of the power of an object-
orented language.
The area method was in fact undefined for the class
ClosedFigure,so it was necessary for appropriate methods of derived classes to be
invoked when a method is redefined in a derived class.
It is possible to offer both static and dynamic binding of
methods in an object oriented language.In languages that represent more of a
compromise between the object-oriented paradigm and standard imperative
languages.In most "pure" object-oriented languages , such as java and smalltalk, all
methods are implicity "virtual" that is,dynamic binding always applies;
ex:
class A
{
void p()
{
System.out.println("A.p");
}
void q()
{
System.out.println("A.q");
}
void f()
{
p();
q();
}
}
class B extends class A
{
void p()
{
System.out.println("B.p");
}
}
void q()
{
System.out.println("B.q");
super.q();
}
}
public class VirtualExample
{
public static void main(String[ ] args}
{
A a=new a();
a.f();
a=new B();
a.f();
}
}

10.5 C++

C++ was originally developed by Bjarne Stroustrup at


AT&T Bell Labs(where C was developed) as an extention of C with Simula-like classes
but in the late 1980's and 1990's it grew well beyond its origins into a language
containing large number of features.
c++ contains class and object declarations similar to those
of Java.Inatance variables and methods are both called members of a class:Instance
variables are reffered to as data members, and methods are reffered to as member
functions.Subclasses are referred to as derived classes and superclasses are called
base classes.
Similar to java,three levels of protection are provided for
class membersin c++:public,private and protected.Public members are those accessible
to client code,as well as to derived classes.protected members are in accessible to
client code but are still accessible to derived classes.Private members are accessible
neither to clients nor to derived classes.
Additionally, the keywords private,public and protected establish blocks in
class declarations, rather than apply only to indiovidual member declarations as in java:
class A
{
// A C++ class
public:
// all public members here
protected:
// all peotected members here
private:
// all private members here
};
In c++,intialisation of objects is have given as in java by constructors that have
the same name as that of the class.constructors are automatically called when an object
is allocated space in memory, and actual parameters to the constructor function are
specified at that time.
The scope resolution operator indicated by a double colon “: :”after the class
name. It gets its name from the fact that it causes the scope of the class declartion to be
reentered.Member functions that aar given implementations in a class declaration are
autimatically assumed to be inline(that is,a compiler may replace the function call by the
actual code for the function.

Class LinkableObject
{
Public:
LinkableObject()
: link(0) { }
LinkableObject (LinkableObject* p)
: link(p) { }
LinkableObject* next()
{
return link;
}
Void linkTo(LinkableObject* p)
{
Link=p;
}
Private:
LinkableObject* link;
};

A c++ definiton of a LinakbleObject class(compare to the java code)

The reason for this notation in c++ is efficiency:c++ insists that all inatance
variables be intialised prior to the execution of the body of any
Constructor; thus, intializat Ion would be performed twice if it were expressed as code
regular inside the body of the constructor(once by the system and once by the actual
code)

Class A
{
Public:
Void p()
{
Cout<<”A: :p\n”;
}
Virtual void q()
{
Cout<<”A::q\n”;
}
Void f()
{
P();
Q();
}
};
Class B:public A
{
Public:
Void p()
{
Cout<<”b::p\n”;
}
Void q()
{
Cout<<”B::q\n”;
}
};
Int main()
{
A a;
B b;
a.f();
b.f();
a=b;
a.f();
}
A c++ program illustrating dynamic and static binding.

The some what suprising output from this progam is


A::p
A::q
A::p
B::q
A::p
A::q

The inherited function A.f is called Inside this function in A are calls to p and q.since p is
not virtual,dynamic binding does not apply ,and this call is resolved at compile time to
A::p(). On the other hand,the call to q is virtual,and the implicit object parameter is used
to dynamically resolve the call during execution.Thus, the call to q is implicitly a call this
s->q(),and in the call b.f(),this is the address of b.so B::q appears on the fourth line of
the above output. On the other hand, the assingment a=b simply copied the data of b to
a and does not change the address of a or the pointer in the call to a.f().Thus,A::p and
A::q are again called.

Int main()
{
A* a=new A;
B* b=new B;
a->f();
b->f();
a=b;
a->f();
return 0;

output:

A::p
A::q
A::p
B::q
A::p
B::q

C++ offers multiple inheritance using a comma-seperated list of base classes,as in


class C : public A,private B {……..};

Multiple inheritance in c++ ordinarily creates separate copies of each class ona
inheritance path.
Ex:
Class A{………};
Class B:public A{…..};
Class C: public A{….};
Class D:public B,public C{….};

This is repeated inheritance.To get a simple copy of an A in class D,one must declare
the inheritnce using the virtual keyword:

Class A{…..};
Class B:virtual public A{…..};
Class C:virtual public A{…..};
Class D:public C{….};

S-ar putea să vă placă și