[go: up one dir, main page]

0% found this document useful (0 votes)
4 views44 pages

Exception Handling

Uploaded by

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

Exception Handling

Uploaded by

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

OOP - JAVA

Object-Oriented Programming
Java
The this
Reference
public classMyDate{ private
int day = 26; private
int month = 9; private
int year = 2016;
public MyDate( int day, int
month, int year){
this.day

= day; this.month
= month; this.year

= year;
}
public MyDate( MyDate date){
this.day =
date.day; this.month =
date.month; this.year
= date.year;
}
public MyDate creteNextDate(int moreDays){
MyDate newDate = new MyDate(this);
//... add moreDays
return newDate;
}
}
Java Coding Conventions
● Packages
○ ro.sapientia.ms
● Classes
○ SavingsAccount
● Methods
○ getAmount()
● Variables
○ amount
● Constants
○ NUM_CLIENTS
The final
Keyword
● Class
○ You cannot subclass a final class.
● Method
○ You cannot override a final
method.
● Variable
○ A final variable is a constant.
○ You can set a final variable only
once.
○ Assignment can occur
independently of the declaration
Relationships between classes
Object-oriented programming
Relationships between classes

● Association
(containment)
– Strong –
Composition

– Weak – Aggregation
Relationships between classes
Composition

– Strong type of
association
– Full ownership
Relationships between classes
Aggregation

– Weak type of
association
– Partial ownership
Relationships between classes
Association – Aggregation - Composition

Associatio
n

Aggregati
on

Compositio
n
Relationships between classes
Implementing Associations (1)

public class Brain{ public class


//... Person{ private
Brain brain;
} //...
}
Relationships between classes
Implementing Associations (2)

public class public class


Date{ private int Person{ private String
day; private int name; private Date
month; private birthDate;
int year;
//... public Person(String name,
} Date birthDate){
this.name = name;
this.birthDate = birthDate; }
//...
}
Relationships between classes
Implementing Associations
(3)
Benedek Istvan, 1990, 1, 12
Burjan Maria, 1968, 4, 15
Dobos Hajnalka Evelin, 1986, 3,
17
...

Write a program which


:Person :Person reads the data of
several persons and
constructs an array of
:Date :Date Persons.
Relationships between classes
Relationship cardinality

– One-to-one

– One-to-many
Relationships between classes
Implementing one-to-many relationship (1)
public class
Student{ private final
long ID; private String
firstname; private
String lastname;
//...
}

public class Course{


private final long ID;
private String name;
public static final int MAX_STUDENTS=100;
private Student[] enrolledStudents;
private int numStudents;

//...
}
Relationships between classes
Implementing one-to-many relationship (2)
public class Course{
private final long ID;
private String name;
public static final int MAX_STUDENTS = 100;
private Student[] enrolledStudents;
private int numStudents;

public Course( long ID, String name ){


this.ID = ID;
this.name = name;
enrolledStudents = new
Student[ MAX_STUDENTS ];
}

public void enrollStudent( Student student


){
enrolledStudents[ numStudents ] =
student;
++numStudents;
}
//...
}
Relationships between classes
Implementing one-to-many relationship (3)
public class Course{
private final long ID;
private String name;

private ArrayList<Student> enrolledStudents;

public Course( long ID, String name ){


this.ID = ID;
this.name = name;
enrolledStudents =
new
ArrayList<Student
>();
}

public void
enrollStudent( Studen
t student ){
enrolledStudents.add(
student);
}

//...
}
Inheritance, Polymorphism
Outline

● Inheritance
○ Parent class
○ Subclass, Child class
● Polymorphism
○ Overriding methods
○ Overloading methods
○ The instanceof
operator
○ Heterogeneous
collections
Problem: repetition in implementations

public class Employee{


private String name;
private double salary;
private Date birthDate;

public String toString(){


//...
}
}

public class
Manager{ private String
name; private double
salary; private Date
birthDate; private String
department;

public String toString(){


//...
}
}
Solution: inheritance

public class Employee{


protected String name;
protected double salary;
protected Date birthDate;
public Employee( … ){
// …
}
public String toString(){
//...
}
}

public class Manager extends


Employee{ private String
department;

public Manager( … ){
// …
}
public String toString(){
// …
}
}
Inheritance - syntax

<modifier> class <name> extends <superclass>{


<declaration*>
}

public class Manager extends Employee{

}
The subclass

● Inherits the data and methods of the


parent class
● Does not inherit the constructors of
the
parent class
● Opportunities:
1) add new data
2) add new methods
3) override inherited methods
(polymorphism)
The subclass

● Opportunities:
1) add new data → department
2) add new methods → e.g.
getDepartment()
3) override inherited methods →
toString()
Invoking Parent Class Constructors
public class Employee{
protected String name;
protected double salary;
protected Date birthDate;
public Employee( String name, double salary, Date birthDate){
this.name = name;
this.salary = salary;
this.birthDate = birthDate;
}
//...
}

public class Manager extends


Employee{ private String
department;
public Manager( String name, double
salary, Date birthDate,
S
t
r
i
n
g

d
e
p
a
r
t
Access Control

Modifier Same Same Subclass Univers


Class Package e

private Yes
default! Yes
Yes
protected Yes Yes Yes
public Yes Yes Yes Yes
Polymorphism - Overriding Methods

● A subclass can modify the behavior


inherited from a parent class
● A subclass can create a method
with different functionality than
the parent's method but with
the:
– same name
– same argument list
– almost the same return type
(can be a subclass of the
overriden return type)
Overriding Methods
public class Employee{
protected String name;
protected double salary;
protected Date birthDate;
public Employee( … ){
// …
}
public String toString(){
return “Name: “+name+”
Salary: ”+salary+”
B. Date:”+birthDate;
}
}
public class Manager extends
Employee{ private String
department;
public Manager( … ){
// …
}
@Override
public String toString(){
return “Name: “+name+” Salary:
”+salary+” B.
Date:”+birthDate
+” department:
”+department;
}
}
Invoking Overridden Methods
public class Employee{
protected String name;
protected double salary;
protected Date birthDate;
public Employee( … ){
// …
}
public String toString(){
return “Name: “+name+”
Salary: ”+salary+”
B. Date:”+birthDate;
}
}
public class Manager extends
Employee{ private String
department;
public Manager( … ){
// …
}
public String toString(){
return super.toString() + ”
department: ”+department;
}
}
Overridden Methods Cannot Be Less
Accessible

public class Parent{


public void foo()
{}
}

public class Child extends Parent{


private void foo(){} //illegal
}
Heterogeneous Arrays

Employee emps[] = new Employee[ 100 ];


emps[ 0 ] = new Employee();
emps[ 1 ] = new Manager();
emps[ 2 ] = new Employee();
// …

// print employees
for( Employee e: emps )
{ System.out.println( e.toString(
) );
}

// count managers
int counter = 0;
for( Employee e: emps ){
if( e instanceof Manager ){
++counter;
}
}
Static vs. Dynamic type of a reference

// static (compile time) type is:


Employee
Employee e;

// dynamic (run time) type is:


Employee
e = new Employee();

// dynamic (run time) type is:


Manager
e = new Manager();
Static vs. Dynamic type of a reference

Employee e = new Manager(“Johann”,3000,


new Date(10,9,1980),”sales”);
System.out.println( e.getDepartment() );// ERROR

//Solution
System.out.println( ((Manager
) e).getDepartment() );// CORRECT

//Better Solution
if( e instanceof Manager ){
System.out.println( ((Ma
nager) e).getDepartment() );
}
The instanceof
Operator

Animal a = new Bear();

//expressions
a instanceof Animal → true
a instanceof Mammal → true
a instanceof Bear → true
a instanceof Date → false
Polymorphism
Overloading Methods

● Polymorphism: the ability to have many different forms


● Methods overloading:
– methods having the same name,
– argument list must differ,
– return types can be different.
● Example:
public void println(int i)
public void println(float f)
public void println(String s)
Polymorphism
Overloading Constructors
public class Employee{
protected String name;
protected double salary;
protected Date birthDate;
public Employee( String name, double salary, Date birthDate){
this.name = name;
this.salary = salary;
this.birthDate = birthDate;
}
public Employee( String name, double
salary){
this(name, salary, null);
}
public Employee( String name, Date
birthDate){
this(name, 1000, birthDate);
}
//...
}
Polymorphism

The ability to have many different forms


● Methods overloading
○ same name, different signature
○ e.g. a class having multiple constructor
○ compile-time polymorphism (static
polymorphism)
● Methods overriding
○ same name, same signature
○ e.g. toString()
○ run-time polymorphism (dynamic
polymorphism)
Remember

● Inheritance
– Subclass
opportunities
● Polymorphism
– Overriding methods
– Overloading methods
– Polymorphic argument
– Heterogeneous
collections
– Static vs. dynamic type
– The instanceof
Abstraction

Abstraction is achieved in 2
ways :

•Abstract class

•Interfaces (Pure Abstraction)


Abstract Class

•An abstract class must be declared with an


abstract keyword.
•It can have abstract and non-abstract
methods.
•It cannot be instantiated.
•It can have constructors and static methods
also.
•It can have final methods which will force the
subclass not to change the body of the
method.
Abstraction

abstract class Animal{


Animal(){
System.out.println("Constructor of the Animal Class");
}
abstract void walk();
void run(){
System.out.println("Animal Run");

}
final void eat(){
System.out.println("All Animal Eat");
}
}
Interfaces

•All the fields in interfaces are public,


static and final by default.
•All methods are public & abstract by
default.
•A class that implements an interface
must implement all the methods
declared in the interface.
•Interfaces support the functionality of
Interface Example

interface Animal{
void run();
// void eat();
}

class Horse implements Animal{


public void run(){
System.out.println("The horse run");
}
}
Static keyword

Static can be :
1.Variable (also known as a class
variable)
2.Method (also known as a class
method)

class School{
public static String name;

// Access the static property as


OOP example

In this detailed example, we have concrete


classes for real animals like Cow, Lion, Parrot,
and Elephant. Each class implements the
Animal interface, providing implementations for
the eat(), sleep(), and makeSound() methods.
These methods define the common behaviors
of all animals. The Mammal and Bird abstract
classes provide common properties and
behaviors for mammals and birds,
respectively. The main class Zoo demonstrates
the usage of these classes by creating
instances of different animals and invoking

You might also like