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

JavaProgramming_Lecture04

Uploaded by

stacy russo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

JavaProgramming_Lecture04

Uploaded by

stacy russo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

AP/ITEC 2610 3.

00
Object Oriented
Programming
Lecture - 4
Sept, 30th 2022 (4pm-7pm)
Learning outcomes:
• Inheritance
• implementing super-classes and a subclasses
• types of Inheritance
1. single class
2. multiple class (interface)
3. hierarchical class
4. hybrid class
-Reusing of the code
• Polymorphism
• Abstract Classes
• Interface Class
• Nested (or Inner) Classes – Classes of classes
Inheritance
• Inheritance is an important pillar of OOP(Object Oriented
Programming).
• It is the mechanism in Java by which one class is allowed to inherit the
features (fields and methods) of another class.
• Classes inherit from a super-class by the extend keyword
• Only 1 super-class can be extended at a time.
Inheritance Example:

• Child – Parents
• Car – Vehicle
• Dog – Animal
• Lecturer – Employee
• Hotel - Building
Inheritance Hierarchies
• Inheritance: the relationship between a more general class
(superclass) and a more specialized class (subclass).
• The subclass inherits data and behavior from the superclass.
Cars share the common traits of all vehicles
Example: the ability to transport people from one place to another

Figure 1 An Inheritance Hierarchy of Vehicle Classes


Other Example of Inheritance
Student Account

Undergrad Postgrad Checking Savings


Account Account

Master’s PhD

• general vs. specialized


• subclass is also called derived class,
extended class, or child class
• superclass is also called base class or
parent class
Syntax - Inheritance:
• subclass (child) - the class
that inherits from another
extends keyword class
• superclass (parent) - the
class being inherited from


class SimpleGui extends JFrame {

}
Types of inheritance:
Hierarchical Inheritance.
An Example code Template:
// Consider the following
class cls1:
Source file Main_In.java Class file ClsA.java
class clsA
{ class Main_In class ClsA {
void add(int p,int q) { {
public static void int p;
System.out.println(p+q); main(String[] args) int q;
} { void add() {
}
// There is another // Create object of a subclass System.out.println("add = " +
ClsB o1 = new ClsB(); p+q); }
class cls2 which extends class // access field of superclass using the }
cls1: integer variables that stores the values //Class B inherit from class A
class clsB extends clsA o1.p = 5;
class ClsB extends ClsA {
o1.q = 6;
{ // call method of superclass using
void mul(int p,int q) void mul() {
object of subclass
System.out.println("product = " +
{ o1.add();
p*q); }
System.out.println(p*q); o1.mul();
}
} }
}
}
Output:
Run Main_In.java and ClsA.java files from your shell:
• Compile
• Output

Arhums-Air:Chap_1 arhumsultana$ javac Main_In.java


Arhums-Air:Chap_1 arhumsultana$ javac ClsA.java
Arhums-Air:Chap_1 arhumsultana$ java Main_In
add = 56
product = 30
Polymorphism
• Means ‘many forms’
• In Java it means we can define methods (functions) many times
• when a function is defined many times in the tree of inheritance, Java
works out which is the correct one to use
• When we extend a class we can override previously defined
variables and methods.
Traditional Example of Inheritance and
Polymorphism
Main Program Class
class Main { class Animal {
public void animalAd() {
public static void main(String[] args) { System.out.println("I'm Abstract!");
// Let’s make a new objecct called inedible }
Animal i = new Animal(); }
// Create a Animal object which is a name of
your class class Pig extends Animal {
public void animalAd() {
Animal p = new Pig(); // Create a Pig System.out.println("Oink oink ... I'm not a
object healthy animal");
Animal b = new Dog(); // Create a Dog }
object }
Animal r = new Cow(); // Create a Cow
class Dog extends Animal {
object public void animalAd() {
i.animalAd(); // Polymorphism that is System.out.println("Woof woof ... I'm your
different functions, same name/Animal Class with animalAd() best friend and not edible");
method }
p.animalAd(); Polymorphism defined }
class Cow extends Animal {
b.animalAd(); public void animalAd() {
r.animalAd(); System.out.println("Moo Moo... I'm your best
farm buddy and delicious");
} }
}
}
Cmd line Output: Animal class
Inheritance
Polymorphism – Sample code using
extends key word

Polymorphism defined
Inheritance Class File Polymorphism
Output:
Run Polymorphism.java (main class file) from your shell:
• Compile
• Output

Arhums-Air:Chap_1 arhumsultana$ javac Polymorphism.java


Arhums-Air:Chap_1 arhumsultana$ java Polymorphism
Quack Quack
Predator - Big Eyes - Big Claws
Abstract Classes: What are they?
• Data abstraction is the process of hiding certain details and
showing only essential information to the user.

• Abstraction can be achieved with either abstract classes or


interfaces
Java Abstract Class / Final Class –
Facts!
• The abstract keyword is a non-access modifier, used for classes and methods:
• 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).
• Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be
inherited from another class).
• An abstract class can have both abstract and regular methods
• Example code (slide # 18)

• An abstract class MUST be inherited


• cannot be used on its own – inheritance only!
• A final class cannot be inherited.
• final can also be used to prevent class members from being overridden or
modified
Example of an Abstract
// Abstract class
Class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep() {
System.out.println("Zzz");

}
} Output:
// Subclass (inherit from Animal)
class Cat extends Animal { Arhums-Air:Abstract_code arhumsultana$ javac Main.java
public void animalSound() { Arhums-Air:Abstract_code arhumsultana$ java Main
// The body of animalSound() is provided here The Cat says: MEWW MEAOO
System.out.println("The Cat says: MEWW
Zzz
MEAOO");
}
}
class Main {
public static void main(String[] args) {
Cat out = new Cat(); // Create a cat object
out.animalSound();
out.sleep();
}
}
Java Interface Class
covered in more detail next week

• Java Interface Class is a type of abstract class.


• it allows methods to be defined without writing any code
• The keywords are interface to define the abstract class and
implements to inherit from it.
• it is the only Java class to allow inheritance from multiple classes
• use keyword implement followed by a list of interface classes, separated by commas
• Interface classes are often used for GUI interfaces.
• covered in detail in next lecture

class SimpleGui extends JFrame implements MouseListener, KeyListener {…}

Example of Interface Class: GUI

/* Hello GUI Class */

import javax.swing.*; // JFrame, JMenuBar, JMenu, JMenuItem


import java.awt.event.*; // ActionListener, ActionEvent

public class HelloGUI { … }

class SimpleGui extends JFrame implements ActionListener


{ … }

/* Get Full Code from Eclass */

Inheritance
Java Nested Classes (Inner Classes)
class Outer {
int x = 10;
• Java Nested Classes are classes defined
inside other classes. // sub-classes are not automatically
created
• This is a naming convention only. Nested n = new Nested();
• think of it like a sub-directory
class Nested {
• dot notation is used for the name int y = 5; }
• e.g. System.out … }
• byte code shows how this works
public class Main {
• Nested class is not constructed public static void main(String[] args)
{
automatically when the outer class is Outer myNest = new Outer();
created. System.out.println(myNest.x + myNest.n.y);

• can be treated like any other normal class, myNest.x = 102;


just with dot notation name myNest.n.y = -690;
System.out.println(myNest.x + myNest.n.y);
• or created internally with constructor }
Nested member classes can be createdNested member classes should
externally
class Outer {
be created using constructor
int x = 10; class Outer {
int x;
class Nested { Nested n;
int y = 5; } public Outer() {x = 10; n = new
Nested();}
class Nested { int y;
} private Nested() { y = 5; } }
public class Main { }
public static void main(String[]
args) { public class Main {
Outer myNest = new Outer();
Outer.Nested n = myNest.new public static void main(String[] args)
{
Nested();
Outer myNest = new Outer();
System.out.println(myNest.x + n.y);
System.out.println(myNest.x + myNest.n.y);

myNest.x = 22; myNest.x = 22;


n.y = 33; myNest.n.y = 33;
System.out.println(myNest.x + n.y); System.out.println(myNest.x + myNest.n.y);
} }
} }
Homework:
• Create a new instance of a sub-sub-class and initialize with a
constructor.
• Debug error in constructing class – next slide.
Debug class Outer {
int x;
Nested n;
public Outer(int v) {x = v; Nested n = new
• This code has a null Nested(5);}
class Nested { int y;
pointer run-time error. private Nested(int v) { y = v; } }
}
• Locate exactly where
error happens by putting public class Main {
temporary println public static void main(String[] args) {
statements into the code Outer myNest = new Outer(10);
System.out.println(myNest.x + myNest.n.y);
• Explain why error
happens myNest.x = 22;
myNest.n.y = 33;
• Fix error. System.out.println(myNest.x + myNest.n.y);
}
}
Online reading
• Java Inheritance (Subclass and Superclass)
https://www.w3schools.com/java/java_inheritance.asp
• Java Polymorphism
https://www.w3schools.com/java/java_polymorphism.asp
• Java Modifiers
https://www.w3schools.com/java/java_modifiers.asp

• Java Abstract class/method


• https://www.w3schools.com/java/java_abstract.asp

You might also like