Session: 4 Java Inheritance
Java
Inheritence
Q?
How inheritance occurs
/> It
occurs when the class is
derived from the another super-class.
/>
The derived class contains
all the members of the class it inherits from +
any member it defines
explicitly.
/>
The derived class can override
the definition of existing methods
by providing its own implementation.
/> we
use the keyword extends to do inheritance:
Class
Subclass extends SuperClass{
…........
}
Q?
why inheritance
/>
the new class we want to make is merely different from the already
existing class. So doing inheritance helps us to reduce
the repetition of code
/> it
also helps us to do localization of
code: i.e. fixing a bug in
superclass fixes it to all the subclass that inherites
the superclass. Whereas adding
functionality also adds the function and there is less chance
of repetitions of same operations.
Q?
what is the visibility(scope) for the methods and attributes present
in Superclass
/>
All attributes and methods marked as
/>
public : are always
accessible by SubClass
/>
private : are accessible
within the SuperClass only
/>
protected : are accessible
within the class and its subclasses
Q?
how many kinds of reference are present
/>
“this” : is a reference
to the current object
/>
“super” is a reference
to the parent class
Q?
How constructor is managed in child class in case of inheritance
/>
Case 1: Java compiler automatically inserts a call to default
constructor (with no
parameters) of parent class.
/>
the execution of constructors proceeds in
top-down in inheritance hierarchy. So in such a way when a
method of child class is executed the super-class
is completely initialized already
/>
Case 2: In case of custom
constructors ( constructors
withparameters )are defined then we cannot define default
constructor explicitly. So the child class constructor must call the
right constructor of the parent class, explicitly.
/> we
use super() to identify
constructors of parent class. And inside the super
( parameters present in superclass must be provided );
Q?
what are the features of Class Object
/>
all classes are the subtypes of Object
Class
/>
class Object has some methods, which are often useful for all classes
so they are overridden
/>
some comman methods used are :
/>
toString( ) : String ,
return string identified to object
/>
equals(Object) : boolean ,
test equality of value of objects
Note:
System.out.print(object) // is same as ….
System.out.print(object.toString());
Q?
How Casting is done in inheritance
/>
Casting is process of Type
conversion
/> We
have Upcast In which we
asssign more specific type (subtype)
to a more general type (supertype)
/>
each class is a subclass of Object so we can always upcast any
instance to Object type.
/>
the good thing is upcast are always
type safe
/>
cast on the reference type only
affects the reference while in contrary a primitive
type cast involves a value
conversion.
/> In
Downcast we assign more
general type (super-type) to
a more specific type (sub-type)
/>
downcast are not safe by default
/> we
throw ClassCastException :
if a class cannot be cast to another class
Q?
how can we down cast safely
/> we
use the instanceof operator
to do so.
/>
aReference instanceof aClass
: return true if the object
referred to by lhs reference can be cast to the rhs class
Q?
what are abstract class
/>
this are the classes which are used to define common
behaviour for many child classes:
/>
this class are not instantiated so variables are not instantiated and
method has no body
/>
this methods are all implemented in
the child classes
public
abstract class School {
private
int class;
public
abstract void draw();
}
Q?what
is java interface
/> it
is the special type of class where :
/>
attributes are implicitly
static and final
/>
methods are implicityly
abstract (no body)
/> we
cannot instantiate this class (no
new objects)*
/>
and it is just used to define the reference
Q?
what are the rules of interface
/>
interface can extend another
interface but cannot extend
a class
/>
interface can extend multiple
interfaces
Q?
what are the rules of using class
/>
class can extend only
one class
/> a
class can implement multiple
interfaces
Note* :
Strongly suggested to use the interface instead of using abstract
methods in a class
Q?
how do we create a new interface
/>
using keyword interface
public
interface Comparable{
…..................//abstract
methods inside
}
Q?
how do we implement the interface in the class
/>
using keyword implements
public
class College implements
Comparable{
….............//overridde
this abastarct methods
}
Comments
Post a Comment