Lab 1 Revision of Object Oriented Programming Concept: Encapsulation
Lab 1 Revision of Object Oriented Programming Concept: Encapsulation
Lab 1 Revision of Object Oriented Programming Concept: Encapsulation
Learning objective:
After this lab student should be able to:
• What is encapsulation? What is data hiding? How to achieve it?
• What is inheritance? Type of inheritance.
• What is polymorphism? Types of polymorphism
• Practical example and exercises?
Encapsulation
Encapsulation is a process of combining data members and functions in a single unit called class.
This is to prevent the access to the data directly, the access to them is provided through the
functions of the class. It is one of the popular feature of Object Oriented Programming (OOPs)
that helps in data hiding.
How Encapsulation is achieved in a class
To do this:
1. Make all the data members private.
2. Create public setter and getter functions for each data member in such a way that the set
function set the value of data member and get function get the value of data member.
Example
package javaapplication1;
/**
*
* @author Shuja
*/
public class Student {
private String name;
private int age ;
private int s_id;
System.out.println(s1.getName());
System.out.println(s1.getAge());
System.out.println(s1.getS_id());
Inheritance
What is Inheritance?
Inheritance is one of the feature of Object-Oriented Programming (OOPs). Inheritance allows a
class to use the properties and methods of another class. In other words, the derived class inherits
the states and behaviors from the base class. The derived class is also called subclass and the base
class is also known as super-class. The derived class can add its own additional variables and
methods. These additional variable and methods differentiates the derived class from the base
class.
Inheritance is a compile-time mechanism. A super-class can have any number of subclasses. But
a subclass can have only one superclass. This is because Java does not support multiple inheritance.
Extends Keywords
Extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends
keyword.
class Super{
.....
.....
}
class Vehicle {
String color;
int speed;
int size;
void attributes() {
System.out.println("Color : " + color);
System.out.println("Speed : " + speed);
System.out.println("Size : " + size);
}
}
The output is
Note:
The derived class inherits all the members and methods that are declared as public or protected. If
declared as private it cannot be inherited by the derived classes. The private members can be
accessed only in its own class. The private members can be accessed through assessor methods as
shown in the example below. The derived class cannot inherit a member of the base class if the
derived class declares another member with the same name.
Single Inheritance
Single inheritance is easy to understand. When a class extends another one class only then we call
it a single inheritance. The below flow diagram shows that class B extends only one class which
is A. Here A is a parent class of B and B would be a child class of A.
Class A
{
public void methodA()
{
System.out.println("Base class method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}
Multilevel Inheritance
Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a
derived class, thereby making this derived class the base class for the new class. As you can see
in below flow diagram C is subclass or child class of B and B is a child class of A. For more
details and example refer – Multilevel inheritance in Java.
Class X
{
public void methodX()
{
System.out.println("Class X method");
}
}
Class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
}
}
Class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
obj.methodX(); //calling grand parent class method
obj.methodY(); //calling parent class method
obj.methodZ(); //calling local method
}
}
Hierarchical Inheritance
In such kind of inheritance one class is inherited by many sub classes. In below example class
B,C and D inherits the same class A. A is parent class (or base class) of B,C & D. Read More at
– Hierarchical Inheritance in java with example program.
Class A
{
public void methodA()
{
System.out.println("method of Class A");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("method of Class B");
}
}
Class C extends A
{
public void methodC()
{
System.out.println("method of Class C");
}
}
Class D extends A
{
public void methodD()
{
System.out.println("method of Class D");
}
}
Class MyClass
{
public void methodB()
{
System.out.println("method of Class B");
}
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}
Super keyword
The super keyword is similar to this keyword following are the scenarios where the super keyword
is used.
• It is used to differentiate the members of superclass from the members of subclass, if they
have same names.
• It is used to invoke the superclass constructor from subclass.
Exercise
Q1 Write a parent class Account that extends class name saving account and saving account
extends account details class.
Saving account
• Minimum bill (int)
• Saving Bill (int)
Account Details
• Deposits (int)
• Widthdrawls(int)
In each class make parameterize constructor and display function. Drive classes invoke super class
constructor and method using super keyword. In the main class make the object of account detail
and display the details of the account.
Polymorphism
Polymorphism in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many
and "morphs" means forms. So polymorphism means many forms.
There are two types of polymorphism in Java: compile-time polymorphism and runtime
polymorphism. We can perform polymorphism in java by method overloading and method
overriding.
Compile-time polymorphism:
Compile-Time polymorphism in java is also known as Static Polymorphism. In this process, the
call to the method is resolved at compile-time. Compile-Time polymorphism is achieved through
Method Overloading..
Example
package staticPolymorphism;
int c = a+b;
int c = a+b+e;
obj.sum ( 30,90);
}
The output of the program will be:
In this program, the sum() method was overloaded with two types with different parameters.
This is the basic concept of compile-time polymorphism in java where we can perform various
operations by using multiple methods having the same name.
Runtime polymorphism
Method Overriding is done when a child or a subclass has a method with the same name,
parameters and return type as the parent or the superclass, then that function overrides the function
in the superclass. In simpler terms, if the subclass provides its definition to a method already
present in the superclass, then that function in the base class is said to be overridden.
It should be noted that runtime polymorphism can only be achieved through functions and not data
members.
Overriding is done by using a reference variable of the superclass. Which method to be called
is determined based on the object which is being referred to by the reference variable. This is
also known as Upcasting.
Upcasting is done when the Parent class’s reference variable refers to the object of the child
class. For example:
Example
class A{}
class B extends A{}
A a=new B(); //upcasting
Example 1:
In this example, we are creating one superclass Animal and three subclasses, Herbivores,
Carnivores and Omnivores. Subclasses extend the superclass and override its eat() method. We
will call the eat() method by the reference variable of Parent class, i.e. Animal class. As it
refers to the base class object and the base class method overrides the superclass method, the
base class method is invoked at runtime. As Java Virtual Machine or the JVM and not the
compiler determines method invocation, it is runtime polymorphism.
class Animal{
void eat(){
System.out.println("Animals Eat");
}
}
class herbivores extends Animal{
void eat(){
System.out.println("Herbivores Eat Plants");
}
}
class omnivores extends Animal{
void eat(){
System.out.println("Omnivores Eat Plants and meat");
}
}
class carnivores extends Animal{
void eat(){
System.out.println("Carnivores Eat meat");
}
}
class main{
public static void main(String args[]){
Animal A = new Animal();
Animal h = new herbivores(); //upcasting
Animal o = new omnivores(); //upcasting
Animal c = new carnivores(); //upcasting
A.eat();
h.eat();
o.eat();
c.eat();
}
}
Exercise