OOPS CONCEPTS
I MCA
JAVA CLASS
• A class is a blueprint for the object. Before we create an object, we
first need to define the class.
• We can think of the class as a sketch (prototype) of a house. It
contains all the details about the floors, doors, windows, etc. Based
on these descriptions we build the house. House is the object.
• Since many houses can be made from the same description, we can
create many objects from a class.
Create a class in Java
We can create a class in Java using the
class keyword. For example,
class Bicycle {
class ClassName { // state or field
private int gear = 5;
// fields
// methods // behavior or method
} public void braking() {
System.out.println("Working of Braking");
Here, fields (variables) and methods }
represent the state and behavior of the }
object respectively.
// stores the value for light
Java Objects // true if light is on
// false if light is off
boolean isOn;
An object is called an instance of a class.
For example, suppose Bicycle is a class then // method to turn on the light
MountainBicycle, SportsBicycle, TouringBicycle, etc void turnOn() {
can be considered as objects of the class. isOn = true;
System.out.println("Light on? " + isOn);
Creating an Object in Java }
Here is how we can create an object of a class. // method to turnoff the light
void turnOff() {
isOn = false;
className object = new className(); System.out.println("Light on? " + isOn);
}
}
// for Bicycle class
Bicycle sportsBicycle = new Bicycle(); public class Main {
public static void main(String[] args) {
Bicycle touringBicycle = new Bicycle(); // create objects led and halogen
Lamp led = new Lamp();
ABSTRACTION
• Abstraction in Java is the process in which we only show
essential details/functionality to the user.
• The non-essential implementation details are not displayed to
the user.
• In Java, abstraction is achieved by interfaces and
abstract classes. We can achieve 100% abstraction using
interfaces.
• Consider a real-life example of a man driving a car. The man
only knows that pressing the accelerator will increase the
speed of a car or applying brakes will stop the car, but he does
not know how on pressing the accelerator the speed is actually
increasing, he does not know about the inner mechanism of
the car or the implementation of the accelerator, brakes, etc.
in the car. This is what abstraction is.
Java Abstract classes and Java Abstract
methods
• An abstract class is a class that is declared with an abstract keyword.
• An abstract method is a method that is declared without implementation.
• An abstract class may or may not have all abstract methods. Some of them
can be concrete methods
• A abstract method must always be redefined in the subclass, thus making
overriding compulsory or making the subclass itself abstract.
• Any class that contains one or more abstract methods must also be declared
with an abstract keyword.
• There can be no object of an abstract class. That is, an abstract class can not
be directly instantiated with the new operator.
• An abstract class can have parameterized constructors and the default
constructor is always present in an abstract class.
/ /Abstract class class Main {
public static void main(String[] args) {
abstract class Animal {
Pig myPig = new Pig(); // Create a Pig object
// Abstract method (does not have a body) myPig.animalSound();
public abstract void animalSound(); myPig.sleep();
}
// Regular method }
public void sleep() {
System.out.println("Zzz");
}
The pig says: wee wee
}
Zzz
// Subclass (inherit from Animal)
class Pig extends Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The pig says: wee wee");
}
}
INTERFACE
Another way to achieve abstraction in Java, is with interfaces.
An interface is a completely "abstract class" that is used to group related methods with empty
bodies:
ExampleGet your own Java Server
// interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void run(); // interface method (does not have a body)
}
To access the interface methods, the interface must be "implemented" ( like inherited) by
another class with the implements keyword (instead of extends).
The body of the interface method is provided by the "implement" class:
EXAMPLE
// Interface
interface Animal { class Main {
public void animalSound(); // interface method (does not have a public static void main(String[] args) {
body) Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
public void sleep(); // interface method (does not have a body)
myPig.sleep();
} }
// Pig "implements" the Animal interface }
class Pig implements Animal {
public void animalSound() { The pig says: wee wee
// The body of animalSound() is provided here Zzz
System.out.println("The pig says: wee wee");
}
public void sleep() {
// The body of sleep() is provided here
System.out.println("Zzz");
}
}
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 do 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
ENCAPSULATION
• The meaning of Encapsulation, is to make
sure that "sensitive" data is hidden from
users. To achieve this, you must: However, as the name variable is declared as
• declare class variables/attributes as
private, we cannot access it from outside this
private class:
• provide public get and set methods to
access and update the value of a private Example
public class Main {
public class Person {
public static void main(String[] args) {
private String name; Person myObj = new Person();
public String getName() { myObj.name = "John"; // error
return name; System.out.println(myObj.name); // error
}
}
}
public void setName(String newName) {
this.name = newName;
}
}
INHERITANCE
• In Java, it is possible to inherit attributes and methods from one class
to another. We group the "inheritance concept" into two categories:
• subclass (child) - the class that inherits from another class
• superclass (parent) - the class being inherited from
• To inherit from a class, use the extends keyword.
Why And When To Use "Inheritance"?
• - It is useful for code reusability: reuse attributes and
methods of an existing class when you create a new
class.
FINAL KEYWORD
If you don't want other classes to inherit from a class, use the final keyword:
If you try to access a final class, Java will generate an error:
final class Vehicle {
...
}
class Car extends Vehicle {
...
}
EXAMPLE
class Vehicle {
protected String brand = "Ford";
public void honk() {
System.out.println("Tuut, tuut!");
}
}
class Car extends Vehicle {
private String modelName = "Mustang";
public static void main(String[] args) {
Car myFastCar = new Car();
myFastCar.honk();
System.out.println(myFastCar.brand + " " + myFastCar.modelName);
}
Java Polymorphism
• Polymorphism means "many forms", and it occurs
when we have many classes that are related to each
other by inheritance.
• 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.
class Animal { class Main {
public void animalSound() { public static void main(String[] args) {
System.out.println("The animal makes a sound"); 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();
class Pig extends Animal { myDog.animalSound();
public void animalSound() { }
}
System.out.println("The pig says: wee wee");
}
}
The animal makes a sound
The pig says: wee wee
class Dog extends Animal {
The dog says: bow wow
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}
THANK YOU