Professional Documents
Culture Documents
Advanced Programming With Java and Uml: by Dr. Salamudeen Alhassan
Advanced Programming With Java and Uml: by Dr. Salamudeen Alhassan
AND UML
CSC 417
By
Dr. Salamudeen Alhassan
Inheritance
Polymorphism
Abstraction
Encapsulation
Interfaces
Packages
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 2 / 12
Inheritance
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 3 / 12
Listing: Inheritance Example
class Calculator {
int z ;
public void addition ( int x , int y ) {
z = x + y;
System . out . println ( x " + " + y + " = " + z ) ;}
public void Subtraction ( int x , int y ) {
z = x - y;
System . out . println ( x " - " + y + " = " + z ) ;}
}
public class N ew_Calcul ator extends Calculator {
public void mul tiplicat ion ( int x , int y ) {
z = x * y;
System . out . println ( x " * " + y + " = " + z ) ;
}
public static void main ( String args []) {
int a = 20 , b = 10;
New_C alculato r myCalculator = new New _Calcula tor ()
;
myCalculator . addition (a , b ) ;
myCalculator . Subtraction (a , b ) ;
myCalculator . multipl ication (a , b ) ;
}
}
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 4 / 12
Inheritance
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 4 / 12
Inheritance
A superclass method can be overridden provided that it is not
marked final.
Method overriding helps to define a behavior that’s specific to a
subclass type.
A subclass can implement a parent class method based on its
requirement.
Rules for method overriding
The argument list should be exactly the same as that of the overridden method.
The return type should be the same or a subtype of the return type declared in the
original overridden method in the superclass.
The access level cannot be more restrictive than the overridden method’s access
level.
Instance methods can be overridden only if they are inherited by the subclass.
A method declared final cannot be overridden.
A method declared static cannot be overridden but can be re-declared.
If a method cannot be inherited, then it cannot be overridden.
Constructors cannot be overridden
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 5 / 12
Listing: Overriding Example
class Animal {
public void move () {
System . out . println (" Animals can move ") ;
}
}
class Dog extends Animal {
public void move () {
System . out . println (" Dogs can walk and run ") ;
}
public void bark () {
System . out . println (" Dogs can bark ") ;
}
}
public class TestDog {
public static void main ( String args []) {
Animal a = new Animal () ; // Animal reference and object
Animal b = new Dog () ; // Animal reference but Dog object
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 6 / 12
Polymorphism
Polymorphism means "many forms"
It is the ability of an object to take on many forms
Polymorphism uses methods obtained from inheritance to
perform different tasks.
This allows us to perform a single action in different ways.
Polymorphism is useful for code reusability.
Example
public interface Vegetarian{}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 6 / 12
Listing: Polymorphism Example
class Animal {
public void animalSound () {
System . out . println (" The animal makes a sound ") ;}
}
class Pig extends Animal {
public void animalSound () {
System . out . println (" The pig says : wee wee ") ;}
}
class Dog extends Animal {
public void animalSound () {
System . out . println (" The dog says : bow wow ") ;}
}
class My_Animals {
public static void main ( String [] args ) {
Animal myAnimal = new Animal () ; // Create a Animal object
Animal myPig = new Pig () ; // Create a Pig object
Animal myDog = new Dog () ; // Create a Dog object
myAnimal . animalSound () ;
myPig . animalSound () ;
myDog . animalSound () ;
}
}
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 7 / 12
Abstraction
Data abstraction is the process of hiding certain details and
showing only essential information to the user.
Abstraction in Java can be achieved with either abstract classes
or interfaces.
The abstract keyword is a non-access modifier, used for classes
and methods.
An abstract class is a restricted class that cannot be used to
create objects (to access it, it must be inherited from another
class).
The abstract method can only be used in an abstract class,
and it does not have a body. The body is provided by the
subclass (inherited from).
In order to achieve security, hide certain details and only show
the important details of an object.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 7 / 12
Listing: Abstraction Example
// Abstract class
abstract class Animal {
// Abstract method ( does not have a body )
public abstract void animalSound () ;
// Regular method
public void sleep () {
System . out . println (" Zziiii ") ;
}
}
// Subclass ( inherit from Animal )
class Pig extends Animal {
public void animalSound () {
// The body of animalSound () is provided here
System . out . println (" The pig says : wee wee ") ;
}
}
class Main {
public static void main ( String [] args ) {
Pig myPig = new Pig () ; // Create a Pig object
myPig . animalSound () ;
myPig . sleep () ;
}
}
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 8 / 12
Encapsulation
Thing to Note
Interfaces cannot be used to create objects.
Interface methods do not have a body.
On implementation of an interface, all of its methods must be overridden.
Interface methods are by default abstract and public.
Interface attributes are by default public, static and final. item An interface
cannot contain a constructor.
Why Interfaces?
To achieve security.
Java does not support "multiple inheritance" but a class can implement multiple
interfaces separated by commas.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 9 / 12
Listing: Interface Example
interface Animal {
// interface method ( does not have a body )
public void animalSound () ;
// interface method ( does not have a body )
public void sleep () ;
}
// Pig " implements " the Animal interface
class Pig implements Animal {
public void animalSound () {
// The body of animalSound () is provided here
System . out . println (" The pig says : wee wee ") ;
}
public void sleep () {
// The body of sleep () is provided here
System . out . println (" Zziii ") ;
}
}
class Main {
public static void main ( String [] args ) {
Pig myPig = new Pig () ; // Create a Pig object
myPig . animalSound () ;
myPig . sleep () ;
}
}
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 10 / 12
Packages
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 10 / 12
Packages cont.
Built-in Packages
The Java API is a library of prewritten classes which are free to
use
They are included in the Java Development Environment.
The library is divided into packages and classes.
Use the import keyword to use a class or a package.
Syntax
import package.name.Class; // Import a single class
import package.name.*; // Import the whole package
Example
import java.util.Scanner; // Import the Scanner class
import java.util.*; // Import the entire java.util package
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 11 / 12
Packages cont.
User-defined Packages
Use the package keyword to create a package
Syntax
package packagename;
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 12 / 12
Listing: Packages Example
package v o l u m e c a l c u l at o r ;
import java . util . Scanner ;
public class Cylinder {
public static void main ( String [] args ) {
// create an input object from scanner
Scanner input = new Scanner ( System . in ) ;
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 12 / 12