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

Object Oriented Programming in Java Chapter 2

The document provides an introduction to encapsulation and access modifiers in Java, explaining public and private properties, methods, and static methods. It also covers inheritance, detailing how subclasses can inherit properties and methods from a base class, and introduces abstract classes and methods. The content is aimed at helping readers understand object-oriented programming concepts in Java through practical examples.

Uploaded by

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

Object Oriented Programming in Java Chapter 2

The document provides an introduction to encapsulation and access modifiers in Java, explaining public and private properties, methods, and static methods. It also covers inheritance, detailing how subclasses can inherit properties and methods from a base class, and introduces abstract classes and methods. The content is aimed at helping readers understand object-oriented programming concepts in Java through practical examples.

Uploaded by

ooafonrinwo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Encapsulation &

access modifiers
I N T R O D U C T I O N T O O B J E C T- O R I E N T E D P R O G R A M M I N G I N J AVA

Sani Yusuf
Lead Software Engineering Content
Developer
Understanding encapsulation
Encapsulation can be explained with a
mobile device

You only have access to dedicated exposed


features like screen, speaker, etc

The inner workings are entirely hidden from


users

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Public properties
Properties marked public can be
accessed by object instances public class Main {

// main method
class Car { public static void main(String[] args) {
// Public property Car myCar = new Car("brown");
public String color; // Can access public members
// from object instance
// Public constructor [Link](
public Car(String color){ [Link]); // Brown
[Link] = color; }
} }
}

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Private properties
Properties marked private can be
accessed by object instances // Main class
public class Main {
Properties marked private can only be // main method
public static void main(String[] args) {
used within the class
Car myCar = new Car("brown");
// Calling private properties causes errors

// Car class [Link](

class Car { [Link]); // Java compilation error

public String color; }

// Private properties }

private String model;

public Car(String color){


[Link] = color;
}
}

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Public methods
// Car class
class Car {
public String color;
private String model;

public Car(String color){


[Link] = color;
}

// Public method
public String getModel(){
return [Link];
}
}

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Private methods
class Car {

// Private method, "calculateSpeed" can only be used within "Car" class


private calculateSpeed(){
// Trademarked formula code
}

public int getSpeed(){


// "calculateSpeed" can be used anywhere within "Car" class
return [Link]();
}
}

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Static methods
Properties marked static can be
accessed without an object instance // Main class
public class Main {
Typically used to house shared
code/libraries
public static void
main(String[] args) {
// We can use "getSquare"
// Formula class
// without an object instance
static class Formula {
[Link](
[Link](5)); // 25
// Method for calculating square
}
static int getSquare(int number) {
}
return number * number;
}
}

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Recap
Access modifiers: public , private , static
Access modifiers help secure applications

static enables the use of classes without creating instances

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Let's practice!
I N T R O D U C T I O N T O O B J E C T- O R I E N T E D P R O G R A M M I N G I N J AVA
Centralising code
with inheritance
I N T R O D U C T I O N T O O B J E C T- O R I E N T E D P R O G R A M M I N G I N J AVA

Sani Yusuf
Lead Software Engineering Content
Developer
Working with multiple classes
// Toyota class // Honda class
class Toyota { class Honda {
public String model; public String model;
public String color; public String color;
public String licensePlate; public String licensePlate;
} }

// Mercedes class Duplication is common when working with


class Mercedes { classes
public String model;
public String color;
public String licensePlate;
}

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


The power of inheritance
Inheritance solved the problem of duplicate // Toyota inheriting from Car class
code class Toyota extends Car {

All shared code can be in one place }

Classes can then inherit using the extends


// Honda inheriting from Car class
keyword
class Honda extends Car {

// Car class
class Car { }

// All public properties


// are inherited // Mercedes inheriting from Car class
public String model; class Mercedes extends Car {
public String color;
public String licensePlate; }
}

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Introducing base class
The Base class is the class that contains shared code
The Car class is an example of a base class

class Car {
public String model;
public String color;
public String licensePlate;
}

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Inheriting with subclasses
The subclass is the class that inherits the // Car Base Class
class Car {
base class public String model;
public String color;
The Toyota class is an example of a public String licensePlate;
}
subclass

A subclass inherits all public properties & // Toyota subclass

methods of the base class class Toyota extends Car {

// Toyota constructor
The super() method calls the constructor public Toyota(){

of the base class // super() calls


// baseclass constructor (Car)
super();
}
}

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Creating inheritance instances
Properties from the base class can be used
by object instances // Toyota subclass
class Toyota extends Car {
public Toyota(){
// Car class super();
class Car { }
public String color; }

// Car constructor // Main class


public Car(String color){ public class Main {
[Link] = color; public static void main(String[] args) {
} Toyota myToyota = new Toyota("Brown");
} // "color" property inherited from "Car"
[Link](
[Link]); // Brown
}
}

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Let's practice!
I N T R O D U C T I O N T O O B J E C T- O R I E N T E D P R O G R A M M I N G I N J AVA
Making the car class
abstract
I N T R O D U C T I O N T O O B J E C T- O R I E N T E D P R O G R A M M I N G I N J AVA

Sani Yusuf
Lead Software Engineering Content
Developer
Explaining abstract class
The phrase Car only exists as a placeholder
We still need to know more like what type
of car it is

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Food analogy for abstract class
You never ask for just food at a restaurant
You always specify what type of food you
want to eat
bread

pasta

rice

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Understanding abstract classes
Abstract classes exist to hold features for inheriting classes
Abstract classes cannot have object instances created

Abstract classes generally do not have code implementation

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Car abstract class

// Car abstract class


abstract class Car {
// abstract drive method
abstract void drive(); // No Code Implementation

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Abstract methods
Abstract method have no implementation
// Toyota class
The class that inherits, must implement
class Toyota extends Car {
abstract methods void drive(){
// Toyota drive() implementation
}
// Car abstract class
}
abstract class Car {
// abstract drive method
// Porsche class
abstract void drive(); // No Code
class Porsche extends Car {
// implementation
void drive(){
}
// Porsche drive() implementation
}
}

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Creating concrete methods
Concrete methods are the methods with code implementation
Subclasses do not need to implement concrete methods

// abstract Car class


abstract class Car {
private int topSpeed;

// Concrete method with implementation


public getTopSpeed(){
return [Link];
}

abstract void drive();


// No Code Implementation
}

INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN JAVA


Let's practice!
I N T R O D U C T I O N T O O B J E C T- O R I E N T E D P R O G R A M M I N G I N J AVA

You might also like