0% found this document useful (0 votes)
3 views28 pages

Lecture - Object Oriented Java

Uploaded by

sohailhamna50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views28 pages

Lecture - Object Oriented Java

Uploaded by

sohailhamna50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 28

CSC445

Modern Programming Languages

Object Oriented
Programming in Java

CSC445, Modern Programming Languages, Dr. Mubeen


Ghafoor, COMSATS
Object-Oriented Programming in
Java

2
Object-Oriented Nomenclature

• “Class” means a category of things

• “Object” means a particular item that belongs to a class


• Also called an “instance”

3
Class and Objects

Circle UML Graphical notation for classes

radius: double UML Graphical notation for fields

UML Graphical notation for methods


findArea(): double

new Circle() new Circle()

circle1: Circle circlen: Circle UML Graphical notation


for objects
radius = 2 ... radius = 5
Class Declaration

class Circle {
double radius = 1.0;

double findArea(){
return radius * radius * 3.14159;
}
}
Declaring/Creating Objects
in a Single Step

ClassName objectReference = new ClassName();

Example:
Circle myCircle = new Circle();
Accessing Objects
• Referencing the object’s data:
objectReference.data
myCircle.radius

• Invoking the object’s method:


objectReference.method
myCircle.findArea()
Example – Circle Class

public class Circle { // class name


double radius; // variables
String color;

double getRadius() {...} // methods


double getArea() {...}
}
// Declare 3 instances of the class Circle, c1, c2, and c3
Circle c1, c2, c3;
// Allocate and construct the instances via new operator
c1 = new Circle();
// You can declare and construct in the same statement
Circle c4 = new Circle();
Constructors.
Constructors are a special kind of methods that
are invoked using the new operator when an object
is created.
Constructors play the role of initializing
objects.
 Constructors must have the same name as the
class itself.
 Constructors do not have a return type—not even
void.
constructor with no parameters is referred to
as a default constructor.
Constructors
Circle(double r) {
radius = r;
}

Circle() {
radius = 1.0;
}

myCircle = new Circle(5.0);


Example - Circle.java
// Define the Circle class
public class Circle { // Save as "Circle.java"
double radius; String color;
// Constructors (overloaded)
public Circle() { // 1st Constructor
radius = 1.0;
color = "red";
}
public Circle(double r) { // 2nd Constructor
radius = r;
color = "red";
}
public Circle(double r, String c) { // 3rd Constructor
radius = r;
color = c;
}
// Public methods
public double getRadius() {
return radius;
}
public String getColor() {
return color;
}
public double getArea() {
return radius*radius*Math.PI;
}
}
Test Class for Circle.java - Example
// Test driver program for the Circle class
public class TestCircle { // Save as "TestCircle.java"
public static void main(String[] args) { // Execution entry point
// Construct an instance of the Circle class called c1
Circle c1 = new Circle(2.0, "blue"); // Use 3rd constructor
System.out.println("Radius is " + c1.getRadius() // use dot operator to invoke member methods
+ " Color is " + c1.getColor()
+ " Area is " + c1.getArea());

// Construct another instance of the Circle class called c2


Circle c2 = new Circle(2.0); // Use 2nd constructor
System.out.println("Radius is " + c2.getRadius()
+ " Color is " + c2.getColor()
+ " Area is " + c2.getArea());

// Construct yet another instance of the Circle class called c3


Circle c3 = new Circle(); // Use 1st constructor
System.out.println("Radius is " + c3.getRadius()
+ " Color is " + c3.getColor()
+ " Area is " + c3.getArea());
}
}
Output of Test Class

Radius is 2.0 Color is blue Area is 12.566370614359172


Radius is 2.0 Color is red Area is 12.566370614359172
Radius is 1.0 Color is red Area is 3.141592653589793
Keyword "this"

public class Circle {


double radius; // Member variable called "radius"
public Circle(double radius) { // Method's argument also called "radius"
this.radius = radius;
// "this.radius" refers to this instance's member variable
// "radius" resolved to the method's argument.
}
...
}
Array of Objects

Circle[] circleArray = new


Circle[10];

An array of objects is actually an array of


reference variables.
So invoking circleArray[1].findArea() involves two
levels of referencing as shown in the next figure.
circleArray references to the entire array.
circleArray[1] references to a Circle object.
Array of Objects, cont.

Circle[] circleArray = new


Circle[10];

circleArray reference circleArray[0] Circle object 0


circleArray[1]

… Circle object 1

circleArray[9] Circle object 9


Java - Inheritance

• Inheritance can be defined as the process where one class acquires


the properties (methods and fields) of another. With the use of
inheritance the information is made manageable in a hierarchical
order.

• The class which inherits the properties of other is known as subclass


(derived class, child class) and the class whose properties are
inherited is known as superclass (base class, parent class).
class Calculation{
int z;
public void addition(int x, int y){
z = x+y; Java - Inheritance
System.out.println("The sum of the given numbers:"+z);
}
public void Substraction(int x,int y){
z = x-y;
System.out.println("The difference between the given numbers:"+z);
}
}
public class My_Calculation extends Calculation{
public void multiplication(int x, int y){
z = x*y;
System.out.println("The product of the given numbers:"+z);
}
public static void main(String args[]){
int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Substraction(a, b);
demo.multiplication(a, b);
}
The sum of the given numbers:30
The difference between the given numbers:10
The product of the given numbers:200
Java - Interfaces

An interface is a reference type in Java, it is similar to class, it is a


collection of abstract methods. A class implements an interface,
thereby inheriting the abstract methods of the interface.
Writing an interface is similar to writing a class. But a class describes
the attributes and behaviours of an object. And an interface contains
behaviours that a class implements.

/* File name : Animal.java */


interface Animal {

public void eat();


public void travel();
}
Implementing Interfaces
• When a class implements an interface, you can think of the class as signing a contract,
agreeing to perform the specific behaviors of the interface.
• A class uses the implements keyword to implement an interface. The implements keyword
appears in the class declaration following the extends portion of the declaration.

/* File name : MammalInt.java */


public class MammalInt implements Animal{
public void eat(){
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels");
}
public int noOfLegs(){
return 0;
}
public static void main(String args[]){
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
Example 1: Instance Variables (“Fields” or
“Data Members”)
class Ship1 {
public double x, y, speed, direction;
public String name;
}

public class Test1 {


public static void main(String[] args) {
Ship1 s1 = new Ship1();
s1.x = 0.0;
s1.y = 0.0;
s1.speed = 1.0;
s1.direction = 0.0; // East
s1.name = "Ship1";
System.out.println(s1.name + " is initially at ("
+ s1.x + "," + s1.y + ").");
s1.x = s1.x + s1.speed
* Math.cos(s1.direction * Math.PI / 180.0);
s1.y = s1.y + s1.speed
* Math.sin(s1.direction * Math.PI / 180.0);
System.out.println(s1.name + " has moved to ("
+ s1.x + "," + s1.y + ").");

} 22

}
Instance Variables: Results

Output:

Ship1 is initially at (1,0).


Ship2 has moved to (-1.41421,1.41421).

23
Example 2: Methods
class Ship2 {
public double x=0.0, y=0.0, speed=1.0, direction=0.0;
public String name = "UnnamedShip";

private double degreesToRadians(double degrees) {


return(degrees * Math.PI / 180.0);
}

public void move() {


double angle = degreesToRadians(direction);
x = x + speed * Math.cos(angle);
y = y + speed * Math.sin(angle);
}

public void printLocation() {


System.out.println(name + " is at ("
+ x + "," + y + ").");
}
}

24
Methods (Continued)
public class Test2 {
public static void main(String[] args) {
Ship2 s1 = new Ship2();
s1.name = "Ship1";
Ship2 s2 = new Ship2();
s2.direction = 135.0; // Northwest
s2.speed = 2.0;
s2.name = "Ship2";
s1.move();
s2.move();
s1.printLocation();
s2.printLocation();
}
}
• Output:
Ship1 is at (1,0).
Ship2 is at (-1.41421,1.41421).

25
Example 3: Constructors
class Ship3 {
public double x, y, speed, direction;
public String name;

public Ship3(double x, double y,


double speed, double direction,
String name) {
this.x = x; // "this" differentiates instance vars
this.y = y; // from local vars.
this.speed = speed;
this.direction = direction;
this.name = name;
}

private double degreesToRadians(double degrees) {


return(degrees * Math.PI / 180.0);
}
...

26
Constructors (Continued)
public void move() {
double angle = degreesToRadians(direction);
x = x + speed * Math.cos(angle);
y = y + speed * Math.sin(angle);
}
public void printLocation() {
System.out.println(name + " is at ("
+ x + "," + y + ").");
}
}
public class Test3 {
public static void main(String[] args) {
Ship3 s1 = new Ship3(0.0, 0.0, 1.0, 0.0, "Ship1");
Ship3 s2 = new Ship3(0.0, 0.0, 2.0, 135.0, "Ship2");
s1.move();
s2.move();
s1.printLocation();
s2.printLocation();
}
}

Introduction to Object Oriented


27
Programming
Constructor Example: Results

• Output:
Ship1 is at (1,0).
Ship2 is at (-1.41421,1.41421).

Introduction to Object Oriented


28
Programming

You might also like