0% found this document useful (0 votes)
7 views12 pages

OOPs

Chapter Four covers fundamental Object-Oriented Programming (OOP) concepts including classes, objects, inheritance, polymorphism, and data abstraction. It explains how to define classes and create objects in Java, the importance of encapsulation for data security, and how inheritance allows subclasses to inherit attributes and methods from superclasses. Additionally, it discusses polymorphism as a means to perform different tasks with the same method and introduces abstraction through abstract classes and methods.

Uploaded by

pwagbadi4546
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

OOPs

Chapter Four covers fundamental Object-Oriented Programming (OOP) concepts including classes, objects, inheritance, polymorphism, and data abstraction. It explains how to define classes and create objects in Java, the importance of encapsulation for data security, and how inheritance allows subclasses to inherit attributes and methods from superclasses. Additionally, it discusses polymorphism as a means to perform different tasks with the same method and introduces abstraction through abstract classes and methods.

Uploaded by

pwagbadi4546
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

CHAPTER FOUR

BASIC OOP CONCEPTS: CLASSES, OBJECT, INHERITANCE, POLYMORPHISM,


DATA ABSTRACTION
Class
CLASS is defined as a template for objects. A class is like a code container.
The definition of the class starts with or without an access modifier (in this case, no access
modifier), which specifies which classes have access to it. This is followed by the keyword class
and the name of the class (MyFirstProgram). Every class definition is enclosed within a curly
brace {}.
SYNTAX
AccessModifier class ClassName {
//vaiables
//methods
}
EXAMPLE
public class Student {
}

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 ();

public class Example{


int x = 5;
public static void main(String[] args) {
Example obj1 = new Example();
Example obj2 = new Example();
System.out.println(obj1.x);
System.out.println(obj2.x);
}
}

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:

i. declare class variables/attributes as private


ii. provide public get and set methods to access and update the value of a private variable

WHY ENCAPSULATION

i. Better control of class attributes and methods


ii. Class attributes can be made read-only (if you only use the get method), or write-only (if
you only use the set method)
iii. Flexible: the programmer can change one part of the code without affecting other parts
iv. Increased security of data

GET AND SET

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:

public class Person {


private String name; // private = restricted access
// Get method
public String getName() {
return name;
}
// Set method
public void setName(String newName) {
this.name = newName;
}
}

4
public class Main {

public static void main(String[] args) {

Person myObj = new Person();

myObj.name = "John"; // error

System.out.println(myObj.name); // error

// Instead, we use the getName() and setName() methods to access and update the variable:

public class Main {

public static void main(String[] args) {

Person myObj = new Person();

myObj.setName("John"); // Set the value of the name variable to "John"

System.out.println(myObj.getName());

EXAMPLE EXPLAINED

The get method returns the value of the variable name.

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).

i. subclass (child) - the class that inherits from another class


ii. superclass (parent) - the class being inherited from

To inherit from a class, use the extends keyword.

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…
}

class Car extends 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");
}
}

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");
}
}
Remember from the Inheritance chapter that we use the extends keyword to inherit from a class.
Now we can create Pig and Dog objects and call the animalSound() method on both of them:

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.

Abstract Classes and Methods

Abstraction can be achieved with either abstract classes or interfaces

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).

An abstract class can have both abstract and regular methods:

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");
}
}

// 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");
}
}

class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}

12

You might also like