Professional Documents
Culture Documents
938e2538-d9d7-41fc-9d2b-5b1a51d092cb-
938e2538-d9d7-41fc-9d2b-5b1a51d092cb-
3
VRINDA A
Assistant Professor,
Dept. of IT, MESCE, Kuttippuram
Inheritance
• Inheritance is one of the features of Object-
Oriented Programming (OOP).
• Method Overloading
• Method Overriding
Method Overloading
• In Java, it is possible to define two or more methods
of same name in a class, provided that there
argument list or parameters are different.
OUTPUT
a: 10
a and b: 10,20
double a: 5.5
O/P : 30.25
Rules for Method Overloading
} // BaseClass reference
but DerivedClass object
public class
DerivedClass extends BaseClass BaseClass obj2 =
{ new DerivedClass();
obj1.methodToOverride();
public void
methodToOverride() obj2.methodToOverride();
{ }
System.out.println ("I'm the }
method of DerivedClass");
}
}
Method Overriding
OUTPUT
OUTPUT
java animals.Cat or
java animals.Elephant
Packages
• If a class wants to use another class in the same package, the
package name does not need to be used.
• Classes in the same package find each other without any special
syntax.
For example • Here, a class named Boss
package payroll; is added to the
public class Boss{ package that already
payroll
public void payEmployee(Employee e)
contains Employee.
{ • The Boss can then
e.mailCheck(); refer
to the Employee class
} without using the payroll
prefix
}
Packages
• What happens if the Employee class is not in the
payroll package?
• The Boss class must then use one of the
following techniques for referring to a class in a different
package.
2. The package can be imported using the import keyword and the
wild card (*)
import payroll.*;
Interfaces
• An interface in java is a blueprint of a class.
• Writing an interface is similar to writing a class.