Professional Documents
Culture Documents
Programming CE-
103
Object:
What happens when a method is invoked using the base class reference?
The object responds in accordance with its true type.
What if the user pointed to a different form of object using the same
reference? The user would observe different behavior.
This is polymorphism.
Method Overriding
When a method of a sub-class has the same name and type as a method of
the super-class, we say that this method is overridden.
A(int a, int b) {
i = a; j = b;
}
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
Example:
class B extends A
{ int k;
void show() {
System.out.println("k: " + k);
}
}
Example:
When show() is invoked on an object of type B, the version of show()
defined in B is used:
class Override {
public static void main(String args[])
{ B subOb = new B(1, 2, 3);
subOb.show();
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
super.show();
System.out.println("k: " + k);
}
}
The super-class version of show() is called within the sub-class’s version.
Overriding versus Overloading 1
Method overriding occurs only when the names and types of the two
methods (super-class and sub-class methods) are identical.
class A {
int i, j;
A(int a, int b) {
i = a; j = b;
}
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
Overriding versus Overloading 2
The show() method in B takes a String parameter, while the show()
method in A takes no parameters:
class B extends A
{ int k;
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show("This is k: ");
subOb.show();
}
}
Dynamic Method Invocation
Overriding is a lot more than the namespace convention.
class A {
void callme() {
System.out.println("Inside A's callme method");
}
}
Example:
Two sub-classes B and C:
class B extends A
{ void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A
{ void callme() {
System.out.println("Inside C's callme method");
}
}
class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a; dim2 = b;
}
double area() {
System.out.println("Area is undefined.");
return 0;
}
}
Example:
Rectangle is a sub-class of Figure:
Rectangle(double a, double b) {
super(a, b);
}
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
Example:
Triangle is a sub-class of Figure:
Triangle(double a, double b)
{ super(a, b);
}
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
Example:
Invoked through the Figure variable and overridden in their respective sub-
classes, the area() method returns the area of the invoking object:
class FindAreas {
public static void main(String args[])
{ Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref;
figref = r; System.out.println(figref.area());
figref = t; System.out.println(figref.area());
figref = f; System.out.println(figref.area());
}
}
Encapsulation:
Binding the data with the code that manipulates it.
It keeps the data and the code safe from external interference
The get method returns the variable value, and the set method
sets the value.
Example 1:
public class Person {
private String name; // private = restricted access
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName)
{ this.name = newName;
}
}
public class Main {
public static void main(String[] args) {
Person myObj = new Person();
myObj.name = "John"; // error
System.out.println(myObj.name); //
error
}
}
Example 2:
public class EncapTest public void setAge( int newAge)
{ private String { age = newAge;
name; private String }
idNum; private int
age; public void setName(String newName)
{ name = newName;
public int getAge() }
{ return age;
} public void setIdNum( String newId)
{ idNum = newId;
public String getName() }
{ return name; }
}
}
}
Why Encapsulation ?
• Class attributes can be made read-only (if you only use the get
method), or write-only (if you only use the set method)
• Flexible: the programmer can change one part of the code without
affecting other parts
The user will have no idea about the inner implementation of the
class. It will not be visible to the user that how the class is storing
values in the variables. He only knows that we are passing the
values to a setter method and variables are getting initialized with
that value.
Increased Flexibility: