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

CSS.103 Lecture 8

The document provides an overview of Object Oriented Programming concepts in Java, including comparisons with C++, common format specifiers, method signatures, polymorphism, and the distinctions between abstract classes and interfaces. It explains key concepts such as inheritance (IS-A relationship) and composition (HAS-A relationship), along with the rules for defining and implementing interfaces. The document serves as a foundational guide for understanding Java programming principles relevant to the Spring 2024 course.

Uploaded by

halaezat08
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)
4 views

CSS.103 Lecture 8

The document provides an overview of Object Oriented Programming concepts in Java, including comparisons with C++, common format specifiers, method signatures, polymorphism, and the distinctions between abstract classes and interfaces. It explains key concepts such as inheritance (IS-A relationship) and composition (HAS-A relationship), along with the rules for defining and implementing interfaces. The document serves as a foundational guide for understanding Java programming principles relevant to the Spring 2024 course.

Uploaded by

halaezat08
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
You are on page 1/ 51

Faculty of Information Technology

Spring 2024

Object Oriented Programming


CCS103 + CS-201
Lec (8)

1
JAVA v.s. C++
Programming Language Comparison

2
Common format specifiers
•%s – String
•%d – Integer
•%f – Floating point number
•%t – Date/Time
•%x – Hexadecimal
Examples:
public class Example{
public static void main(String args[]){
String name = “Fatma";
String message = String.format("Hello, %s!", name);
System.out.println(message);
// Output: Hello, Fatma! 3
public class Example{
public static void main(String args[]){
String name = " Fatma ";
int age = 18;
double luckyNum = 123.456;
String formattedString = String.format("Name:
%s, Age: %d, Lucky No: $%.2f", name, age,
luckyNum);
System.out.println(formattedString);
// Output: Name: Fatma, Age: 18, Lucky No:
$123.456
}
}

4
Signatures
• In any programming language, a signature is what
distinguishes one function or method from another
• In Java, two methods have to differ in their names
or in the number or types of their parameters
– foo(int i) and foo(int i, int j) are different
– foo(int i) and foo(int k) are the same
– foo(int i, double d) and foo(double d, int i) are
different

5
◼ Polymorphism means many (poly) shapes (morph)
◼ In Java, polymorphism refers to the fact that you can
have multiple methods with the same name in the same
class
◼ There are two kinds of polymorphism:
◼ Overloading
◼ Two or more methods with different signatures
◼ Overriding
◼ Replacing an inherited method with another having the same signature

6
Overloading
class Test {
public static void main(String args[]) {
myPrint(5);
myPrint(5.0);
}
static void myPrint(int i) {
System.out.println("int i = " + i);
}
static void myPrint(double d) {
System.out.println("double d = " + d);
}
}

int i = 5
double d = 5.0

7
Overriding
class Animal {
public static void main(String args[]) {
Animal animal = new Animal();
Dog dog = new Dog();
animal.print();
dog.print();
}
void print() {
System.out.println("Superclass Animal");
}
}
public class Dog extends Animal {
void print() {
System.out.println("Subclass Dog");
}
}
Superclass Animal
Subclass Dog
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Java

Abstraction

Is-A and Has-A Relationship

Interface
23
Java Abstraction

Abstract Class
Abstract Methods

24
Abstract Class
• Abstract classes may or may not contain abstract methods

• But, if a class has at least one abstract method, then the


class must be declared abstract.

• If a class is declared abstract, it cannot be instantiated.

• To use an abstract class, you have to inherit it from another


class, provide implementations to the abstract methods in it.

• If you inherit an abstract class, you have to provide


implementations to all the abstract methods in it 25
Abstract Methods
• abstract keyword is used to declare the method as abstract.

• You have to place the abstract keyword before the method name
in the method declaration.

• An abstract method contains a method signature, but no method


body.

• Instead of curly braces, an abstract method will have a semoi


colon (;) at the end.

26
27
28
29
Java

Is-A and Has-A Relationship

30
Is-A and Has-A Relationship

One of the advantages of an OOP language


is code reuse.
There are two ways we can do code reuse
either by the implementation of inheritance
(IS-A relationship), or object composition
(HAS-A relationship).

31
Is-A Relationship

The concept of IS-A is a totally based on Inheritance, which can be of


two types Class Inheritance or Interface Inheritance.

It is just like saying "A is a B type of thing". For example, Apple is a


Fruit, Car is a Vehicle etc.

Inheritance is uni-directional.
For example, House is a Building. But Building is not a House.

Wherever you see an extends keyword or implements keyword in a


class declaration, then this class is said to have IS-A relationship.

32
Has-A Relationship

Composition(HAS-A) simply mean the use of


instance variables that are references to other objects.

For example Maruti has Engine, or House has


Bathroom.

Let’s understand these concepts with an example of


Car class.

33
34
35
36
37
Java

Interface

38
Interface
Interfaces are just a definition of behavior.

Java Interface is not a class but it is defined in away


similar to the class definition.

An Interface describes the functions and behaviors of


a specific object type but doesn't implement them.

When a class implements an interface it has to


implement all methods declared in that interface.
39
hints about Interface

Interface functions should be public and abstract.

Interface fields should be public and final.

Use the Keyword interface to define an interface.

If you define a public interface with name


myInterface the java file should be named
as myInterface.java (Similar to public class).
40
A class implementing an interface should use the
keyword implements.

No objects can be created from an interface.

Interfaces don't have constructors as they can't be


initiated
An Interface can extends one or more interfaces.

You can define a reference of type interface but you


should assign to it an object instance of class type
which implements that interface. 41
Interface

One Interface - One Class

42
43
Interface

Interface Inherits An Interface

44
45
Interface

Many interfaces - One class

46
47
48
Interface

Interface and Abstract Class

49
50
END

51

You might also like