OOPs
OOPs
Note: All Java programs starts processing from the main() method. Therefore, it is a mandatory
part of every program.
1
OBJECT: is an instance of a class. When an objects is created, it inherits all the variables and
methods of the class.
CREATE AN OBJECT
In Java, an object is created from a class. To create an object of a class, specify the class name,
followed by the object name and use the keyword new
SYNTAX
ClassName object = new ClassName ();
EXAMPLE
Example obj1 = new Example ();
2
public class Student {
String fname = "Musa";
String lname = "Isa";
int age = 24;
public static void main(String[] args) {
Student std= new Student();
System.out.println("Name: " + std.fname + " " + std.lname);
System.out.println("Age: " + std.age);
}
}
3
ENCAPSULATION
Encapsulation is an OO concept that is used to hide sensitive hidden from the users. To achieve
that, a programmer:
WHY ENCAPSULATION
private variables can only be accessed within the same class (an outside class has no access to
it). However, it is possible to access them if we provide public get and set methods.
The get method returns the variable value, and the set method sets the value.
Syntax for both is that they start with either get or set, followed by the name of the variable,
with the first letter in upper case:
4
public class Main {
System.out.println(myObj.name); // error
// Instead, we use the getName() and setName() methods to access and update the variable:
System.out.println(myObj.getName());
EXAMPLE EXPLAINED
The set method takes a parameter (newName) and assigns it to the name variable.
The this keyword is used to refer to the current object.
5
JAVA INHERITANCE
Inheritance is an OO concepts that allows a subclass (child class) inherits the attributes and
methods of the super class (parent class).
In the example below, the Car class (subclass) inherits the attributes and methods from
the Vehicle class (superclass):
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);
}
}
6
THE FINAL KEYWORD
If you don't want other classes to inherit from a class, use the final keyword:
SYNTAX
final class Vehicle {
...code…
}
EXAMPLE
final class Show{
//final class method
public void show(){
System.out.println("Inside final method");
}
}
class FinalClassTest extends Show{
public void show(){
System.out.println("Inside overridden method of final class");
}
}
public class Main {
public static void main(String args[]){
//creating object of FinalClassTest Class
FinalClassTest obj = new FinalClassTest();
//method call
obj.show();
}
}
OUTPUT
error: cannot inherit from final Show
7
class FinalClassTest extends Show{
POLYMORPHISM
Polymorphism means "many forms". It’s an object oriented concept that allows a method to have
different form and perform different task.
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 Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
8
9
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}
class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Create a Animal object
Animal myPig = new Pig(); // Create a Pig object
Animal myDog = new Dog(); // Create a Dog object
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
}
10
ABSTRACTION
abstraction is the process of hiding certain details and showing only essential information to the
user.
The abstract keyword is a non-access modifier, used for classes and methods:
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be
inherited from another class).
Abstract method: can only be used in an abstract class, and it does not have a body. The body is
provided by the subclass (inherited from).
EXAMPLE
abstract class Animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}
From the example above, it is not possible to create an object of the Animal class:
Animal myObj = new Animal(); // will generate an error
To access the abstract class, it must be inherited from another class. Let's convert the Animal
class we used in the Polymorphism chapter to an abstract class:
11
// Abstract class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep() {
System.out.println("Zzz");
}
}
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
12