Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

ADVANCED PROGRAMMING WITH JAVA

AND UML
CSC 417

By
Dr. Salamudeen Alhassan

March 16, 2022


Advance Java

Inheritance
Polymorphism
Abstraction
Encapsulation
Interfaces
Packages

ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 2 / 12
Inheritance

Inheritance is a process whereby one class acquires the


properties and behaviours of another class.
Subclass/derived/child class is the class which inherits from
another class.
Superclass/base/parent class is the class being inherited from.
To inherit from a class the extends keyword is used.
Inheritance helps manage information in a hierarchical order.
Inheritance is useful for code reusability

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

All public and protected members (fields, methods, and nested


classes) from the superclass can be inherited.
Constructors are not members, so they are not inherited by
subclasses, but the constructor of the superclass can be invoked
from the subclass.
The super keyword is similar to this keyword
It is used to differentiate the members of superclass from the
members of subclass, if they have same names.
It is used to invoke the superclass constructor from subclass.
Use the final keyword if you don’t want a class to be inherited.

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

a . move () ; // runs the method in Animal class


b . move () ; // runs the method in Dog class
b . bark () ;
}
}

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

Encapsulation means data hiding.


In encapsulation, the variables of a class will be hidden from
other classes, and can be accessed only through the methods of
their current class.
To achieve encapsulation
Declare the variables of a class as private.
Provide public setter and getter methods to modify and view
the variables values.
Why Encapsulation?
Better control of class attributes and methods
Class attributes can be made read-only or write-only
Flexible
Increased security of data
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 8 / 12
Listing: Encapsulation Example
public class Staff {
private String name , idNum ;
private int age ;
public int getAge () {
return age ;}
public String getName () {
return name ;}
public String getIdNum () {
return idNum ;}
public void setAge ( int newAge ) {
age = newAge ;}
public void setName ( String newName ) {
name = newName ;}
public void setIdNum ( String newId ) {
idNum = newId ;}
}
public class Staff_Main {
public static void main ( String args []) {
Staff mystaff = new Staff () ;
mystaff . setName (" James ") ;
mystaff . setAge (20) ;
mystaff . setIdNum ("12343 ms ") ;
System . out . print ( mystaff . getName () + " " + mystaff . getAge () ) ;}
}
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 9 / 12
Interfaces
An interfaces is another way to achieve abstraction in Java.
An interface is a completely "abstract class" that is used to group related methods with
empty bodies
They are implemented by using the implements keyword.
The body of the interface method is provided by the "implement" class.

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

A package is use to group related classes in Java.


It is much like a folder in a file directory.
Packages are used to avoid name conflicts, and to write a better
maintainable code.
Categories
Built-in Packages (packages from the Java API)
User-defined 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 ) ;

// declare the variable for the radius , pi and height


double pi = Math . PI ; // declare and assign pi
float r ; // declare for radius
float h ; // declare for height

// request for user inputs of r and h


System . out . print (" Enter the radius : " );
r = input . nextFloat () ; // accept radius input
System . out . print ("\ nEnter the height : " );
h = input . nextFloat () ; // accept height input

// declare and compute volume of the cylinder


double v = pi * r * r * h ;
System . out . println (" The volume is " + v ) ;
}
}

ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 16, 2022 12 / 12

You might also like