OOP Injava
OOP Injava
Prepared by
Dr. Ashesh Mahidadia
Java is Platform Independent
JAVA
Platform-
Distributed
Independent
Memory
Secure
Management
Java Byte
Java Code Code
Java compiler
(.java)
(.class)
In procedural programming,
• groups of actions that perform some task are formed into functions and functions are grouped to
form programs.
In OOP,
• programmers concentrate on creating their own user-defined types called classes.
• each class contains data as well as the set of methods (procedures) that manipulate the data.
• an instance of a user-defined type (i.e. a class) is called an object.
• OOP encapsulates data (attributes) and methods (behaviours) into objects, the data and methods
of an object are intimately tied together.
• Objects have the property of information hiding.
COMP2511: OOP in Java 3
Inheritance in Object Oriented Programming (OOP)
v Inheritance is a form of software reusability in which new classes are created from the
existing classes by absorbing their attributes and behaviours.
v Instead of defining completely (separate) new class, the programmer can designate
that the new class is to inherit attributes and behaviours of the existing class (called
superclass). The new class is referred to as subclass.
v Programmer can add more attributes and behaviours to the subclass, hence,
normally subclasses have more features than their super classes.
Very Important:
v Don’t use inheritance unless all or most inherited attributes and methods make sense.
v For example, mathematically a circle is-a (an) oval, however you should not inherit a class circle
from a class oval. A class oval can have one method to set width and another to set height.
Very Important:
v Getting “Is-a” versus “Has-a” relationships correct is both subtle and potentially critical. You
should consider all possible future usages of the classes before finalising the hierarchy.
v It is possible that obvious solutions may not work for some applications.
v For example,
a circle can be described by the x, y position of its centre and by its radius.
v By defining the Circle class (as below), we can create a new data type.
For example,
Circle c ;
c = new Circle ( ) ;
OR
c.x = 2;
c.y = 5;
c.r = 1;
a = c.area( );
First Approach:
Important:
v Considering the variable “c” is of type Circle,
v we can only access attributes and methods available in the class Circle.
v we cannot call draw method for “c”.
Object Class :
v Its the only class that does not have a superclass.
v The methods defined by Object can be called by any Java object (instance).
v Often we need to override the following methods:
• toString()
o read the API at https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#toString()
• equals()
o read the API at
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#equals(java.lang.Object)
• hasCode()
Polymorphism
• An object’s ability to decide what method to apply to itself, depending on where
it is in the inheritance hierarchy, is usually called polymorphism.
However,
v if all the three classes define f(), then
calling super.f() in class C invokes class B’s definition of the method.
v Importantly, in this case, there is no way to invoke A.f() from within class C.
v Note that super.super.f() is NOT legal Java syntax.
COMP2511: OOP in Java 33
Method Overloading
Defining methods with the same name and different argument or return types is called
method overloading.
In Java,
v a method is distinguished by its method signature - its name, return type, and by the
number, type, and position of its arguments
For example,
double add(int, int)
double add(int, double)
double add(float, int)
double add(int, int, int)
double add(int, double, int)
This can help in maintaining the consistency of the data for an object, that means the state
of an object.
Visibility Modifiers
Java provides five access modifiers (for variables/methods/classes),
v public - visible to the world
v private - visible to the class only
v protected - visible to the package and all subclasses
v No modifier (default) - visible to the package
v https://docs.oracle.com/javase/tutorial/
v https://www.w3schools.com/java/default.asp
v https://www.tutorialspoint.com/java/index.htm