SlideShare a Scribd company logo
ABSTRACT SUPERCLASSES AND
ABSTRACT METHODS
Chapter 8.3:
Polymorphism
 Polymorphism is an object-oriented concept that allows
us to create versatile software designs that deals with
multiple objects.
 The term polymorphism literally means "having many
forms“ – can refer to multiple types of related objects
over time in consistence way.
 Polymorphism is one of the most elegant uses of
inheritance.
 A polymorphic reference is a variable that can refer
to different types of objects at different points in time.
Specifically, a reference variable of a superclass type
can refer to any object of its subclasses in the
inheritance hierarchy.
Polymorphic Reference
 For example, if the Animal class is used to
derive a class called Cat, then a Animal
reference could be used to point to a Cat
object
 Similarly, Animal can refer to any of its
subclasses
Animal myPet;
myPet = new Cat();
Animal
CatRabbit
myPet = new Rabbit();
Polymorpic Reference
 Another example:
 UndergraduateStudent and GraduateStudent are
subclasses of Student, the following statements are valid:
Student stud1 = new Student();
stud1 = new UndergraduateStudent();
stud1 = new GraduateStudent();
Polymorphic Behaviour
 When a method is invoked using the
superclass variable, it is the class of the
object (not of the variable type) that
determines which method is run.
Student stud1 = new Student();
stud1.computeCourseGrade();
stud1 = new UndergraduateStudent();
stud1.computeCourseGrade();
stud1 = new GraduateStudent();
stud1.computeCourseGrade();
Polymorphic Behaviour
 The superclass type variable can only be used to
invoke methods in subclass that also exist in the
superclass (overridden by subclass). New
methods in subclass is not visible to the
superclass variable.
 Eg. If UndergraduateStudent has a method
printUG():
Student stud1 = stud1 = new UndergraduateStudent();
stud1.printUG(); //======== error!!
Creating the roster Array
 We can maintain our class roster using an
array, combining objects from the Student,
UndergraduateStudent, and
GraduateStudent classes.
Student roster = new Student[40];
. . .
roster[0] = new GraduateStudent();
roster[1] = new UndergraduateStudent();
roster[2] = new UndergraduateStudent();
. . .
State of the roster Array
 The roster array with elements referring to
instances of GraduateStudent or
UndergraduateStudent classes.
Sample Polymorphic
Message
 To compute the course grade using the roster
array, we execute
 If roster[i] refers to a GraduateStudent, then
the computeCourseGrade method of the
GraduateStudent class is executed.
 If roster[i] refers to an UndergraduateStudent,
then the computeCourseGrade method of the
UndergraduateStudent class is executed.
for (int i = 0; i < numberOfStudents; i++) {
roster[i].computeCourseGrade();
}
The instanceof Operator
 The instanceof operator can help us learn the
class of an object.
 The following code counts the number of
undergraduate students.
int undergradCount = 0;
for (int i = 0; i < numberOfStudents; i++) {
if ( roster[i] instanceof UndergraduateStudent ) {
undergradCount++;
}
}
Abstract class
 a class that cannot be instantiated but that can be
the parent of other classes.
 objects can ONLY be created from subclass that
inherits from the abstract super (parent) class
 the subclass is forced to implement the abstract
method inherited from the super class through the
overriding process
Java Abstract classes
 An abstract class is a class that is declared
with the reserved word abstract in its heading.
abstract class ClassName {
. . . . . // definitions of methods and variables
}
Abstract classes rules
 An abstract class can contain instance
variables, constructors, the finalizer, and
nonabstract methods
 An abstract class can contain abstract
method(s).
 If a class contains an abstract method, then the
class must be declared abstract.
 We cannot instantiate an object from abstract
class. We can only declare a reference
variable of an abstract class type.
 We can instantiate an object of a subsclass of
an abstract class, but only if the class gives the
definitions of all the abstract methods of the
superclass.
Abstract method
 An abstract method is a method that has only the
header without body.
 The header of an abstract method must contain the
reserved word abstract and ends with semicolon(;).
 Syntax:
<AccessSpecifier> abstract ReturnType
MethodName(ParameterList);
 E.g.
public abstract void print();
public abstract String larger(int value);
void abstract insert(Object item);
Example of an abstract class &
method
public abstract class Animal
{
protected String type;
public void setType(String t)
{ type = t;
}
public abstract void sound();
}
Abstract method rules
 Abstract method declaration must be ended with
semicolon (;)
 Abstract method cannot be private type because a
private members cannot be accessed.
 Constructor and static method cannot be used as
abstract method.
 Must be overridden by non-abstract subclass
Abstract class declaration
abstract class Card {
String recipient; // name of who gets the card
public abstract void greeting(); //abstract greeting() method
}
Abstract Class Card and its
Subclasses
abstract Card
abstract greeting( )
AidulFitriCard
greeting( )
BirthdayCard
greeting( )
String recipient
int ageint syawalYear
Abstract method MUST be
overridden by subclass
public abstract class Card
{
String recipient;
public abstract String greeting();
}
public class AidulFitriCard extends Card
{
int syawalYear;
public String greeting()
{
……….
}
}
public class BirthdayCard extends Card
{
int age;
public String greeting()
{
………..
}
}
AidulFitriCard Class
public class AidulFitriCard extends Card
{
int syawalYear;
public AidulFitriCard(String who, int year) {
recipient = who;
syawalYear = year;
}
public String greeting()
{
System.out.println(“To “ + recipient);
System.out.println(“Selamat Hari Raya Aidul Fitri”);
System.out.println(“1 Syawal “ + syawalYear);
}
}
BirthdayCard Class
public class BirthdayCard extends ________
{
int age;
public _____________(String who, int year) {
recipient = who;
age = year;
}
public String greeting()
{
System.out.println(“Happy birthday to “+ _______);
System.out.println(“You are now “ +age+ “ years old.”);
}
}
CardTester Program
public class CardTester {
public static void main ( String[] args ) {
String name;
Scanner input = new Scanner(System.in);
System.out.print(“Your name please>");
name = input.nextLine();
Card myCard = new AidulFitri( me, 1429 );
myCard.greeting();
myCard = new BirthdayCard( me, 35 );
myCard.greeting();
}
}
 Demonstrate abstract class and polymorphism

More Related Content

What's hot

Lecture 2
Lecture 2Lecture 2
Lecture 2
emailharmeet
 
1207028 634528828886611250
1207028 6345288288866112501207028 634528828886611250
1207028 634528828886611250
Akhil Nama
 
Data types in java | What is Datatypes in Java | Learning with RD | Created b...
Data types in java | What is Datatypes in Java | Learning with RD | Created b...Data types in java | What is Datatypes in Java | Learning with RD | Created b...
Data types in java | What is Datatypes in Java | Learning with RD | Created b...
rahuldaredia21
 
Md03 - part3
Md03 - part3Md03 - part3
Md03 - part3
Rakesh Madugula
 
Data types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaData types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in java
Javed Rashid
 
ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
Atul Sehdev
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
Anil Bapat
 
java programming basics - part ii
 java programming basics - part ii java programming basics - part ii
java programming basics - part ii
jyoti_lakhani
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
Akshay soni
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patterns
Olivier Bacs
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
Ravi_Kant_Sahu
 
+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes
Andrew Raj
 
wrapper classes
wrapper classeswrapper classes
wrapper classes
Rajesh Roky
 
Java tutorial part 3
Java tutorial part 3Java tutorial part 3
Java tutorial part 3
Mumbai Academisc
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classes
teach4uin
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
Zafor Iqbal
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
dplunkett
 
Lecture 4 classes ii
Lecture 4 classes iiLecture 4 classes ii
Lecture 4 classes ii
the_wumberlog
 
Farhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd YearFarhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd Year
dezyneecole
 

What's hot (20)

Lecture 2
Lecture 2Lecture 2
Lecture 2
 
1207028 634528828886611250
1207028 6345288288866112501207028 634528828886611250
1207028 634528828886611250
 
Data types in java | What is Datatypes in Java | Learning with RD | Created b...
Data types in java | What is Datatypes in Java | Learning with RD | Created b...Data types in java | What is Datatypes in Java | Learning with RD | Created b...
Data types in java | What is Datatypes in Java | Learning with RD | Created b...
 
Md03 - part3
Md03 - part3Md03 - part3
Md03 - part3
 
Data types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaData types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in java
 
ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
java programming basics - part ii
 java programming basics - part ii java programming basics - part ii
java programming basics - part ii
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patterns
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes
 
wrapper classes
wrapper classeswrapper classes
wrapper classes
 
Java tutorial part 3
Java tutorial part 3Java tutorial part 3
Java tutorial part 3
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classes
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
 
Lecture 4 classes ii
Lecture 4 classes iiLecture 4 classes ii
Lecture 4 classes ii
 
Farhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd YearFarhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd Year
 

Viewers also liked

Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
sotlsoc
 
Chapter 7.3
Chapter 7.3Chapter 7.3
Chapter 7.3
sotlsoc
 
Chapter 6.2
Chapter 6.2Chapter 6.2
Chapter 6.2
sotlsoc
 
Chapter 5.2
Chapter 5.2Chapter 5.2
Chapter 5.2
sotlsoc
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1
sotlsoc
 
Chapter 6.4
Chapter 6.4Chapter 6.4
Chapter 6.4
sotlsoc
 
Chapter 4.1
Chapter 4.1Chapter 4.1
Chapter 4.1
sotlsoc
 
Chapter 2.4
Chapter 2.4Chapter 2.4
Chapter 2.4
sotlsoc
 
Chapter 7.0
Chapter 7.0 Chapter 7.0
Chapter 7.0
sotlsoc
 

Viewers also liked (9)

Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
 
Chapter 7.3
Chapter 7.3Chapter 7.3
Chapter 7.3
 
Chapter 6.2
Chapter 6.2Chapter 6.2
Chapter 6.2
 
Chapter 5.2
Chapter 5.2Chapter 5.2
Chapter 5.2
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1
 
Chapter 6.4
Chapter 6.4Chapter 6.4
Chapter 6.4
 
Chapter 4.1
Chapter 4.1Chapter 4.1
Chapter 4.1
 
Chapter 2.4
Chapter 2.4Chapter 2.4
Chapter 2.4
 
Chapter 7.0
Chapter 7.0 Chapter 7.0
Chapter 7.0
 

Similar to Chapter 8.3

Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13
Vince Vo
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
Chap11
Chap11Chap11
Chap11
Terry Yoast
 
Bc0037
Bc0037Bc0037
Bc0037
hayerpa
 
Application package
Application packageApplication package
Application package
JAYAARC
 
Chapter8:Understanding Polymorphism
Chapter8:Understanding PolymorphismChapter8:Understanding Polymorphism
Chapter8:Understanding Polymorphism
It Academy
 
Classes2
Classes2Classes2
Classes2
phanleson
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
Terry Yoast
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
Object and class
Object and classObject and class
Object and class
mohit tripathi
 
.NET F# Abstract class interface
.NET F# Abstract class interface.NET F# Abstract class interface
.NET F# Abstract class interface
DrRajeshreeKhande
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
SakkaravarthiS1
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
Java Concepts
Java ConceptsJava Concepts
Java Concepts
AbdulImrankhan7
 
polymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdfpolymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdf
BapanKar2
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
Vince Vo
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
Ahsan Raja
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
Eduardo Bergavera
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
Abid Kohistani
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
Suresh Mohta
 

Similar to Chapter 8.3 (20)

Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
 
Chap11
Chap11Chap11
Chap11
 
Bc0037
Bc0037Bc0037
Bc0037
 
Application package
Application packageApplication package
Application package
 
Chapter8:Understanding Polymorphism
Chapter8:Understanding PolymorphismChapter8:Understanding Polymorphism
Chapter8:Understanding Polymorphism
 
Classes2
Classes2Classes2
Classes2
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Object and class
Object and classObject and class
Object and class
 
.NET F# Abstract class interface
.NET F# Abstract class interface.NET F# Abstract class interface
.NET F# Abstract class interface
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
Java Concepts
Java ConceptsJava Concepts
Java Concepts
 
polymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdfpolymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdf
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
 

More from sotlsoc

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 new
sotlsoc
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 new
sotlsoc
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 new
sotlsoc
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
sotlsoc
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
sotlsoc
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4
sotlsoc
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3
sotlsoc
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
sotlsoc
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
sotlsoc
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0
sotlsoc
 
Chapter 10.2
Chapter 10.2Chapter 10.2
Chapter 10.2
sotlsoc
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
sotlsoc
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0
sotlsoc
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0
sotlsoc
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3
sotlsoc
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4
sotlsoc
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1
sotlsoc
 
Chapter 8.1
Chapter 8.1Chapter 8.1
Chapter 8.1
sotlsoc
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0
sotlsoc
 
Chapter 9.2
Chapter 9.2Chapter 9.2
Chapter 9.2
sotlsoc
 

More from sotlsoc (20)

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 new
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 new
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 new
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0
 
Chapter 10.2
Chapter 10.2Chapter 10.2
Chapter 10.2
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1
 
Chapter 8.1
Chapter 8.1Chapter 8.1
Chapter 8.1
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0
 
Chapter 9.2
Chapter 9.2Chapter 9.2
Chapter 9.2
 

Recently uploaded

RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
spdendr
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 

Recently uploaded (20)

RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 

Chapter 8.3

  • 1. ABSTRACT SUPERCLASSES AND ABSTRACT METHODS Chapter 8.3:
  • 2. Polymorphism  Polymorphism is an object-oriented concept that allows us to create versatile software designs that deals with multiple objects.  The term polymorphism literally means "having many forms“ – can refer to multiple types of related objects over time in consistence way.  Polymorphism is one of the most elegant uses of inheritance.  A polymorphic reference is a variable that can refer to different types of objects at different points in time. Specifically, a reference variable of a superclass type can refer to any object of its subclasses in the inheritance hierarchy.
  • 3. Polymorphic Reference  For example, if the Animal class is used to derive a class called Cat, then a Animal reference could be used to point to a Cat object  Similarly, Animal can refer to any of its subclasses Animal myPet; myPet = new Cat(); Animal CatRabbit myPet = new Rabbit();
  • 4. Polymorpic Reference  Another example:  UndergraduateStudent and GraduateStudent are subclasses of Student, the following statements are valid: Student stud1 = new Student(); stud1 = new UndergraduateStudent(); stud1 = new GraduateStudent();
  • 5. Polymorphic Behaviour  When a method is invoked using the superclass variable, it is the class of the object (not of the variable type) that determines which method is run. Student stud1 = new Student(); stud1.computeCourseGrade(); stud1 = new UndergraduateStudent(); stud1.computeCourseGrade(); stud1 = new GraduateStudent(); stud1.computeCourseGrade();
  • 6. Polymorphic Behaviour  The superclass type variable can only be used to invoke methods in subclass that also exist in the superclass (overridden by subclass). New methods in subclass is not visible to the superclass variable.  Eg. If UndergraduateStudent has a method printUG(): Student stud1 = stud1 = new UndergraduateStudent(); stud1.printUG(); //======== error!!
  • 7. Creating the roster Array  We can maintain our class roster using an array, combining objects from the Student, UndergraduateStudent, and GraduateStudent classes. Student roster = new Student[40]; . . . roster[0] = new GraduateStudent(); roster[1] = new UndergraduateStudent(); roster[2] = new UndergraduateStudent(); . . .
  • 8. State of the roster Array  The roster array with elements referring to instances of GraduateStudent or UndergraduateStudent classes.
  • 9. Sample Polymorphic Message  To compute the course grade using the roster array, we execute  If roster[i] refers to a GraduateStudent, then the computeCourseGrade method of the GraduateStudent class is executed.  If roster[i] refers to an UndergraduateStudent, then the computeCourseGrade method of the UndergraduateStudent class is executed. for (int i = 0; i < numberOfStudents; i++) { roster[i].computeCourseGrade(); }
  • 10. The instanceof Operator  The instanceof operator can help us learn the class of an object.  The following code counts the number of undergraduate students. int undergradCount = 0; for (int i = 0; i < numberOfStudents; i++) { if ( roster[i] instanceof UndergraduateStudent ) { undergradCount++; } }
  • 11. Abstract class  a class that cannot be instantiated but that can be the parent of other classes.  objects can ONLY be created from subclass that inherits from the abstract super (parent) class  the subclass is forced to implement the abstract method inherited from the super class through the overriding process
  • 12. Java Abstract classes  An abstract class is a class that is declared with the reserved word abstract in its heading. abstract class ClassName { . . . . . // definitions of methods and variables }
  • 13. Abstract classes rules  An abstract class can contain instance variables, constructors, the finalizer, and nonabstract methods  An abstract class can contain abstract method(s).  If a class contains an abstract method, then the class must be declared abstract.  We cannot instantiate an object from abstract class. We can only declare a reference variable of an abstract class type.  We can instantiate an object of a subsclass of an abstract class, but only if the class gives the definitions of all the abstract methods of the superclass.
  • 14. Abstract method  An abstract method is a method that has only the header without body.  The header of an abstract method must contain the reserved word abstract and ends with semicolon(;).  Syntax: <AccessSpecifier> abstract ReturnType MethodName(ParameterList);  E.g. public abstract void print(); public abstract String larger(int value); void abstract insert(Object item);
  • 15. Example of an abstract class & method public abstract class Animal { protected String type; public void setType(String t) { type = t; } public abstract void sound(); }
  • 16. Abstract method rules  Abstract method declaration must be ended with semicolon (;)  Abstract method cannot be private type because a private members cannot be accessed.  Constructor and static method cannot be used as abstract method.  Must be overridden by non-abstract subclass
  • 17. Abstract class declaration abstract class Card { String recipient; // name of who gets the card public abstract void greeting(); //abstract greeting() method }
  • 18. Abstract Class Card and its Subclasses abstract Card abstract greeting( ) AidulFitriCard greeting( ) BirthdayCard greeting( ) String recipient int ageint syawalYear
  • 19. Abstract method MUST be overridden by subclass public abstract class Card { String recipient; public abstract String greeting(); } public class AidulFitriCard extends Card { int syawalYear; public String greeting() { ………. } } public class BirthdayCard extends Card { int age; public String greeting() { ……….. } }
  • 20. AidulFitriCard Class public class AidulFitriCard extends Card { int syawalYear; public AidulFitriCard(String who, int year) { recipient = who; syawalYear = year; } public String greeting() { System.out.println(“To “ + recipient); System.out.println(“Selamat Hari Raya Aidul Fitri”); System.out.println(“1 Syawal “ + syawalYear); } }
  • 21. BirthdayCard Class public class BirthdayCard extends ________ { int age; public _____________(String who, int year) { recipient = who; age = year; } public String greeting() { System.out.println(“Happy birthday to “+ _______); System.out.println(“You are now “ +age+ “ years old.”); } }
  • 22. CardTester Program public class CardTester { public static void main ( String[] args ) { String name; Scanner input = new Scanner(System.in); System.out.print(“Your name please>"); name = input.nextLine(); Card myCard = new AidulFitri( me, 1429 ); myCard.greeting(); myCard = new BirthdayCard( me, 35 ); myCard.greeting(); } }  Demonstrate abstract class and polymorphism