You are on page 1of 27

Lecture 12

Object Oriented Programming


Principles
What is Encapsulation?

 Encapsulation in Java is a process of wrapping code and


data together into a single unit.
 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.
Advantages of Encapsulation

 It provides you the control over the data.


 It is a way to achieve data hiding in Java.
 providing only a setter or getter method,
you can make the class read-only or
write-only.
• Flexible: the programmer can change one
part of the code without affecting other
parts.
• Increased security of data
What is Inheritance?
 is a mechanism in which one object acquires the
properties and behaviors of a parent object.
 Superclass/parent - the class being inherited from.
 Subclass/child- the class that inherits from another
class.
 To inherit from a class, use the extends keyword.
 Why use inheritance in java
– For Method Overriding
– For Code Reusability.
Types of Inheritance?
is-a relationship
 In Java, inheritance is an is-a relationship. That is,
we use inheritance only if there exists an is-a
relationship between two classes. For example,
• Car is a Vehicle
• Orange is a Fruit
• Surgeon is a Doctor
• Dog is an Animal
 Here, Car can inherit from Vehicle, Orange can
inherit from Fruit, and so on.
Dog Class
public class Dog {
private String name;
private int fleas;

public Dog(String n, int f) {


name = n;
fleas = f;
}

public String getName() { return name; }

public int getFleas() { return fleas; }

public void speak() {


System.out.println("Woof");
}
}
Cat Class
public class Cat {
private String name;
private int hairballs;

public Cat(String n, int h) {


name = n;
hairballs = h;
}

public String getName() { return name; }

public int getHairballs() { return hairballs; }

public void speak() {


System.out.println("Meow");
}
}
Problem: Code Duplication

• Dog and Cat have the name field and


the getName method in common

• Classes often have a lot of state and


behavior in common

• Result: lots of duplicate code!


Solution: Inheritance
• Inheritance allows you to write new classes
that inherit from existing classes

• The existing class whose properties are


inherited is called the "parent" or superclass

• The new class that inherits from the super


class is called the "child" or subclass

• Result: Lots of code reuse!


Dog Cat
String name String name
int fleas int hairballs
String getName() String getName()
int getFleas() int getHairballs()
void speak() void speak()

using
inheritance

superclass
Animal
subclass
String name
subclass String getName()

Dog Cat
int fleas int hairballs
int getFleas() int getHairballs()
void speak() void speak()
Animal Superclass
public class Animal {

private String name;

public Animal(String n) {
name = n;
}

public String getName() {


return name;
}
}
Dog Subclass
public class Dog extends Animal {

private int fleas;

public Dog(String n, int f) {


super(n); // calls Animal constructor
fleas = f;
}

public int getFleas() {


return fleas;
}

public void speak() {


return System.out.println("Woof");
}
}
Cat Subclass
public class Cat extends Animal {

private int hairballs;

public Cat(String n, int h) {


super(n); // calls Animal constructor
hairballs = h;
}

public int getHairballs() {


return hairballs;
}

public void speak() {


return System.out.println("Meow");
}
}
Inheritance Quiz 1
• What is the output of the following?
Dog d = new Dog("Rover" 3);
Cat c = new Cat("Kitty", 2);
System.out.println(d.getName() + " has " +
d.getFleas() + " fleas");
System.out.println(c.getName() + " has " +
c.getHairballs() + " hairballs");

Rover has 3 fleas


Kitty has 2 hairballs

(Dog and Cat inherit the getName method from Animal)


Inheritance Rules

• Constructors are not members and are not inherited


by subclass.
• Subclass don’t inherit private members.
• Subclass don’t inherit members if the subclass has
members with the same name as superclass.
• Use the super keyword in the subclass constructor
to call the superclass constructor
• Subclass inherit members declared with no access
specifiers in the same package
• Subclass inherit public or protected members of a
superclass.
Subclass Constructor
• The first thing a subclass constructor must do
is call the superclass constructor

• This ensures that the superclass part of the


object is constructed before the subclass part

• If you do not call the superclass constructor


with the super keyword, and the superclass
has a constructor with no arguments, then that
superclass constructor will be called implicitly.
Implicit Super Constructor Call
then this Beef subclass:

public class Beef extends Food {


If I have this Food class: private double weight;
public Beef(double w) {
public class Food { weight = w
private boolean raw; }
public Food() { }
raw = true;
} is equivalent to:
}
public class Beef extends Food {
private double weight;
public Beef(double w) {
super();
weight = w
}
}
Inheritance Quiz 2
public class A {
public A() { System.out.println("I'm A"); }
}

public class B extends A {


public B() { System.out.println("I'm B"); }
}

public class C extends B {


public C() { System.out.println("I'm C"); }
}

What does this print out? I'm A


I'm B
C x = new C(); I'm C
Overriding Methods
• Subclasses can override methods in their superclass
class Therm { class ThermUS extends Therm {
public double celsius;
public ThermUS(double c) {
public Therm(double c) { super(c);
celsius = c; }
}
// degrees in Fahrenheit
public double getTemp() { public double getTemp() {
return celcius; return celsius * 1.8 + 32;
} }
} }

• What is the output of the following? 212


ThermUS thermometer = new ThermUS(100);
System.out.println(thermometer.getTemp());
Calling Superclass Methods
• When you override a method, you can call
the superclass's of the method by using the
syntax super.method()

class Therm { class ThermUS extends Therm {


private double celsius;
public ThermUS(double c) {
public Therm(double c) { super(c);
celcius = c; }
}
public double getTemp() {
public double getTemp() { return super.getTemp()
return celcius; * 1.8 + 32;
} }
} }
Access Level
• Classes can contain fields and methods
of four different access levels:

• private: access only to the class itself

• package: access only to classes in the


same package

• protected: access to classes in the


same package and to all subclasses

• public: access to all classes everywhere


Variable Type vs Object Type
• Variables have the types they are given when
they are declared and objects have the type of
their class.

• For an object to be assigned to a variable is


must be of the same class or a subclass of the
type of the variable.

• You may not call a method on a variable if it's


type does not have that method, even if the
object it references has the method.
Which Lines Don't Compile?
public static void main(String[] args) {
Animal a1 = new Animal();
a1.getName();
a1.getFleas(); // Animal does not have getFleas
a1.getHairballs(); // Animal does not have getHairballs
a1.speak(); // Animal does not have speak
Animal a2 = new Dog();
a2.getName();
a2.getFleas(); // Animal does not have getFleas
a2.getHairballs(); // Animal does not have getHairballs
a2.speak(); // Animal does not have speak
Dog d = new Dog();
d.getName();
d.getFleas();
d.getHairballs(); // Dog does not have getHairballs
d.speak();
}
Polymorphism
• Polymorphism is the ability of an object to take on
different forms.
• You can perform Polymorphism in Java via two
different methods:
1.Method Overloading
Method overloading is the process that can create multiple methods
of the same name in the same class, and all the methods work in
different ways.
1.Method Overriding
Method overriding is the process when the subclass or a child class
has the same method as declared in the parent class.
Polymorphism
class Shapes {
public void area(int r)
{
System.out.println("Circle area = "+3.14*r*r);
}
public void area(double b, double h)
{
System.out.println("Triangle area="+0.5*b*h);
}
public void area(int l, int b)
{
System.out.println("Rectangle area="+l*b);
}
}
Questions
1. The most restrictive access modifier is ___
2. A java file can have only one public class T/F
3. Which access modifier gives access to a class in
the same package and to all subclass?
4. How to achieve encapsulation in Java?
5. What type of inheritance does Java have?
6. The process of wrapping up of data and function in
a single unit is called ________
7. The variable that is declared within the class but
outside the member method is called as
7. What is the difference between Abstraction and
Encapsulation?

You might also like