0% found this document useful (0 votes)
2 views9 pages

OOPl Core Principles of OOP Lesson002

The document explains the core principles of Object-Oriented Programming (OOP), focusing on inheritance and encapsulation. Inheritance allows a class to acquire properties and behaviors from another class, facilitating code reusability, while encapsulation involves wrapping data and methods together to restrict access and enhance data hiding. Examples illustrate single, multilevel, and hierarchical inheritance, as well as how to implement encapsulation using private variables and public getter/setter methods.

Uploaded by

BRIAN MUTURI
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)
2 views9 pages

OOPl Core Principles of OOP Lesson002

The document explains the core principles of Object-Oriented Programming (OOP), focusing on inheritance and encapsulation. Inheritance allows a class to acquire properties and behaviors from another class, facilitating code reusability, while encapsulation involves wrapping data and methods together to restrict access and enhance data hiding. Examples illustrate single, multilevel, and hierarchical inheritance, as well as how to implement encapsulation using private variables and public getter/setter methods.

Uploaded by

BRIAN MUTURI
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/ 9

Core principle of OOP

Inheritance

Inheritance can be defined as the procedure or mechanism of acquiring all


the properties and behavior of one class to another, i.e. acquiring the
properties and behavior of child class from the parent class. This concept was
built in order to achieve the advantage of creating a new class that gets built
upon an already existing class(es). It is mainly used for code reusability
within a Java program. The class that gets inherited taking the properties of
another class is the subclass or derived class or child class. Again, the class
whose properties get inherited is the superclass or base class or parent class.
The keyword extends is used to inherit the properties of the base class to
derived class.

The structure of using this keyword looks something like this:

class base
{
.....
.....
}
class derive extends base
{
.....
..
...
}
Single inheritance- One class inherits from another
class Teacher {

void teach() {

System.out.println("Teaching subjects");

class Student extends Teacher {

void listen() {

System.out.println("Listening to teacher");

}
class CheckForInheritance {

public static void main(String[] args) {

Student s1 = new Student();

s1.teach(); // Inherited from Teacher

s1.listen(); // Defined in Student

output

Teaching subjects

Listening to teacher

Teacher is the parent (superclass) with a method teach().

Student is the child (subclass) that extends Teacher and adds its own method
listen().

This is Single Inheritance because only one parent class is involved.

In this type of inheritance, a derived class gets created from another derived
class and can have any number of levels.

Multilevel Inheritance- HomeTution inherits from Student, which inherits


from Teacher

class Teacher {
void teach() {
System.out.println("Teaching subject");
}
}

class Student extends Teacher {


void listen() {
System.out.println("Listening");
}
}

class HomeTution extends Student {


void explains() {
System.out.println("Does homework");
}
}

class CheckForInheritance {
public static void main(String[] argu) {
HomeTution h = new HomeTution
h.explains(); // Output: Does homework
h.teach(); // Inherited from Teacher
h.listen(); // Inherited from Student
}
}

Output

Does homework

Teaching subject

Listening
In this type of inheritance, there are more than 1 derived classes which get
created from one single base class.

Teacher - base class

Student extends Teacher - gets teach()

HomeTution extends Student - gets both teach() and listen()

Hierarchical Inheritance - Student and Principal inherit from Teacher

class Teacher {

void teach() {

System.out.println("Teaching subject");

class Student extends Teacher {

void listen() {

System.out.println("Listening");

class Principal extends Teacher {

void evaluate() {

System.out.println("Evaluating");
}

class CheckForInheritance {

public static void main(String[] argu) {

Principal p = new Principal();

p.evaluate(); // Output: Evaluating

p.teach(); // Inherited from Teacher

Output

Evaluating

Teaching subject

Here ;Teacher is the base class.Student and Principal both inherit from
Teacher.

Let us imagine a situation where there are three classes: A, B and C. The C
class inherits A and B classes. In case, class A and class B have a method
with same name and type and as a programmer, you have to call that method
from child class's (C) object, there will be ambiguity as which method will
be called either of A or of B class.
So Java reduces this hectic situation by the use of interfaces which
implements this concept and reduce this problem; as compile-time errors are
tolerable than runtime faults in the program.

Encapsulation

Encapsulation is one of the four fundamental OOP concepts. The other three are
inheritance, polymorphism, and abstraction. Encapsulation in Java is a
mechanism of wrapping the data variables and code acting on the data
methods together as a single unit. In encapsulation the variables of a class will be
hidden from other classes, and can be accessed only through the methods of their
current class, therefore it is also known as data hiding. To achieve encapsulation in
Java Declare the variables of a class as private. Provide public setter and getter
methods to modify and view the variables values.
Example: Below given is an example that demonstrates how to achieve
Encapsulation in Java:
public class EncapTest{
// Step 1: Data Hiding
private String name;
private String idNum;
private int age;
// Step 2: Public Getters - Controlled access
public int getAge(){
return age;
}
public String getName(){
return name;
}
public String getIdNum(){
return idNum;
}
// Step 3: Public Setters - Controlled modification
public void setAge( int newAge){
age = newAge;
}
public void setName(String newName){
name = newName;
}
public void setIdNum( String newId){
idNum = newId;
}
}

The public setXXX and getXXX methods are the access points of the instance
variables of the EncapTest class. Normally, these methods are referred as getters
and setters. Therefore, any class that wants to access the variables should access
them through these getters and setters.
The variables of the EncapTest class can be accessed as below:
public class RunEncap{
public static void main(String args[]){
EncapTest encap = new EncapTest();
encap.setName("James");
encap.setAge(20);
encap.setIdNum("12343ms");
System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge());
}
}
Output
Name : James Age : 20
Benefits of Encapsulation:
The fields of a class can be made read-only or write-only. A class can have total
control over what is stored in its fields. The users of a class do not know how the
class stores its data. A class can change the data type of a field and users of the
class do not need to change any of their code.

You might also like