Professional Documents
Culture Documents
UNIT-2: Inheritance, Interface and Package
UNIT-2: Inheritance, Interface and Package
UNIT-2: Inheritance, Interface and Package
• Public
• Private
• Protected ( protected members can be accessed only by sub
classes).
• NOTE: Variables, methods, and constructors, which are
declared protected in a superclass can be accessed only by the
subclasses in other package or any class within the package of
the protected members' class.
class
• Method overloading:
If a class has multiple methods having
same name but different in parameters,
it is known as Method Overloading.
• Method Overriding:
If subclass (child class) has
the same method as declared in the
super class(parent class), it is known
as method overriding.
Faculty: Mrs.M.Lalitha, Assistant Professor, CSE, GNITS
POLYMORPHISM 12
of arguments or both.
class sup {
void show() { System.out.println("super class method"); }
}
class sub1 extends sup {
void show() { System.out.println(" sub class -1 method"); }
13
}
class sub2 extends sup {
void show() { System.out.println(" sub class -2 method"); }
}
class demo {
public static void main(String[] s) {
sup superob = new sup();
sub1 subob1 = new sub1();
sub2 subob2 = new sub2();
sup supref; What’s the output?
supref = superob;
supref.show();
supref = subob1;
supref.show();
supref = subob2;
supref.show();
Faculty: Mrs.M.Lalitha, Assistant Professor, CSE, GNITS
}
}
FINAL keyword 14
• equals():
• it compares 2 objects.
• returns true if objects are equivalent.
• it checks if the invoking reference refers to the same object as the one
passed as an argument.
• this method is overridden to determine if 2 objects are equal in their
contents.
• toString():
• returns a string that contains a description of the object on which it is
called.
• this method is automatically called when an object is output using println().
• notice the unusual syntax in the return type for getClass().this relates to
java's generic features. generics allow the type of data used by a class or
method to be specified as a parameters.
• valueOf():
• depend on the passed parameters. This method returns the string
representation of the passed argument.
• Since an abstract class does not define a complete implementation, there can
be no objects of an abstract class (it will give compilation error, if you create an
object for abstract class).
• When a subclass inherits an abstract class, it must implement all of the abstract
methods in the superclass. If it doesn't, then the subclass must also be specified
as abstract. Thus, the abstract attribute is inherited until such time as a
complete implementation is achieved.
• NOTE: it is illegal to declare a class as both abstract and final since an abstract
class is incomplete by itself and relies on its subclasses to provide complete
implementation.
• Interface defines what a class must do but not how it will do it.
• In java, an interface defines set of methods that will be
implemented by a class.
• interfaces are similar to abstract classes, except that no method
can include a body.