MS1104 2
MS1104 2
MS1104 2
CHAPTER 2
OBJECT-ORIENTED PROGRAMMING WITH
JAVA
Methods are used to perform certain actions, and they are also known as functions.
Everything in Java is associated with classes and objects, along with its
attributes and methods. For example: in real life, a car is an object. The
car has attributes, such as weight and color, and methods, such as drive
and brake.
A Class is like an object constructor, or a "blueprint" for creating
objects.
Parameterized constructor
Methods are used to perform certain actions, and they are also known
as functions.
Why use methods? To reuse code: define the code once, and use it many
times.
}
© ISBAT UNIVERSITY – 2019. 11/20/2021
Access Modifiers
Modifier Description
Modifier Description
public The code is accessible for all classes
private The code is only accessible within the declared class
default The code is only accessible in the same package. This is used when you don't
specify a modifier.
protected The code is accessible in the same package and subclasses.
Modifier Description
final The class cannot be inherited by other classes (You will learn more about inheritance in
the Inheritance chapter)
abstract The class cannot be used to create objects (To access an abstract class, it must be inherited
from another class. You will learn more about inheritance in the Inheritance chapter)
transient Attributes and methods are skipped when serializing the object containing them
In the example below, the Car class (subclass) inherits the attributes and
methods from the Vehicle class (superclass):
© ISBAT UNIVERSITY – 2019. 11/20/2021
class Vehicle {
protected String brand = "Ford"; // Vehicle attribute
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
}
}
class Car extends Vehicle {
private String modelName = "Mustang"; // Car attribute
public static void main(String[] args) {
// Create a myCar object
Car myCar = new Car();
// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();
// Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
System.out.println(myCar.brand + " " + myCar.modelName);
} © ISBAT UNIVERSITY – 2019. 11/20/2021
}
Java Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are related to each other
by inheritance.
Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another
class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action
in different ways.
For example, think of a superclass called Animal that has a method called animalSound(). Subclasses of
Animals could be Pigs, Cats, Dogs, Birds - And they also have their own implementation of an animal sound
(the pig oinks, and the cat meows, etc.):
class MyMainClass {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
} © ISBAT UNIVERSITY – 2019. 11/20/2021
}
Notes on Interfaces:
Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not
possible to create an "Animal" object in the MyMainClass)
Interface methods does not have a body - the body is provided by the "implement" class
On implementation of an interface, you must override all of its methods
Interface methods are by default abstract and public
Interface attributes are by default public, static and final
An interface cannot contain a constructor (as it cannot be used to create objects)
Why And When To Use Interfaces?
1) To achieve security - hide certain details and only show the important details of an object
(interface).
2) Java does not support "multiple inheritance" (a class can only inherit from one superclass).
However, it can be achieved with interfaces, because the class can implement multiple interfaces.
Note: To implement multiple interfaces, separate them with a comma (see example below).
interface SecondInterface {
public void myOtherMethod(); // interface method
}
class MyMainClass {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
© ISBAT
} UNIVERSITY – 2019. 11/20/2021
Final Keyword
The final keyword in java is used to restrict the user. The java final
keyword can be used in many context. Final can be:
variable
method
class
For Example:
class Bike{
final void run(){System.out.println("running...");}
}
class Honda2 extends Bike{
public static void main(String args[]){
new Honda2().run();
}
}
© ISBAT UNIVERSITY – 2019. 11/20/2021
Thank You