0% found this document useful (0 votes)
4 views

Encapsulation Inhetance and Polymorphism

The document provides an overview of programming concepts such as variables, encapsulation, inheritance, and polymorphism in Java. It explains how to declare variables, use access modifiers, and implement getter and setter methods to manage private data. Additionally, it illustrates the concepts of subclassing and method overriding through examples of classes like Person, Vehicle, and Animal.

Uploaded by

nestormadrigal01
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Encapsulation Inhetance and Polymorphism

The document provides an overview of programming concepts such as variables, encapsulation, inheritance, and polymorphism in Java. It explains how to declare variables, use access modifiers, and implement getter and setter methods to manage private data. Additionally, it illustrates the concepts of subclassing and method overriding through examples of classes like Person, Vehicle, and Animal.

Uploaded by

nestormadrigal01
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

CO M P U T E R

PR O G R A M M IN
G
Let’s have some recap!
DIRECTION: Reveal the word by analyzing the given set of pictures and letters.
CLUE: KINDS OF MATERIALS INSERTING IN MICROSOFT
WORD
And mean.

S_ R_NG B_O__ I _ _EG _


_N _
STRING BOOLEAN INTEGER
Guess a word for the
picture!
Pre-test!
Direction: How to declare a variable inside our programming
language. Write your answer on the board.
Objectives

Definition of variable.
Identify the different types of variables.
Declaring(creates)variables.
ENCA P SU LAT I O N
What is access modifier?
 The meaning of Encapsulation, is to make sure that
"sensitive" data is hidden from users. To achieve this, you
must:

 declare class variables/attributes as private


 provide public get and set methods to access and update the
value of a private variable
Get and Set
 You learned from the previous chapter that 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

// Getter
public String getName() {
return name;
}

// Setter
public void setName(String newName) {
this.name = newName;
}
}
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.

 However, as the name variable is declared as private,


we cannot access it from outside this class
public class Main {
public static void main(String[] args) {
Person myObj = new Person();
myObj.name = "John"; // error
System.out.println(myObj.name); // error
}
}
Output
John

If the variable was declared as public, we would expect


the following expecr output.
final class Vehicle {
protected String brand = "Ford";
public void honk() {
System.out.println("Tuut, tuut!");
}
}

class Main extends Vehicle {


private String modelName = "Mustang";
public static void main(String[] args) {
Main myFastCar = new Main();
myFastCar.honk();
System.out.println(myFastCar.brand + " " + myFastCar.modelName);
}
}
However, as we try to access a private variable, we
get an error:

MyClass.java:4: error: name has private access in


Person
myObj.name = "John";
^
MyClass.java:5: error: name has private access in
Person
System.out.println(myObj.name);
^
2 errors
Java Inheritance (Subclass and Superclass)
 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.

 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);
}
}
Output
Tuut, tuut!
Ford Mustang
Java Polymorphism
 Polymorphism means "many forms", and it occurs
when we have many classes that are related to each
other by inheritance.

 Like we specified in the previous chapter; 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
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");
}
}
Assessment
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");
}
}
Exercise how to declared polymorphism
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();
}
 Output
The animal makes a sound
The pig says: wee wee
The dog says: bow wow

You might also like