SlideShare a Scribd company logo
COMPUTER
PROGAMMING 2
Prepared by Regine Jesa J. Palara, MIT
Inheritance
• It enables the use of existing class to define a new class.
• Derived Class is defined by adding instance variables and
methods to an existing class.
• It is also known as subclass, child class, and descendant class.
• Base Class is the existing class that the derived class is built
upon.
• It is also known as superclass, parent class, and ancestor class.
• Syntax of inheritance:
Inheritance
• Sample usage:
• If a derived class defines a method with the same name, the
same number and types of parameters and the same return
type as a method in the base class, the definition in the derived
class is said to override the definition in the base class.
• A derived class has its own constructor. It does not inherit any
constructors from the base class.
• In the definition of a constructor for the derived class, the typical
first action is to call a constructor of the base class.
Inheritance
• One way of defining a constructor is by calling another
constructor in the same class. The default constructor calls
another in that class by using this keyword.
• The default constructor Person() calls the constructor.
• Any use of the super keyword must be the first action in a
constructor definition.
Polymorphism
• Is the ability of an object to take on many forms.
• It allows you to make changes in the method definition for the
derived classes and have those changes apply to the methods
written in the base class.
• The most common use of polymorphism occurs when a base
class reference is used to refer to a derived class object.
• When an overridden method is invoked, its action is the one
defined in the class used to create the object using the new
operator. It is not determined by the type of the variable naming
the object.
Polymorphism
• Dynamic Binding allows many meanings to be associated to
one method name.
• The definition of a method is not bound to an invocation of the method
until run time when the method is called. This is the mechanism used
by polymorphism.
• Example:
Interface and Abstract Method
• Interface is used to specify methods that a class must
implement. It contains headings for a number of public
methods.
• An interface should include comments that describe the
methods.
• Different classes can implement the same interface.
• Syntax:
• Sample usage:
Interface and Abstract Method
• Abstract Class is a method that is declared as abstract and
does not have an implementation.
• This is written without braces, followed by semicolon.
• Syntax:
• Abstract classes are similar to interfaces. They both cannot be
instantiated and may contain a mix of methods declared with or
without an implementation.
Interface and Abstract Method
• Consider using interfaces if any of these statements apply to your
situation:
✓You expect that unrelated classes would implement you interface.
✓You want to specify the behavior of a particular data type, but not concerned
about who implements its behavior.
✓You want to take advantage of multiple inheritance of type.
• Consider using abstract class if any of these statements apply to
your situation:
✓You want to share code among several closely related classes.
✓You expect that classes that extend your abstract have many common
methods or fields, or require access modifiers other than public (such as
protected and private).
✓You want to declare non-static or non-final fields. This enables you to define
methods that can access and modify the state of the object to which they
belong.

More Related Content

Similar to 04_-_Inheritance_Polymorphism_and_Interfaces.pdf

Object-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & ProgrammingObject-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & Programming
Allan Mangune
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2ee
Shiva Cse
 

Similar to 04_-_Inheritance_Polymorphism_and_Interfaces.pdf (20)

Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
Java session2
Java session2Java session2
Java session2
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Java programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- InheritanceJava programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- Inheritance
 
Java
JavaJava
Java
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
OOPS Characteristics
OOPS CharacteristicsOOPS Characteristics
OOPS Characteristics
 
Object-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & ProgrammingObject-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & Programming
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with java
 
Unit 4
Unit 4Unit 4
Unit 4
 
Object Oriented Programming C#
Object Oriented Programming C#Object Oriented Programming C#
Object Oriented Programming C#
 
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
 
Oopsinphp
OopsinphpOopsinphp
Oopsinphp
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2ee
 
Abstraction in java.pptx
Abstraction in java.pptxAbstraction in java.pptx
Abstraction in java.pptx
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
 

Recently uploaded

standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
Computer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage sComputer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage s
MAQIB18
 
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Domenico Conte
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
enxupq
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
ewymefz
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
ewymefz
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 

Recently uploaded (20)

standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
Computer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage sComputer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage s
 
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
Tabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflowsTabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflows
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 
Supply chain analytics to combat the effects of Ukraine-Russia-conflict
Supply chain analytics to combat the effects of Ukraine-Russia-conflictSupply chain analytics to combat the effects of Ukraine-Russia-conflict
Supply chain analytics to combat the effects of Ukraine-Russia-conflict
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
How can I successfully sell my pi coins in Philippines?
How can I successfully sell my pi coins in Philippines?How can I successfully sell my pi coins in Philippines?
How can I successfully sell my pi coins in Philippines?
 
tapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive datatapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive data
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
Using PDB Relocation to Move a Single PDB to Another Existing CDB
Using PDB Relocation to Move a Single PDB to Another Existing CDBUsing PDB Relocation to Move a Single PDB to Another Existing CDB
Using PDB Relocation to Move a Single PDB to Another Existing CDB
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 

04_-_Inheritance_Polymorphism_and_Interfaces.pdf

  • 1. COMPUTER PROGAMMING 2 Prepared by Regine Jesa J. Palara, MIT
  • 2. Inheritance • It enables the use of existing class to define a new class. • Derived Class is defined by adding instance variables and methods to an existing class. • It is also known as subclass, child class, and descendant class. • Base Class is the existing class that the derived class is built upon. • It is also known as superclass, parent class, and ancestor class. • Syntax of inheritance:
  • 3. Inheritance • Sample usage: • If a derived class defines a method with the same name, the same number and types of parameters and the same return type as a method in the base class, the definition in the derived class is said to override the definition in the base class. • A derived class has its own constructor. It does not inherit any constructors from the base class. • In the definition of a constructor for the derived class, the typical first action is to call a constructor of the base class.
  • 4. Inheritance • One way of defining a constructor is by calling another constructor in the same class. The default constructor calls another in that class by using this keyword. • The default constructor Person() calls the constructor. • Any use of the super keyword must be the first action in a constructor definition.
  • 5. Polymorphism • Is the ability of an object to take on many forms. • It allows you to make changes in the method definition for the derived classes and have those changes apply to the methods written in the base class. • The most common use of polymorphism occurs when a base class reference is used to refer to a derived class object. • When an overridden method is invoked, its action is the one defined in the class used to create the object using the new operator. It is not determined by the type of the variable naming the object.
  • 6. Polymorphism • Dynamic Binding allows many meanings to be associated to one method name. • The definition of a method is not bound to an invocation of the method until run time when the method is called. This is the mechanism used by polymorphism. • Example:
  • 7. Interface and Abstract Method • Interface is used to specify methods that a class must implement. It contains headings for a number of public methods. • An interface should include comments that describe the methods. • Different classes can implement the same interface. • Syntax: • Sample usage:
  • 8. Interface and Abstract Method • Abstract Class is a method that is declared as abstract and does not have an implementation. • This is written without braces, followed by semicolon. • Syntax: • Abstract classes are similar to interfaces. They both cannot be instantiated and may contain a mix of methods declared with or without an implementation.
  • 9. Interface and Abstract Method • Consider using interfaces if any of these statements apply to your situation: ✓You expect that unrelated classes would implement you interface. ✓You want to specify the behavior of a particular data type, but not concerned about who implements its behavior. ✓You want to take advantage of multiple inheritance of type. • Consider using abstract class if any of these statements apply to your situation: ✓You want to share code among several closely related classes. ✓You expect that classes that extend your abstract have many common methods or fields, or require access modifiers other than public (such as protected and private). ✓You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.