0% found this document useful (0 votes)
36 views5 pages

Norzagaray College: Abstraction

The document discusses the four main principles of object-oriented programming (OOP): abstraction, inheritance, polymorphism, and encapsulation. It provides examples to illustrate each principle. Abstraction allows hiding unnecessary details and showing only important features through abstract classes and interfaces. Inheritance enables code reuse by extending parent classes. Polymorphism allows the same methods to work in different forms using method overloading and overriding. Encapsulation protects data by making fields private and providing public getter and setter methods.

Uploaded by

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

Norzagaray College: Abstraction

The document discusses the four main principles of object-oriented programming (OOP): abstraction, inheritance, polymorphism, and encapsulation. It provides examples to illustrate each principle. Abstraction allows hiding unnecessary details and showing only important features through abstract classes and interfaces. Inheritance enables code reuse by extending parent classes. Polymorphism allows the same methods to work in different forms using method overloading and overriding. Encapsulation protects data by making fields private and providing public getter and setter methods.

Uploaded by

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

NORZAGARAY COLLEGE

Municipal Compound, Norzagaray, Bulacan

COLLEGE OF COMPUTING STUDIES

OOP101 – OBJECT - ORIENTED PROGRAMMING

Second Semester, S.Y. 2019-2020

Name: Cruz, Jhelaiza E. Date: 6/14/2020


Instructor: Mr. Raymond Gatacelo Course, Yr. & Sec: ACT/BSCS 2B

Principles of OOP

Abstraction
With abstraction, you can hide the internal workings of an object and only show the features the user needs
to know about. Java provides two ways to implement abstraction: abstract classes and interfaces. With
abstract classes, you can achieve partial abstraction, while interfaces make total (100%) abstraction possible.

Example

abstract class Animal {


abstract void move();
abstract void eat();

void label() {
System.out.println("Animal's data:");
}
}

class Bird extends Animal {

void move() {
System.out.println("Moves by flying.");
}
void eat() {
System.out.println("Eats birdfood.");
}
}

class Fish extends Animal {


void move() {
System.out.println("Moves by swimming.");
}
void eat() {
System.out.println("Eats seafood.");
}
}
class TestBird {
public static void main(String[] args) {
Animal myBird = new Bird();

myBird.label();
myBird.move();
myBird.eat();
}
}

class TestFish {
public static void main(String[] args) {
Animal myFish = new Fish();

myFish.label();
myFish.move();
myFish.eat();
}
}

Output :

(TestBird output)

Animal's data:
Moves by flying.
Eats birdfood.

(Test Fish output )

Animal's data:
Moves by swimming.
Eats seafood.
Inheritance
Inheritance allows us to extend a class with child classes that inherit the fields and methods of the parent class.
It’s an excellent way to achieve code reusability. In Java, we need to use the extends keyword to create a child
class.

class Bird {
public String reproduction = "egg";
public String outerCovering = "feather";

public void flyUp() {


System.out.println("Flying up...");
}
public void flyDown() {
System.out.println("Flying down...");
}
}

class Eagle extends Bird {


public String name = "eagle";
public int lifespan = 15;
}

class TestEagle {
public static void main(String[] args) {
Eagle myEagle = new Eagle();

System.out.println("Name: " + myEagle.name);


System.out.println("Reproduction: " + myEagle.reproduction);
System.out.println("Outer covering: " + myEagle.outerCovering);
System.out.println("Lifespan: " + myEagle.lifespan);
myEagle.flyUp();
myEagle.flyDown();
}
}

(Output of TestEagle)

Reproduction: another egg


Outer covering: feather
Lifespan: 15
Flying up...
Flying down...

class Animal {

public void eat() {


System.out.println("I can eat");
}

public void sleep() {


System.out.println("I can sleep");
}
}

class Dog extends Animal {


public void bark() {
System.out.println("I can bark");
}
}
class Main {
public static void main(String[] args) {

Dog dog1 = new Dog();

dog1.eat();
dog1.sleep();

dog1.bark();
}
}

I can eat
I can sleep
I can bark

Polymorphism
Polymorphism makes it possible to use the same entity in different forms. In Java, this means that you can
declare several methods with the same name until they are different in certain characteristics. Java provides
us with two ways to implement polymorphism: method overloading and method overriding.

class Bird {
public void fly() {
System.out.println("The bird is flying.");
}
public void fly(int height) {
System.out.println("The bird is flying " + height + " feet high.");
}
public void fly(String name, int height) {
System.out.println("The " + name + " is flying " + height + " feet high.");
}
}

class TestBird {
public static void main(String[] args) {
Bird myBird = new Bird();

myBird.fly();
myBird.fly(10000);
myBird.fly("eagle", 10000);
}
}

( Output of TestBird )

The bird is flying.


The bird is flying 10000 feet high.
The eagle is flying 10000 feet high.
Encapsulation
With encapsulation, you can protect the fields of a class. To do so, declare the fields as private and providing
access to them with getter and setter methods.

class Animal {
private String name;
private double averageWeight;
private int numberOfLegs;

// Getter methods
public String getName() {
return name;
}
public double getAverageWeight() {
return averageWeight;
}
public int getNumberOfLegs() {
return numberOfLegs;
}

// Setter methods
public void setName(String name) {
this.name = name;
}
public void setAverageWeight(double averageWeight) {
this.averageWeight = averageWeight;
}
public void setNumberOfLegs(int numberOfLegs) {
this.numberOfLegs = numberOfLegs;
}
}

public class TestAnimal {


public static void main(String[] args) {
Animal myAnimal = new Animal();

myAnimal.setName("Eagle");
myAnimal.setAverageWeight(1.5);
myAnimal.setNumberOfLegs(2);

System.out.println("Name: " + myAnimal.getName());


System.out.println("Average weight: " + myAnimal.getAverageWeight() + "kg");
System.out.println("Number of legs: " + myAnimal.getNumberOfLegs());
}
}

(Output for TestAnimal)


Name: Eagle
Average weight: 1.5kg
Number of legs: 2

You might also like