OOP Principles (Polymorphism, Abstraction)
OOP Principles (Polymorphism, Abstraction)
OOP Principles (Polymorphism, Abstraction)
Polymorphism
What Do you think about this Concept
✓ Introduction
✓ superclass reference to subclass-type variable
✓ subclass method calls via superclass type variable
✓ Multiple inheritance and interfaces
Introduction
✓ Polymorphism allows programmers to send the same message to
objects from different classes.
✓ Polymorphism enables us to “program in the general” rather than
“program in the specific”.
✓ Polymorphism enables you to write programs that process objects that
share the same superclass (either directly or indirectly) as if they are all
objects of the super class.
✓ It is possible to design and implement systems that are easily
extensible. i.e., new class can be added with little or no modification to
the general portion of the program.
R/Ship among Objects in Inheritance
✓ An object of subclass can be treated as object of its superclass.
✓ A Program can create an array of superclass reference that refers to
objects of many subclass types.
✓ This is allowed because a subclass is a type of its superclass.
✓ However, we can’t treat a superclass object as a subclass object
because a superclass object is not an object of any of its subclasses.
The “is-a” relationship applies only from a subclass to its direct and
indirect superclass.
{ y=yValue; }
x=xValue; return y; }
}//end class
public class Circle4 extends Point3 {
public class PolyTest{
private double radius;
public static void main(String args[])
public Circle4(int xValue, int yValue, double rValue) {
{
super(xValue,yValue);
Point3 point = new Point3(30,50);
setRadius(rValue); }
Circle4 circle = new Circle4(120,89,2.7);
public void setRadius(double rValue) {
String output = “Call Point3’s toString with superclass”+
radius = (rValue < 0.0?0.0:rValue); }
“reference to superclass object:\n”+ point.toString();
public double getRadius() {
output+= “\n\nCall Circle4’s toString with subclass
return radius; }
”+ “reference to subclass object:\n”+circle.toString();
public double getDiameter() {
Point3 pointRef = circle;
return 2*getRadius(); }
Output+= “\n\ncall Circle4’s toString with superclass ”+
public double getCircumfrence() {
“ refernce to subclass object:\n “+pointRef.toString();
return Math.PI*getDiameter(); }
return Math.PI*getRadius()*getRadius(); }
}//end class
✓ In the above program, the Statement Point3 pointRef = circle; assigns
the reference of subclass object circle to superclass-type variable
PointRef.
✓ A superclass-type variable that contains a reference to a subclass
object calls the subclass method.
✓ Hence, pointRef.toString() actually calls class Circle4’s toString method
❖ Assigning a superclass object’s reference to a subclass-type
variable
public class PolyTest2{
} } // end class
} } // end class
point.setX(10);
point.setY(20);
✓ When we try to invoke subclass only
point.toString();
methods using point, the java
compiler generates errors on each //attempt to invoke subclass-only (Circle4) method on subclass object through
method. superclass (Point3) reference
public abstract void eat (); no curly braces – just end the declaration
with semi colon
✓ Here, the abstract Animal class informs the derived types that “I have
a method name eat () that takes no argument and returns nothing. If
you derive from me, you figure out the details”.
✓ Implementing an abstract method is just like overriding a method.
The first class
✓ An abstract class means the class must be extended; an abstract method
means the method must be implemented (provide a body).
✓ When a class extends from Abstract class can’t give any implementation for
the abstract method it should marked as abstract again.
✓ Constructors and static methods can’t be declared abstract.
N.B. Although we can not instantiate objects of abstract super classes, we
can still use that abstract class as a declared reference type, for the
purpose of polymorphism.
Exercise
- Abstract Superclass Employee declares the “interface” to the hierarchy –
set of methods that a program can invoke on all Employee objects.
- Try to create a Polymorphic program by using this information
Multiple Inheritance and Interfaces
✓ An Interface is like a class that can’t be instantiated.
✓ It specifies a set of requirements for a class to implement.
✓ An interface is a description of a capability. It lists the methods that a
class must implement to carry out that capability.
✓ All interface members must be public, and interfaces may not specify
any implementation details, i.e., no instance variables and no default
methods implementations.
✓ A compile-time error occurs if the identifier naming an interface
appears as the name of any other class or interface in the same
package.
✓ All methods declared in an interface are implicitly public abstract
methods and all fields are implicitly public, static and final.
✓ A class that implements an interface must provide an implementation for
all the method signatures in the interface.
✓ An interface specifies what operations are allowed but not how they’re
performed.
✓ A java interface can provide 100% abstraction.
E.g.
public interface Shape {
public double getArea();
public double getVolume();
public String getName();
}
public class Point extends Object implements Shape {
private int x;
private int y;
public Point (int xValue, int yValue)
{
x = xValue;
y = yValue;
}
….
Getter and Setter Methods
public double getArea()
{
return 0.0; }
public double getVolume() {
return 0.0; }
public String getName() {
return “Point”; }
}
✓ When a class implements an interface, the same “is-a” relationship
provided by inheritance applies. For example, class Point implements
Shape. Therefore, a Point object is a Shape. In fact, objects of any class
that extends Point are also Shape objects.
✓ Objects of any subclasses of the class that implements the interface can
also be thought of as objects of the interface type.
✓ Thus, as we can assign the reference of a subclass object to superclass
variable, we can assign the reference of a sub class object to an
interface variable. (Polymorphism)
N.B. By using Interface, unrelated classes can implement a set of common
methods.
You treat an object by the role it plays, rather than by the class type
from which it was instantiated.
Payable Interface
- Contains one method double getPaymentAmount();
It doesn’t contain implementation
Invoice
}
Summary
✓ To create a package, you choose a name for the package and put a
package statement with that name at the top of every source file that
contains the types (classes, interfaces, enumerations, and annotation types)
that you want to include in the package.
//save by A.java //save by B.java
package pack; package mypack;
public class A { import pack.*;
public void msg() {
class B{
System.out.println("Hello");}
} public static void main(String args[]){
A obj = new A();
obj.msg();
}}
✓ Similar with Java Api package we can access our own package.
There are three ways to access the package from outside the package
❖import package.*;
❖import package.classname;
❖fully qualified name;
1) Using packagename. *
✓ If you use package. * then all the classes and interfaces of this package will
be accessible but not sub packages.
✓ The import keyword is used to make the classes and interface of another
package accessible to the current package.
//save by A.java //save by B.java
package pack; package mypack;
public class A{ import pack. *;
public void msg(){ public class B{
System.out.println("Hello");} } public static void main(String args[]){
A obj = new A();
obj.msg(); }}
}
}
2) Using packagename.classname
✓ If you import package.classname then only declared class of this package
will be accessible.
✓ If you import a package, all the classes and interface of that package will
be imported excluding the classes and interfaces of the sub packages.
Sub packages: Package inside the package is called the sub package. It
should be created to categorize the package further.
Naming Conventions: Package names are written in all lower case to avoid
conflict with the names of class or interface.