CS202 - Chapter 1 - Java Bascis - Abstract Classes - Interfaces
CS202 - Chapter 1 - Java Bascis - Abstract Classes - Interfaces
CHAPTER 1
JAVA BASICS REVIEW
ABSTRACT CLASSES
INTERFACES
SOPHOMORE - SE
SPRING 2023
CS202 - ADVANCED OO PROGRAMMING 1
Outline
AGENDA
2
1. Visibility & Static Modifiers
2. Non-access modifier
▪ static
CS311
CS202
- ADVANCED
- ADVANCED
PROGRAMMING
OO 3
PROGRAMMING
Visibility Types (1)
There are two types of modifiers in Java: access modifiers and non-access
modifiers.
We can change the access level of fields, constructors, methods, and class
by applying the access modifier on it.
➢ Protected: The access level of a protected modifier is within the package and
outside the package through child class. If you do not make the child class, it cannot
be accessed from outside the package.
➢ Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
➢ Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
The public access modifier is accessible everywhere. It has the widest scope among all other
modifiers.
Attribute Reading/writing or modification
Method Calling method
Class Instantiation objects of this class and access to its
attributes/methods
//save by B.java
package mypack;
//save by A.java import pack.*;
package pack; class B extends A {
public class A { public static void main(String args[]){
protected int att; B obj = new B();
protected void msg() obj.att = 2;
{ System.out.println("Hello");} obj.msg(); }
} }
//save by B.java
package mypack;
import pack.*;
class B {
public static void main(String args[])
//save by A.java { A obj = new A(); //Compile Time Error
package pack; obj.msg(); //Compile Time Error
class A { }
void msg() } /* The scope of class A and its method msg() is default so it
{ System.out.println("Hello");} cannot be accessed from outside the package.*/
}
attributes: the value of a static attribute is not realtive to the object but to the class (same
value of this attributes for all objects instantiating this class)
We can access directly by using the class name instead of using object name or by calling any object
instantiating this clase
Method: its code does not depend on an object et may be executed if there is no object
instantiating this class (example main)
Nested Classes
Every code used in/with static members can not reference the current instance or object (this)
04/11/19 ADVANCED
CS202 - PROGRAMMING
ADVANCED OO 13
PROGRAMMING
Final modifier
The keyword final may be applied to :
➢ Class variables
➢ Instance variables
➢ Local variables
➢ Methods
➢ Method’s parameters
➢ Classes
public MyClass () {
public MyClasse() {
this.const = 10;}
} //ERROR
A final class
• Cannot be modified,
• Cannot be inherited (no sub-classes to be defined).
➢ A concrete classe inheriting from one or multiple abstract classes (indirectly), should implement all
the existing abstract methods
➢ Abstract classes will be used throw commun methods having the same name.
Like a class, an interface can have methods and variables, but the
methods declared in interface are by default abstract (only method
signature, no body).
▪▪ Interfaces specify what a class must do and not how. It is the blueprint of the class.
➔If a class implements an interface and does not provide method bodies for all
functions specified in the interface, then class must be declared abstract.
▪▪ Since java does not support multiple inheritance in case of class, but by using
interface it can achieve multiple inheritance .
▪▪ Interfaces are used to implement abstraction. So the question arises why use
interfaces when we have abstract classes? The reason is, abstract classes may
contain non-final variables, whereas variables in interface are final, public and
static.
A concrete class should provide an implementation for all the methods declared in the
interfaces in question as well those are declared in super-classes (abstract).
CS202 - ADVANCED OO 25
PROGRAMMING
CASTING & ➢ Casting
➔ B is a particular/specific case of A
➔ the objects instantiating the class B, are implicitely instantiating the class A
➔ The inheritence consists of defining a class, called sub-class, class child or derived class
from another one called mother class, super class or base class.
➔ the sub-class retreive automatically all members and behaviors from the the super-class and
may eventually define other specific ones.
moveTo(int,int)
public void moveTo(int newX, int newY) {
this.x = newX;
getBlue():byte
public byte getRed() {return rgb[0];}
public byte getGreen() {return rgb[1];}
public byte getBlue() {return rgb[2];}
}
Each object instantiating a derived class combines the derived attributes from the base class
with those defined in its own class
➢➢We have int x and int y in objects of the class ColoredPixel. We have also 3 colors (green, red, blue) for
each object of ColoredPixel
public static void main(String[] args) {
Pixel p = new Pixel();
p.moveTo(1, 1);
cp.getRed(); // NullPointerException
}
➢➢Each constructor begins by calling the constructor of the super -class: super()
➢➢the default constructor (added by the compiler) call the default constructor of the super-class
public ColoredPixel() {
public Pixel(int newX, int newY) {
this.x = newX; // not allowed
this.x = newX;
this.y = newY; // not allowed
this.y = newY; }
// super(); // Constructor Pixel() is undefined
// ...
} super(0,0); // OK; note that x and y are private!
rgb = new byte[3]; }
}
CS202 - ADVANCED OO 30
3
PROGRAMMING 0
Example
public class Employee{
protected String name ;
protected double salary ;
public Employee ( String n, double s){
this.name = n;
this.salary = s ;}
public String getName(){ return Name ;}
public double getSalary() { return salary ;}
public String toString() { return getName()+” ”+getSalary(); }
}
double sumSalaries = 0;
for ( int i = 0 ; i < 5 ; ++i)
if(!(enterprise[i] instanceof Manager))
sumSalaries += enterprise[i].getSalary();
Questions
- Write all the needed classes in Java.
- Each class should contain a constructor for initializing the objects to be created.
- Each class should redefine the method toString().
-Write a principal program in separate class to create two wtudents, two professors and two employees.
The program should display all the informations about the created objects.
CS202 - ADVANCED OO 33
PROGRAMMING
Class Person
@Override
public String toString() {
return ”I am " + this.LName.toUpperCase() + " ”
+ this.FName.substring(0, 1).toUpperCase() + ””
+ this.FName.substring(1).toLowerCase();
}
}
CS202 - ADVANCED OO PROGRAMMING 34
Class Student
@Override
public String toString() {
return super.toString() + " my registration number is : " + this. RegisID;
}
@Override
public String toString() {
return super.toString() + " my salary is: " + this.Salairy + " D";
}
@Override
public String toString() {
return super.toString() + " my speciality is : " + this.speciality;
}
➢➢ It is rarely useful, but generally a bad idea since it presents a source of error
➔ Solution : casting using (cast) allow to change or convert the type of the
decalred reference (or object)
CS202 - ADVANCED OO 39
PROGRAMMING
class A {
int x = 1; }
class B extends A {
String x = "zz"; }
class C extends B {
boolean x = true;
CS202 - ADVANCED OO 40
4
PROGRAMMING 0
Question
How can we modify the first or last name of a professor?
Instanceof operator
It is possible to perform the conversion/casting correctly without exceptions
thanks to the operator instanceof ➔ x instance of T
The result is true when x is not null and can be assigned to a variable of type T without
ClassCastException; otherwise, it is false.
class A { }
class B extends A { } A ab = null;
System.out.println(ab instanceof A); // false
class C extends B { }
ab = new B();
System.out.println(ab instanceof A); // true
System.out.println(ab instanceof B); // true
System.out.println(ab instanceof C); // false
CS202 - ADVANCED OO 42
PROGRAMMING
Overriding
POLYMORPHISM
Overloading
public Pixel(int newX, int newY) { public class ColoredPixel extends Pixel {
this.x = newX; private byte[] rgb;
this.y = newY; }
CS202 - ADVANCED OO 44
PROGRAMMING
Method overloading
If the method signature is different from the one defined in the super class, it is called
overloading :
Both methods have the same name but have different parameters Generally,
both are defined in the same child class
class Pixel {
void m1() { ... }
void m2() { ... }
Pixel m3() { ... }
void m4(Pixel p) { ... }
}
class ColoredPixel extends Pixel {
void m1() { ... } // overriding
void m2(int a) { ... } // overloading
ColoredPixel m3() { ... } // overriding
void m4(Pixel p) { ... } // overriding
void m4(ColoredPixel p) { ... } // overloading
}
CS202 - ADVANCED OO 45
PROGRAMMING
Example – classes definition
public class Employee{
protected String name ;
protected double salary ;
public Employee ( String n, double s){
this.nom = n ;
this.salary = s ;}
public String getName(){ return name ;}
public double getSalary() { return salary ;}
public String toString() { return getName()+” ”+getSalary(); }
}
CS202 - ADVANCED OO 46
PROGRAMMING
Example – principal program
Employee enterprise[] = new Employee[5] ;
enterprise[0]= new Chief("Cronos", 1000, 500) ;
enterprise[1]= new Chief("Zeus ", 1000, 600) ;
enterprise[2]= new Employee("Ares", 620) ;
enterprise[3]= new Employee ("Apollon", 700) ;
enterprise[4]= new Employee ("Aphrodite ", 100) ;
double sumSalaries = 0;
for ( int i = 0 ; I < 5 ; ++i)
if(!(enterprise[i] instanceof Chief))
sumSalaries += enterprise[i].getSalary();
CS202 - ADVANCED OO 47
PROGRAMMING