OBJECT ORIENTED
PROGRAMMING - JAVA
YSS#
Ref 1: Deitel & Deitel (2010). Java How to Program (8th
ed.). Prentice Hall
Ref 2: Lindholm, T. & Yellin, F. (2007). The Java Virtual
Machine Specification (2nd ed.). Addison-Wesley
Ref 3: Gosling, J., Bill, J., Steele, G. & Bracha, G. (2005).
The Java Language Specification (3rd
ed.). Addison-Wesley.
OOP ABSTRACTION
Abstraction - Objects only reveal internal mechanisms that are relevant for the use of other objects,
hiding any unnecessary implementation code. This concept helps developers make changes and
additions over time more easily.
Data Abstraction is the property by virtue of which Data Abstraction may also be defined as the process
only the essential details are displayed to the user. of identifying only the required characteristics of an
The trivial or the non-essentials units are not object ignoring the irrelevant details.The properties
displayed to the user. Ex: A car is viewed as a car and behaviors of an object differentiate it from other
rather than its individual components (engine, objects of similar type and also help in
wheels, gear box). classifying/grouping the objects.
Need for Abstract Classes
Subclasses may not need to inherit a method implementation if the method is specialized.
public class Television extends ElectronicDevice {
public void turnOn() {
changeChannel(1);
initializeScreen();
}
public void turnOff() {}
Television or
MobilePhone class
public void changeChannel(int channel) {}
public void initializeScreen() {}
ElectronicDevice dev = new Television();
dev.turnOn();// Works for TV since changeChannel(1) does apply
ElectronicDevice dev = new MobilePhone();
dev.turnOn();// Does not work for Mobile Phone since changeChannel(1) does not apply
Defining Abstract Classes
A class can be declared as abstract by using the abstract class-level modifier.
public abstract class ElectronicDevice { }
An abstract class can be subclassed.
public class Television extends ElectronicDevice { }
An abstract class cannot be instantiated.
ElectronicDevice dev = new ElectronicDevice(); // error
Defining Abstract Methods
A method can be declared as abstract by using the abstract method-level modifier.
public abstract class ElectronicDevice {
public abstract void turnOn();
public abstract void turnOff();
An abstract method:
- Cannot have a method body
- Must be declared in an abstract class
- Is overridden in subclasses // Override means replace method’s existing code in sub-class
Abstraction – Shape Example
Calculating area for shapes differ with the type of shape. This calls for methods whose implementation is provided by the sub-class instead of
the base class. This means the method shall be defined in the base class but the actual method body/Code is done at the sub-class.
Example ~~
abstract class Shape class Circle extends Shape class Rectangle extends Shape{
{ {
String color; double radius; double length;
// these are abstract methods public Circle(String color,double radius) { double width;
abstract double area(); // calling Shape constructor public Rectangle(String color,double length,double width) {
public abstract String toString(); super(color); // calling Shape constructor
System.out.println("Circle constructor called"); super(color);
// abstract class can have constructor this.radius = radius; System.out.println("Rectangle constructor called");
public Shape(String color) { } this.length = length;
System.out.println("Shape constructor called"); @Override this.width = width;
this.color = color; double area() { }
} return Math.PI * Math.pow(radius, 2); @Override
// this is a concrete method } double area() {
public String getColor() { @Override return length*width;
return color; public String toString() { }
} return "Circle color is " + super.color + @Override
} "and area is : " + area(); public String toString() {
} return "Rectangle color is " + super.color +
} "and area is : " + area();
}
public class Test }
{
public static void main(String[] args)
{ Shape constructor called
Run Circle constructor called
Shape s1 = new Circle("Red", 2.2);
Shape s2 = new Rectangle("Yellow", 2, 4);
Shape constructor called Output
Rectangle constructor called
Circle color is Redand area is : 15.205308443374602
System.out.println(s1.toString()); Rectangle color is Yellowand area is : 8.0
System.out.println(s2.toString());
}
}
Final Methods
A method can be declared final. Final methods may not be overridden.
Methods are marked as final to prevent method overriding and thus the
public class MethodParentClass {
parent class implementation suffices for the derived classes
public final void printMessage() {
System.out.println("This is a final method");
public class MethodChildClass extends MethodParentClass {
@override
public void printMessage() { //// compile-time error
System.out.println("Cannot override method");
}
Final Classes
A class can be declared final. Final classes can not be extended.
public final class FinalParentClass { }
// compile-time error
public class ChildClass extends FinalParentClass { }
// To prevent inheritance of a class, mark it as final
Final Variables
The final modifier can be applied to variables.
Final variables may not change their values after they are initialized.
Final variables can be:
•Class fields
— Final fields with compile-time constant expressions are constant variables.
— Static can be combined with final to create an always-available, never-changing
variable.
•Method parameters
•Local variables
Declaring Final Variables
public class VariableExampleClass {
private final int field;
public static final int JAVA_CONSTANT = 10;
public VariableExampleClass() {
field = 100;
}
public void changeValues(final int param) {
param = 1; // compile-time error
final int localVar;
localVar = 42;
localVar = 43; // compile-time error
}
}