SlideShare a Scribd company logo
1 of 23
USING CLASSES WITH
POLYMORPHISM
Chapter 8.2:
Defining Classes with Inheritance
 Case Study:
 Suppose we want implement a class roster that
contains both undergraduate and graduate
students.
 Each student’s record will contain his or her
name, three test scores, and the final course
grade.
 The formula for determining the course grade is
different for graduate students than for
undergraduate students.
Undergrads: pass if avg test score >= 70
Grads: pass if avg test score >= 80
Modeling Two Types of Students
 There are two ways to design the
classes to model undergraduate and
graduate students.
 We can define two unrelated classes, one for
undergraduates and one for graduates.
 We can model the two kinds of students by
using classes that are related in an inheritance
hierarchy.
 Two classes are unrelated if they are not
connected in an inheritance relationship.
Classes for the Class
Roster
 For the Class Roster sample, we design three
classes:
 Student
 UndergraduateStudent
 GraduateStudent
 The Student class will incorporate behavior and
data common to both UndergraduateStudent and
GraduateStudent objects.
 The UndergraduateStudent class and the
GraduateStudent class will each contain
behaviors and data specific to their respective
objects.
Inheritance Hierarchy
Definition of GraduateStudent &
UndergraduateStudent classes
class GraduateStudent extends Student {
//constructor not shown
public void computeCourseGrade() {
int total = 0;
total = test1 + test2 + test3;
if (total / 3 >= 80) {
courseGrade = "Pass";
} else {
courseGrade = "No Pass";
}
}
}
class UndergraduateStudent extends Student {
//Constructor not shown
public void computeCourseGrade() {
int total = 0;
total = test1 + test2 + test3;
if (total / 3 >= 70) {
courseGrade = "Pass";
} else {
courseGrade = "No Pass";
}
}
}
Declaring a Subclass
 A subclass inherits data and methods from the
superclass. In the subclass, you can also:
 Add new data
 Add new methods
 Override the methods of the superclass
○ Modify existing behaviour of parent
Overriding vs. Overloading
public class Test {
public static void main(String[] args) {
A a = new A();
a.p(10);
}
}
class B {
public void p(int i) {
}
}
class A extends B {
// This method overrides the method in B
public void p(int i) {
System.out.println(i);
}
}
public class Test {
public static void main(String[] args) {
A a = new A();
a.p(10);
}
}
class B {
public void p(int i) {
}
}
class A extends B {
// This method overloads the method in B
public void p(double i) {
System.out.println(i);
}
}
Inheritance Rules
1. The private members of the superclass are
private to the superclass
2. The subclass can access the members of the
superclass according to the accessibility rules
3. The subclass can include additional data and/or
method members
Inheritance Rules
(continued)
4. The subclass can override, that is, redefine
the methods of the superclass
 The overriding method in subclass must have
similar
 Name
 Parameter list
 Return type
5. All members of the superclass are also
members of the subclass
 Similarly, the methods of the superclass (unless
overridden) are also the methods of the
subclass
 Remember Rule 1 & 2 when accessing a
member of the superclass in the subclass
Inheritance Rules
(continued)
6. (Using the Keyword super)
The keyword super refers to the direct
superclass of a subclass . This keyword can be
used in two ways:
 To call a superclass constructor
 super(); //must be the first statement in subclass’s constructor
 To call a superclass method
 super.methodname();
 this is only used if the subclass overrides the superclass
method
The Object Class is the Superclass
of Every Java Class
INHERITANCE: (Accessibility
Modifier)
 Sometimes , it is called visibility modifier
 Not all properties can be accessed by sub
class.
 Super class can control a data accessing
from subclass by giving the type of
accessing to the members and methods.
 A class can declare the data members or
method as a public, private or protected.
 If it is not declared, the data or method will
be set to default type.
INHERITANCE: Member
Accessibility
Accessibility criteria
Modifier Same Class Same
Package
Subclass Universe
private Yes No No No
default Yes Yes No No
protected Yes Yes Yes No
public Yes Yes Yes Yes
INHERITANCE: Data Accessibility
Sub class B
public int b
protected int c
Super class
int a
public int b
protected int c
private int d
Sub class A
int a
public int b
protected int c
Package B
Package A
Refer to the previous slide
 Super class has 2 subclasses : Subclass A
and Subclass B.
 Subclass A is defined in same package with
superclass, subclass B is defined outside
the package.
 There are 4 accessibility data types: public,
protected, private and default.
 Subclass A can access all properties of
superclass except private.
 But, subclass B can only access the
properties outside the package which are
public and protected.
Example: Visibility Modifiers
public class C1 {
public int x;
protected int y;
int z;
private int u;
protected void m() {
}
}
public class C2 {
C1 o = new C1();
can access o.x;
can access o.y;
can access o.z;
cannot access o.u;
can invoke o.m();
}
public class C3
extends C1 {
can access x;
can access y;
can access z;
cannot access u;
can invoke m();
}
package p1;
public class C4
extends C1 {
can access x;
can access y;
cannot access z;
cannot access u;
can invoke m();
}
package p2;
public class C5 {
C1 o = new C1();
can access o.x;
cannot access o.y;
cannot access o.z;
cannot access o.u;
cannot invoke o.m();
}
What’s wrong
with the
code?
How to fix it?
class ClassX
{
private int m;
public String toString()
{
return new String("(" + m + ")");
}
}
public class ClassY extends ClassX
{
private int n;
public String toString()
{
return new String("(" + m + " , " + n + ")");
}
}
class TestAccesibility
{
public static void main(String [] args)
{
ClassX x = new ClassX;
ClassY y = new ClassY;
System.out.println("x = " + x);
System.out.println("y = " + y);
}
}
Inheritance and
Constructors
 Unlike members of a superclass, constructors of a
superclass are not inherited by its subclasses.
 You must define a constructor for a class or use
the default constructor added by the compiler.
 The statement
super();
calls the superclass’s constructor.
 super(); must be the first statement in the
subclass contructor.
 A call to the constructor of the
superclass must be in the first
statement in the child constructor.
public Box(double l, double w, double h)
{
super(l,w);
height = h;
}
Rectangle myRectangle = new Rectangle(5, 3);
Box myBox = new Box(6, 5, 4);
Superclass’s Constructor Is Always
Invoked
 A subclass constructor may invoke its superclass’s
constructor. If none is invoked explicitly, the compiler
puts super() as the first statement in the constructor.
For example, the constructor of class A:
public A(double d) {
// some statements
}
is equivalent to
public A(double d) {
super();
// some statements
}
public A() {
}
is equivalent to
public A() {
super();
}
Example on the Impact of a
Superclass without no-arg
Constructor
 Find out the error in the program:
class Fruit {
public Fruit(String name) {
System.out.println("Fruit constructor is invoked");
}
}
public class Apple extends Fruit {
public Apple(String name) {
System.out.println(“Apple constructor is invoked");
}
}

More Related Content

What's hot

Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
Seminar on java
Seminar on javaSeminar on java
Seminar on javashathika
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-javaDeepak Singh
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritanceteach4uin
 
Java Inheritance
Java InheritanceJava Inheritance
Java InheritanceVINOTH R
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and objectFajar Baskoro
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08Terry Yoast
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
 
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية Mahmoud Alfarra
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaEdureka!
 
Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear ASHNA nadhm
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingShivam Singhal
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods javaPadma Kannan
 

What's hot (20)

Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | Edureka
 
Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
C# Types of classes
C# Types of classesC# Types of classes
C# Types of classes
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods java
 
Inheritance
Inheritance Inheritance
Inheritance
 

Viewers also liked

Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2sotlsoc
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3sotlsoc
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1sotlsoc
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1sotlsoc
 
Chapter 5.1
Chapter 5.1Chapter 5.1
Chapter 5.1sotlsoc
 
Chapter 3.1
Chapter 3.1Chapter 3.1
Chapter 3.1sotlsoc
 
Chapter 7.4
Chapter 7.4Chapter 7.4
Chapter 7.4sotlsoc
 
Chapter 2.2
Chapter 2.2Chapter 2.2
Chapter 2.2sotlsoc
 
Chapter 3.4
Chapter 3.4Chapter 3.4
Chapter 3.4sotlsoc
 

Viewers also liked (9)

Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
 
Chapter 5.1
Chapter 5.1Chapter 5.1
Chapter 5.1
 
Chapter 3.1
Chapter 3.1Chapter 3.1
Chapter 3.1
 
Chapter 7.4
Chapter 7.4Chapter 7.4
Chapter 7.4
 
Chapter 2.2
Chapter 2.2Chapter 2.2
Chapter 2.2
 
Chapter 3.4
Chapter 3.4Chapter 3.4
Chapter 3.4
 

Similar to Chapter 8.2

Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1PRN USM
 
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 InterfacesSakkaravarthiS1
 
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 examplesSunil Kumar Gunasekaran
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritanceKalai Selvi
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance SlidesAhsan Raja
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppthenokmetaferia1
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++RAJ KUMAR
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 

Similar to Chapter 8.2 (20)

Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
 
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
 
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
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Core java oop
Core java oopCore java oop
Core java oop
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Chap11
Chap11Chap11
Chap11
 
Hemajava
HemajavaHemajava
Hemajava
 
inheritance
inheritanceinheritance
inheritance
 
6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 

More from sotlsoc

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 newsotlsoc
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 newsotlsoc
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 newsotlsoc
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 newsotlsoc
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1sotlsoc
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4sotlsoc
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3sotlsoc
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5sotlsoc
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0sotlsoc
 
Chapter 10.2
Chapter 10.2Chapter 10.2
Chapter 10.2sotlsoc
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0sotlsoc
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0sotlsoc
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3sotlsoc
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4sotlsoc
 
Chapter 8.1
Chapter 8.1Chapter 8.1
Chapter 8.1sotlsoc
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0sotlsoc
 
Chapter 9.2
Chapter 9.2Chapter 9.2
Chapter 9.2sotlsoc
 
Chapter 8.3
Chapter 8.3Chapter 8.3
Chapter 8.3sotlsoc
 
Chapter 5.2
Chapter 5.2Chapter 5.2
Chapter 5.2sotlsoc
 
Chapter 7.3
Chapter 7.3Chapter 7.3
Chapter 7.3sotlsoc
 

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 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
 
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.0
Chapter 11.0Chapter 11.0
Chapter 11.0
 
Chapter 10.2
Chapter 10.2Chapter 10.2
Chapter 10.2
 
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 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
 
Chapter 8.3
Chapter 8.3Chapter 8.3
Chapter 8.3
 
Chapter 5.2
Chapter 5.2Chapter 5.2
Chapter 5.2
 
Chapter 7.3
Chapter 7.3Chapter 7.3
Chapter 7.3
 

Recently uploaded

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 

Recently uploaded (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 

Chapter 8.2

  • 2. Defining Classes with Inheritance  Case Study:  Suppose we want implement a class roster that contains both undergraduate and graduate students.  Each student’s record will contain his or her name, three test scores, and the final course grade.  The formula for determining the course grade is different for graduate students than for undergraduate students. Undergrads: pass if avg test score >= 70 Grads: pass if avg test score >= 80
  • 3. Modeling Two Types of Students  There are two ways to design the classes to model undergraduate and graduate students.  We can define two unrelated classes, one for undergraduates and one for graduates.  We can model the two kinds of students by using classes that are related in an inheritance hierarchy.  Two classes are unrelated if they are not connected in an inheritance relationship.
  • 4. Classes for the Class Roster  For the Class Roster sample, we design three classes:  Student  UndergraduateStudent  GraduateStudent  The Student class will incorporate behavior and data common to both UndergraduateStudent and GraduateStudent objects.  The UndergraduateStudent class and the GraduateStudent class will each contain behaviors and data specific to their respective objects.
  • 6. Definition of GraduateStudent & UndergraduateStudent classes class GraduateStudent extends Student { //constructor not shown public void computeCourseGrade() { int total = 0; total = test1 + test2 + test3; if (total / 3 >= 80) { courseGrade = "Pass"; } else { courseGrade = "No Pass"; } } } class UndergraduateStudent extends Student { //Constructor not shown public void computeCourseGrade() { int total = 0; total = test1 + test2 + test3; if (total / 3 >= 70) { courseGrade = "Pass"; } else { courseGrade = "No Pass"; } } }
  • 7. Declaring a Subclass  A subclass inherits data and methods from the superclass. In the subclass, you can also:  Add new data  Add new methods  Override the methods of the superclass ○ Modify existing behaviour of parent
  • 8. Overriding vs. Overloading public class Test { public static void main(String[] args) { A a = new A(); a.p(10); } } class B { public void p(int i) { } } class A extends B { // This method overrides the method in B public void p(int i) { System.out.println(i); } } public class Test { public static void main(String[] args) { A a = new A(); a.p(10); } } class B { public void p(int i) { } } class A extends B { // This method overloads the method in B public void p(double i) { System.out.println(i); } }
  • 9. Inheritance Rules 1. The private members of the superclass are private to the superclass 2. The subclass can access the members of the superclass according to the accessibility rules 3. The subclass can include additional data and/or method members
  • 10. Inheritance Rules (continued) 4. The subclass can override, that is, redefine the methods of the superclass  The overriding method in subclass must have similar  Name  Parameter list  Return type 5. All members of the superclass are also members of the subclass  Similarly, the methods of the superclass (unless overridden) are also the methods of the subclass  Remember Rule 1 & 2 when accessing a member of the superclass in the subclass
  • 11. Inheritance Rules (continued) 6. (Using the Keyword super) The keyword super refers to the direct superclass of a subclass . This keyword can be used in two ways:  To call a superclass constructor  super(); //must be the first statement in subclass’s constructor  To call a superclass method  super.methodname();  this is only used if the subclass overrides the superclass method
  • 12. The Object Class is the Superclass of Every Java Class
  • 13. INHERITANCE: (Accessibility Modifier)  Sometimes , it is called visibility modifier  Not all properties can be accessed by sub class.  Super class can control a data accessing from subclass by giving the type of accessing to the members and methods.  A class can declare the data members or method as a public, private or protected.  If it is not declared, the data or method will be set to default type.
  • 14. INHERITANCE: Member Accessibility Accessibility criteria Modifier Same Class Same Package Subclass Universe private Yes No No No default Yes Yes No No protected Yes Yes Yes No public Yes Yes Yes Yes
  • 15. INHERITANCE: Data Accessibility Sub class B public int b protected int c Super class int a public int b protected int c private int d Sub class A int a public int b protected int c Package B Package A
  • 16. Refer to the previous slide  Super class has 2 subclasses : Subclass A and Subclass B.  Subclass A is defined in same package with superclass, subclass B is defined outside the package.  There are 4 accessibility data types: public, protected, private and default.  Subclass A can access all properties of superclass except private.  But, subclass B can only access the properties outside the package which are public and protected.
  • 17. Example: Visibility Modifiers public class C1 { public int x; protected int y; int z; private int u; protected void m() { } } public class C2 { C1 o = new C1(); can access o.x; can access o.y; can access o.z; cannot access o.u; can invoke o.m(); } public class C3 extends C1 { can access x; can access y; can access z; cannot access u; can invoke m(); } package p1; public class C4 extends C1 { can access x; can access y; cannot access z; cannot access u; can invoke m(); } package p2; public class C5 { C1 o = new C1(); can access o.x; cannot access o.y; cannot access o.z; cannot access o.u; cannot invoke o.m(); }
  • 18. What’s wrong with the code? How to fix it? class ClassX { private int m; public String toString() { return new String("(" + m + ")"); } } public class ClassY extends ClassX { private int n; public String toString() { return new String("(" + m + " , " + n + ")"); } } class TestAccesibility { public static void main(String [] args) { ClassX x = new ClassX; ClassY y = new ClassY; System.out.println("x = " + x); System.out.println("y = " + y); } }
  • 19. Inheritance and Constructors  Unlike members of a superclass, constructors of a superclass are not inherited by its subclasses.  You must define a constructor for a class or use the default constructor added by the compiler.  The statement super(); calls the superclass’s constructor.  super(); must be the first statement in the subclass contructor.
  • 20.  A call to the constructor of the superclass must be in the first statement in the child constructor. public Box(double l, double w, double h) { super(l,w); height = h; }
  • 21. Rectangle myRectangle = new Rectangle(5, 3); Box myBox = new Box(6, 5, 4);
  • 22. Superclass’s Constructor Is Always Invoked  A subclass constructor may invoke its superclass’s constructor. If none is invoked explicitly, the compiler puts super() as the first statement in the constructor. For example, the constructor of class A: public A(double d) { // some statements } is equivalent to public A(double d) { super(); // some statements } public A() { } is equivalent to public A() { super(); }
  • 23. Example on the Impact of a Superclass without no-arg Constructor  Find out the error in the program: class Fruit { public Fruit(String name) { System.out.println("Fruit constructor is invoked"); } } public class Apple extends Fruit { public Apple(String name) { System.out.println(“Apple constructor is invoked"); } }