This document summarizes key concepts from a lecture on inheritance in object-oriented programming. It discusses how inheritance allows classes to extend existing classes to reuse behavior while adding specialized behavior. Key points include: inheritance enables code reuse; subclasses can override methods of the parent class; subclasses inherit protected fields and methods from the parent; and the "super" keyword is used to call methods or constructors of the parent class from within the subclass. Polymorphism allows subclasses to be used where the parent class is expected due to late binding, which determines the actual method to call at runtime based on the object's type.
This document provides an overview of inheritance in object-oriented programming. It defines inheritance as a child class automatically inheriting variables and methods from its parent class. Benefits include reusability, where behaviors are defined once in the parent class and shared by all subclasses. The document discusses how to derive a subclass using the extends keyword, what subclasses can do with inherited and new fields and methods, and how constructors are called following the constructor calling chain. It also covers overriding and hiding methods and fields, type casting between subclasses and superclasses, and final classes and methods that cannot be extended or overridden.
Inheritance is the mechanism that allows programmers to create new classes from existing class. By using inheritance programmers can re-use code they've already written.
This document discusses inheritance in Java. It covers key concepts like subclasses inheriting fields and methods from superclasses, the "is-a" relationship between classes, overriding methods, abstract classes and methods, and interfaces. Polymorphism is also discussed, where a reference variable can refer to objects of its type as well as subclasses.
The document discusses inheritance in object-oriented programming. It defines inheritance as a form of code reuse where a new class inherits properties from an existing parent or superclass. The child class inherits methods and data from the parent class. Inheritance allows for polymorphism as child classes can override parent methods while also accessing parent functionality. The document provides examples of inheritance relationships and discusses key inheritance concepts like overriding, dynamic binding, and the use of the super keyword.
The document discusses inheritance in object-oriented programming. It defines inheritance as a mechanism where a subclass inherits properties and behaviors from its parent superclass. The key points are:
- A subclass inherits attributes and methods from its superclass. The subclass can also define its own attributes and methods or override existing superclass methods.
- There are two types of inheritance - single inheritance where a subclass inherits from one superclass, and multiple inheritance which is not supported in Java.
- Access modifiers like public, private, protected determine which members of the superclass are accessible to subclasses. Private members cannot be accessed by subclasses.
- The extends keyword is used to create a subclass that inherits from an existing superclass. The subclass inherits all non
Superclasses, and Subclasses, Overriding and Hiding Methods, Polymorphism, Inheritance Hierarchies, Super keyword, Final Classes and Methods, Abstract,
Classes and Methods, Nested classes & Inner Classes,
finalization and garbage collection.
This document discusses key object-oriented programming concepts including encapsulation, inheritance, polymorphism, abstract classes, and interfaces. It provides examples of how encapsulation hides implementation details and inheritance allows classes to inherit properties from superclasses. Polymorphism allows objects to take on multiple forms through inheritance. Abstract classes cannot be instantiated directly but provide a common definition that concrete subclasses implement. Interfaces define behaviors for classes to implement but do not provide implementations.
Know the difference between Inheritance and aggregation
Understand how inheritance is done in Java
Learn polymorphism through Method Overriding
Learn the keywords : super and final
Understand the basics of abstract class
This document provides an overview of polymorphism in object-oriented programming. It discusses:
1) Polymorphism allows the same method invocation to produce different results depending on the type of object, enabling "programming to the general type."
2) When a method is called through a superclass variable, the correct subclass version is called based on the object's type, not the variable type.
3) Polymorphism promotes extensibility by allowing new object types to respond to existing method calls without modifying existing code.
This document discusses polymorphism and object-oriented programming concepts. It begins by introducing polymorphism and how it allows the same method invocation to produce different results depending on the object type. It then discusses abstract classes and interfaces, explaining that abstract classes declare common attributes and behaviors for subclasses to implement, while interfaces define common method signatures without implementation. The document provides examples of polymorphism in class hierarchies, demonstrating how invoking methods through a superclass reference causes the correct subclass version to be called based on the object's type.
This document discusses inheritance in object-oriented programming. It defines inheritance as a way to create a subclass that extends the functionality of a superclass. Subclasses inherit fields and methods from the superclass and can override or add new functionality. Abstract classes and interfaces are also covered as ways to define common behaviors without implementing them. Polymorphism allows subclasses to be referenced by their superclass type.
This document discusses object-oriented programming concepts related to inheritance in Java. It defines inheritance as a way to create subclasses that extend existing superclasses, allowing the subclass to inherit attributes and behaviors of the superclass. The relationship between a superclass and subclass is described as an "is-a" relationship. The document provides examples of inheritance relationships between classes like GradedActivity and subclasses like FinalExam. It also covers key inheritance concepts like overriding methods, calling superclass constructors, protected members, and chains of inheritance.
The document provides an overview of object-oriented programming concepts in Java including inheritance, polymorphism, and abstraction. It discusses key inheritance concepts such as subclasses, superclasses, the extends keyword, and accessing members of parent classes. It also covers polymorphism through method overriding and abstract classes. The benefits and costs of inheritance are outlined as well as common forms of inheritance like generalization, specialization, and extension.
This document provides an overview of polymorphism and object-oriented programming concepts like abstract classes and interfaces. It includes code examples of an Employee hierarchy with abstract Employee class and concrete subclasses like SalariedEmployee and HourlyEmployee. The subclasses override the abstract earnings() method from the Employee class. The examples demonstrate polymorphic behavior by invoking earnings() on Employee references that point to subclass objects.
Object Oriented Programming_Chapter 3 (Two Lectures)
1- Let’s think on Inheritance
2- Let’s focus on Superclass’s Constructor
الكلية الجامعية للعلوم والتكنولوجيا - خان يونس
University college of science & technology
1. Inheritance in Java allows classes to extend other classes and interfaces to implement other interfaces. This allows code reuse and establishes type constraints.
2. Abstract classes can contain both abstract and non-abstract methods while interfaces contain only abstract methods. Interfaces establish contracts that implementing classes must follow.
3. When constructing objects with inheritance, superclass constructors are called before subclass constructors. Abstract classes and interfaces allow incomplete implementations to be extended.
- Inheritance allows one class to acquire properties and behaviors of another class. A subclass inherits from a superclass.
- Polymorphism allows one action to be performed in different ways. In Java it occurs through method overloading and overriding.
- Method overloading involves multiple methods with the same name but different parameters. Overriding involves a subclass method with same signature as the superclass method. Overriding enables polymorphism at runtime while overloading involves compile-time polymorphism.
The error is that the Fruit superclass does not have a no-arg constructor, but the Apple subclass constructor does not explicitly call a superclass constructor. When a subclass does not explicitly call a superclass constructor, the compiler automatically inserts a no-arg super() call. But since Fruit does not have a no-arg constructor, this causes a compile-time error.
To fix it, the Apple constructor needs to explicitly call the Fruit constructor:
public Apple(String name) {
super(name);
System.out.println("Apple constructor is invoked");
}
This chapter discusses decision making, object-oriented fundamentals, inner classes, and exception handling in Java. It describes the use of if and switch statements for decision making. It also covers access specifiers, encapsulation, inheritance, polymorphism, abstract classes, interfaces, inner classes, and exceptions.
This document summarizes key points about inheritance in Java from a chapter in a textbook. It discusses how a subclass inherits characteristics from its parent class, including methods and data. It also covers abstract classes, polymorphism through method overriding in subclasses, and how polymorphic references allow different methods to be called depending on the actual object type.
This document discusses polymorphism and inheritance in object-oriented programming. It covers key concepts such as overriding superclass methods in subclasses, calling superclass constructors during inheritance, accessing overridden superclass methods using the "super" keyword, and information hiding using private/protected access modifiers. The document also explains that subclasses cannot override methods declared as static, final, or in a final superclass.
Inheritance allows one class to extend another class, inheriting its attributes and behaviors. The subclass extends the superclass and inherits all of its variables and methods. The subclass can add additional fields and methods. A subclass object can be referenced by a superclass reference variable, allowing polymorphism through method overriding. Abstract classes define methods that subclasses must implement, without providing an implementation itself. The final keyword prevents inheritance or method overriding.
The document discusses inheritance and polymorphism in Java. It provides examples of inheritance using the "extends" keyword, polymorphism through method overloading and overriding, abstract classes and methods, interfaces, and the differences between interfaces and abstract classes. Key topics covered include defining subclasses that inherit attributes and methods from superclasses, adding new functionality or overriding existing functionality, accessing superclasses using the "super" keyword, and defining abstract and interface functionality without implementations.
First Review PPT gfinal gyft ftu liu yrfut goSowndarya6
CyberShieldX provides end-to-end security solutions, including vulnerability assessment, penetration testing, and real-time threat detection for business websites. It ensures that organizations can identify and mitigate security risks before exploitation.
Unlike traditional security tools, CyberShieldX integrates AI models to automate vulnerability detection, minimize false positives, and enhance threat intelligence. This reduces manual effort and improves security accuracy.
Many small and medium businesses lack dedicated cybersecurity teams. CyberShieldX provides an easy-to-use platform with AI-powered insights to assist non-experts in securing their websites.
Traditional enterprise security solutions are often expensive. CyberShieldX, as a SaaS platform, offers cost-effective security solutions with flexible pricing for businesses of all sizes.
Businesses must comply with security regulations, and failure to do so can result in fines or data breaches. CyberShieldX helps organizations meet compliance requirements efficiently.
More Related Content
Similar to Object oriented programming java inheritance (20)
This document discusses key object-oriented programming concepts including encapsulation, inheritance, polymorphism, abstract classes, and interfaces. It provides examples of how encapsulation hides implementation details and inheritance allows classes to inherit properties from superclasses. Polymorphism allows objects to take on multiple forms through inheritance. Abstract classes cannot be instantiated directly but provide a common definition that concrete subclasses implement. Interfaces define behaviors for classes to implement but do not provide implementations.
Know the difference between Inheritance and aggregation
Understand how inheritance is done in Java
Learn polymorphism through Method Overriding
Learn the keywords : super and final
Understand the basics of abstract class
This document provides an overview of polymorphism in object-oriented programming. It discusses:
1) Polymorphism allows the same method invocation to produce different results depending on the type of object, enabling "programming to the general type."
2) When a method is called through a superclass variable, the correct subclass version is called based on the object's type, not the variable type.
3) Polymorphism promotes extensibility by allowing new object types to respond to existing method calls without modifying existing code.
This document discusses polymorphism and object-oriented programming concepts. It begins by introducing polymorphism and how it allows the same method invocation to produce different results depending on the object type. It then discusses abstract classes and interfaces, explaining that abstract classes declare common attributes and behaviors for subclasses to implement, while interfaces define common method signatures without implementation. The document provides examples of polymorphism in class hierarchies, demonstrating how invoking methods through a superclass reference causes the correct subclass version to be called based on the object's type.
This document discusses inheritance in object-oriented programming. It defines inheritance as a way to create a subclass that extends the functionality of a superclass. Subclasses inherit fields and methods from the superclass and can override or add new functionality. Abstract classes and interfaces are also covered as ways to define common behaviors without implementing them. Polymorphism allows subclasses to be referenced by their superclass type.
This document discusses object-oriented programming concepts related to inheritance in Java. It defines inheritance as a way to create subclasses that extend existing superclasses, allowing the subclass to inherit attributes and behaviors of the superclass. The relationship between a superclass and subclass is described as an "is-a" relationship. The document provides examples of inheritance relationships between classes like GradedActivity and subclasses like FinalExam. It also covers key inheritance concepts like overriding methods, calling superclass constructors, protected members, and chains of inheritance.
The document provides an overview of object-oriented programming concepts in Java including inheritance, polymorphism, and abstraction. It discusses key inheritance concepts such as subclasses, superclasses, the extends keyword, and accessing members of parent classes. It also covers polymorphism through method overriding and abstract classes. The benefits and costs of inheritance are outlined as well as common forms of inheritance like generalization, specialization, and extension.
This document provides an overview of polymorphism and object-oriented programming concepts like abstract classes and interfaces. It includes code examples of an Employee hierarchy with abstract Employee class and concrete subclasses like SalariedEmployee and HourlyEmployee. The subclasses override the abstract earnings() method from the Employee class. The examples demonstrate polymorphic behavior by invoking earnings() on Employee references that point to subclass objects.
Object Oriented Programming_Chapter 3 (Two Lectures)
1- Let’s think on Inheritance
2- Let’s focus on Superclass’s Constructor
الكلية الجامعية للعلوم والتكنولوجيا - خان يونس
University college of science & technology
1. Inheritance in Java allows classes to extend other classes and interfaces to implement other interfaces. This allows code reuse and establishes type constraints.
2. Abstract classes can contain both abstract and non-abstract methods while interfaces contain only abstract methods. Interfaces establish contracts that implementing classes must follow.
3. When constructing objects with inheritance, superclass constructors are called before subclass constructors. Abstract classes and interfaces allow incomplete implementations to be extended.
- Inheritance allows one class to acquire properties and behaviors of another class. A subclass inherits from a superclass.
- Polymorphism allows one action to be performed in different ways. In Java it occurs through method overloading and overriding.
- Method overloading involves multiple methods with the same name but different parameters. Overriding involves a subclass method with same signature as the superclass method. Overriding enables polymorphism at runtime while overloading involves compile-time polymorphism.
The error is that the Fruit superclass does not have a no-arg constructor, but the Apple subclass constructor does not explicitly call a superclass constructor. When a subclass does not explicitly call a superclass constructor, the compiler automatically inserts a no-arg super() call. But since Fruit does not have a no-arg constructor, this causes a compile-time error.
To fix it, the Apple constructor needs to explicitly call the Fruit constructor:
public Apple(String name) {
super(name);
System.out.println("Apple constructor is invoked");
}
This chapter discusses decision making, object-oriented fundamentals, inner classes, and exception handling in Java. It describes the use of if and switch statements for decision making. It also covers access specifiers, encapsulation, inheritance, polymorphism, abstract classes, interfaces, inner classes, and exceptions.
This document summarizes key points about inheritance in Java from a chapter in a textbook. It discusses how a subclass inherits characteristics from its parent class, including methods and data. It also covers abstract classes, polymorphism through method overriding in subclasses, and how polymorphic references allow different methods to be called depending on the actual object type.
This document discusses polymorphism and inheritance in object-oriented programming. It covers key concepts such as overriding superclass methods in subclasses, calling superclass constructors during inheritance, accessing overridden superclass methods using the "super" keyword, and information hiding using private/protected access modifiers. The document also explains that subclasses cannot override methods declared as static, final, or in a final superclass.
Inheritance allows one class to extend another class, inheriting its attributes and behaviors. The subclass extends the superclass and inherits all of its variables and methods. The subclass can add additional fields and methods. A subclass object can be referenced by a superclass reference variable, allowing polymorphism through method overriding. Abstract classes define methods that subclasses must implement, without providing an implementation itself. The final keyword prevents inheritance or method overriding.
The document discusses inheritance and polymorphism in Java. It provides examples of inheritance using the "extends" keyword, polymorphism through method overloading and overriding, abstract classes and methods, interfaces, and the differences between interfaces and abstract classes. Key topics covered include defining subclasses that inherit attributes and methods from superclasses, adding new functionality or overriding existing functionality, accessing superclasses using the "super" keyword, and defining abstract and interface functionality without implementations.
First Review PPT gfinal gyft ftu liu yrfut goSowndarya6
CyberShieldX provides end-to-end security solutions, including vulnerability assessment, penetration testing, and real-time threat detection for business websites. It ensures that organizations can identify and mitigate security risks before exploitation.
Unlike traditional security tools, CyberShieldX integrates AI models to automate vulnerability detection, minimize false positives, and enhance threat intelligence. This reduces manual effort and improves security accuracy.
Many small and medium businesses lack dedicated cybersecurity teams. CyberShieldX provides an easy-to-use platform with AI-powered insights to assist non-experts in securing their websites.
Traditional enterprise security solutions are often expensive. CyberShieldX, as a SaaS platform, offers cost-effective security solutions with flexible pricing for businesses of all sizes.
Businesses must comply with security regulations, and failure to do so can result in fines or data breaches. CyberShieldX helps organizations meet compliance requirements efficiently.
Rigor, ethics, wellbeing and resilience in the ICT doctoral journeyYannis
The doctoral thesis trajectory has been often characterized as a “long and windy road” or a journey to “Ithaka”, suggesting the promises and challenges of this journey of initiation to research. The doctoral candidates need to complete such journey (i) preserving and even enhancing their wellbeing, (ii) overcoming the many challenges through resilience, while keeping (iii) high standards of ethics and (iv) scientific rigor. This talk will provide a personal account of lessons learnt and recommendations from a senior researcher over his 30+ years of doctoral supervision and care for doctoral students. Specific attention will be paid on the special features of the (i) interdisciplinary doctoral research that involves Information and Communications Technologies (ICT) and other scientific traditions, and (ii) the challenges faced in the complex technological and research landscape dominated by Artificial Intelligence.
A substation at an airport is a vital infrastructure component that ensures reliable and efficient power distribution for all airport operations. It acts as a crucial link, converting high-voltage electricity from the main grid to the lower voltages needed for various airport facilities. This essay will explore the functions, components, and importance of a substation at an airport.
Functions of an Airport Substation:
Voltage Conversion:
Substations step down high-voltage electricity to lower levels suitable for airport operations, like terminal buildings, runways, and other facilities.
Power Distribution:
They distribute electricity to various loads, including lighting, air conditioning, navigation systems, and ground support equipment.
Grid Stability:
Substations help maintain the stability of the power grid by controlling voltage levels and managing power flows.
Redundancy and Reliability:
Airports often have redundant substations or interconnected systems to ensure uninterrupted power supply, even in case of a fault.
Switching and Control:
Substations provide switching capabilities to connect or disconnect circuits, enabling maintenance and power management.
Protection:
Substations incorporate protective devices, like circuit breakers and relays, to safeguard the power system from faults and ensure safe operation.
Key Components of an Airport Substation:
Transformers: These convert high-voltage electricity to lower voltage levels.
Circuit Breakers: These devices switch circuits on or off, protecting the system from faults.
Busbars: These are large, conductive bars that distribute electricity from transformers to other equipment.
Switchgear: This includes equipment that controls the flow of electricity, such as isolators and switches.
Control and Protection Systems: These systems monitor the substation's performance, detect faults, and automatically initiate corrective actions.
Capacitors: These improve the power factor and reduce losses in the system.
Importance of Airport Substations:
Reliable Power Supply:
Substations are essential for providing reliable power to critical airport functions, ensuring safety and efficiency.
Safe and Efficient Operations:
They contribute to the safe and efficient operation of runways, terminals, and other airport facilities.
Airport Infrastructure:
Substations are an integral part of the airport's infrastructure, enabling various operations and services.
Economic Impact:
Substations support the economic activities of the airport, including passenger and cargo handling.
Modernization and Sustainability:
Modern substations incorporate advanced technologies and systems to improve efficiency, reduce energy consumption, and enhance sustainability.
In conclusion, an airport substation is a crucial component of airport infrastructure, ensuring reliable and efficient power distribution, grid stability, and safe operations.
This study will provide the audience with an understanding of the capabilities of soft tools such as Artificial Neural Networks (ANN), Support Vector Regression (SVR), Model Trees (MT), and Multi-Gene Genetic Programming (MGGP) as a statistical downscaling tool. Many projects are underway around the world to downscale the data from Global Climate Models (GCM). The majority of the statistical tools have a lengthy downscaling pipeline to follow. To improve its accuracy, the GCM data is re-gridded according to the grid points of the observed data, standardized, and, sometimes, bias-removal is required. The current work suggests that future precipitation can be predicted by using precipitation data from the nearest four grid points as input to soft tools and observed precipitation as output. This research aims to estimate precipitation trends in the near future (2021-2050), using 5 GCMs, for Pune, in the state of Maharashtra, India. The findings indicate that each one of the soft tools can model the precipitation with excellent accuracy as compared to the traditional method of Distribution Based Scaling (DBS). The results show that ANN models appear to give the best results, followed by MT, then MGGP, and finally SVR. This work is one of a kind in that it provides insights into the changing monsoon season in Pune. The anticipated average precipitation levels depict a rise of 300–500% in January, along with increases of 200-300% in February and March, and a 100-150% increase for April and December. In contrast, rainfall appears to be decreasing by 20-30% between June and September.
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODSsamueljackson3773
In this paper, the author discusses the concerns of using various wireless communications and how to use
them safely. The author also discusses the future of the wireless industry, wireless communication
security, protection methods, and techniques that could help organizations establish a secure wireless
connection with their employees. The author also discusses other essential factors to learn and note when
manufacturing, selling, or using wireless networks and wireless communication systems.
David Boutry - Mentors Junior DevelopersDavid Boutry
David Boutry is a Senior Software Engineer in New York with expertise in high-performance data processing and cloud technologies like AWS and Kubernetes. With over eight years in the field, he has led projects that improved system scalability and reduced processing times by 40%. He actively mentors aspiring developers and holds certifications in AWS, Scrum, and Azure.
2. Chapter Topics (1 of 2)
10.1 What Is Inheritance?
10.2 Calling the Superclass Constructor
10.3 Overriding Superclass Methods
10.4 Protected Members
10.5 Chains of Inheritance
10.6 The Object Class
3. Chapter Topics (2 of 2)
10.7 Polymorphism
10.8 Abstract Classes and Abstract Methods
10.9 Interfaces
10.10 Anonymous Classes
4. 10.1 What is Inheritance?
Generalization versus Specialization
• Real-life objects are typically specialized versions of other more general objects.
• The term “insect” describes a very general type of creature with numerous characteristics.
• Grasshoppers and bumblebees are insects
– They share the general characteristics of an insect.
– However, they have special characteristics of their own.
▪ grasshoppers have a jumping ability, and
▪ bumblebees have a stinger.
• Grasshoppers and bumblebees are specialized versions of an insect.
6. The “is a” Relationship (1 of 2)
• The relationship between a superclass and an inherited class
is called an “is a” relationship.
– A grasshopper “is a” insect.
– A poodle “is a” dog.
– A car “is a” vehicle.
• A specialized object has:
– all of the characteristics of the general object, plus
– additional characteristics that make it special.
• In object-oriented programming, inheritance is used to create
an “is a” relationship among classes.
7. The “is a” Relationship (2 of 2)
• We can extend the capabilities of a class.
• Inheritance involves a superclass and a subclass.
– The superclass is the general class and
– the subclass is the specialized class.
• The subclass is based on, or extended from, the superclass.
– Superclasses are also called base classes, and
– subclasses are also called derived classes.
• The relationship of classes can be thought of as parent
classes and child classes.
8. Inheritance (2 of 2)
• The subclass inherits fields and methods from the superclass
without any of them being rewritten.
• New fields and methods may be added to the subclass.
• The Java keyword, extends, is used on the class header to
define the subclass.
10. Inheritance, Fields and Methods
• Members of the superclass that are marked private:
– are not inherited by the subclass,
– exist in memory when the object of the subclass is created
– may only be accessed from the subclass by public methods of the
superclass.
• Members of the superclass that are marked public:
– are inherited by the subclass, and
– may be directly accessed from the subclass.
11. Inheritance, Fields and Methods (2 of 2)
• When an instance of the subclass is created, the non-
private methods of the superclass are available through
the subclass object.
• Non-private methods and fields of the superclass are
available in the subclass.
12. Inheritance and Constructors
• Constructors are not inherited.
• When a subclass is instantiated, the superclass default constructor is
executed first.
• Example:
– SuperClass1.java
– SubClass1.java
– ConstructorDemo1.java
13. The Superclass’s Constructor
• The super keyword refers to an object’s superclass.
• The superclass constructor can be explicitly called from the
subclass by using the super keyword.
• Example:
– Rectangle.java, Cube.java, CubeDemo.java
14. 10.2 Calling the Superclass Constructor
• If a parameterized constructor is defined in the
superclass,
– the superclass must provide a no-arg constructor, or
▪ subclasses must provide a constructor, and
▪ subclasses must call a superclass constructor.
• Calls to a superclass constructor must be the first java
statement in the subclass constructors.
15. 10.3 Overriding Superclass Methods (1 of 5)
• A subclass may have a method with the same signature as a superclass
method.
• The subclass method overrides the superclass method.
• This is known as method overriding.
• Example:
– GradedActivity.java, CurvedActivity.java, CurvedActivityDemo.java
17. 10.3 Overriding Superclass Methods (3 of 5)
• Recall that a method’s signature consists of:
– the method’s name
– the data types method’s parameters in the order that they appear.
• A subclass method that overrides a superclass method must have the same signature as the
superclass method.
• An object of the subclass invokes the subclass’s version of the method, not the superclass’s.
• The @Override annotation should be used just before the subclass method declaration.
– This causes the compiler to display a error message if the method fails to correctly
override a method in the superclass.
18. 10.3 Overriding Superclass Methods (4 of 5)
• A subclass method can call the overridden superclass method via the
super keyword.
• There is a distinction between overloading a method and overriding a
method.
• Overloading is when a method has the same name as one or more
other methods, but with a different signature.
• When a method overrides another method, however, they both have
the same signature.
19. 10.3 Overriding Superclass Methods (5 of 5)
• Both overloading and overriding can take place in an inheritance relationship.
• Overriding can only take place in an inheritance relationship.
• Example:
– SuperClass3.java,
– SubClass3.java,
– ShowValueDemo.java
20. Preventing a Method from Being Overridden
• The final modifier will prevent the overriding of a superclass
method in a subclass.
• If a subclass attempts to override a final method, the compiler
generates an error.
• This ensures that a particular superclass method is used by
subclasses rather than a modified version of it.
21. 10.4 Protected Members (1 of 2)
• Java provides a third access specification, protected.
• Protected members of class:
– may be accessed by methods in a subclass, and
– by methods in the same package as the class.
• A protected member’s access is somewhere between private
and public.
• Example:
– GradedActivity2.java
– FinalExam2.java
– ProtectedDemo.java
22. 10.4 Protected Members (2 of 2)
• Using protected instead of private makes some tasks easier.
• However, any class that is derived from the class, or is in the same package, has
unrestricted access to the protected member.
• It is always better to make all fields private and then provide public methods for
accessing those fields.
• If no access specifier for a class member is provided, the class member is given
package access by default.
• Any method in the same package may access the member.
23. Access Specifiers
Access Modifier
Accessible to a subclass inside
the same package?
Accessible to all other classes
inside the same package?
default (no modifier) Yes Yes
Public Yes Yes
Protected Yes Yes
Private No No
Access Modifier
Accessible to a subclass outside
the package?
Accessible to all other classes
outside the package?
default (no modifier) No No
Public Yes Yes
Protected Yes No
Private No No
24. 10.5 Chains of Inheritance
• Classes often are depicted graphically in a class hierarchy.
• A class hierarchy shows the inheritance relationships between
classes.
25. 10.6 The Object Class (1 of 2)
• All Java classes are directly or indirectly derived from a class named Object.
• Object is in the java.lang package.
• Any class that does not specify the extends keyword is automatically derived
from the Object class.
• Ultimately, every class is derived from the Object class.
26. 10.6 The Object Class (2 of 2)
• Because every class is directly or indirectly derived from the
Object class:
– every class inherits the Object class’s members.
▪ example: toString and equals.
• In the Object class, the toString method returns a string
containing the object’s class name and a hash of its memory
address.
• The equals method accepts the address of an object as its
argument and returns true if it is the same as the calling
object’s address.
• Example: ObjectMethods.java
27. 10.7 Polymorphism (1 of 4)
• A reference variable can reference objects of classes that are
derived from the variable’s class.
GradedActivity exam;
• We can use the exam variable to reference a GradedActivity
object.
• The GradedActivity class is also used as the superclass for the
FinalExam class.
• An object of the FinalExam class is a GradedActivity object.
28. 10.7 Polymorphism (2 of 4)
• A GradedActivity variable can be used to reference a
FinalExam object.
• This statement creates a FinalExam object and stores the object’s
address in the exam variable.
• This is an example of polymorphism.
• The term polymorphism means the ability to take many forms.
• In Java, a reference variable is polymorphic because it can
reference objects of types different from its own, as long as those
types are subclasses of its type.
29. 10.7 Polymorphism (3 of 4)
• Other legal polymorphic references:
• The GradedActivity class has three methods: setScore,
getScore, and getGrade.
• A GradedActivity variable can be used to call only those three
methods.
30. Polymorphism and Dynamic Binding
• If the object of the subclass has overridden a method in the
superclass:
– If the variable makes a call to that method the subclass’s version
of the method will be run.
• Java performs dynamic binding or late binding when a variable
contains a polymorphic reference.
• The Java Virtual Machine determines at runtime which method to
call, depending on the type of object that the variable references.
31. 10.7 Polymorphism (4 of 4)
• It is the object’s type, rather than the reference type, that
determines which method is called.
• Example:
– Polymorphic.java
• You cannot assign a superclass object to a subclass
reference variable.
32. 10.8 Abstract Classes
• An abstract class cannot be instantiated, but other
classes are derived from it.
• An Abstract class serves as a superclass for other
classes.
• The abstract class represents the generic or abstract
form of all the classes that are derived from it.
• A class becomes abstract when you place the abstract
key word in the class definition.
33. 10.8 Abstract Methods (1 of 2)
• An abstract method has no body and must be overridden in a subclass.
• An abstract method is a method that appears in a superclass, but expects to be
overridden in a subclass.
• An abstract method has only a header and no body.
• Example:
– Student.java, CompSciStudent.java,
CompSciStudentDemo.java
34. 10.8 Abstract Methods (2 of 2)
• Notice that the key word abstract appears in the header,
and that the header ends with a semicolon.
• Any class that contains an abstract method is automatically
abstract.
• If a subclass fails to override an abstract method, a compiler
error will result.
• Abstract methods are used to ensure that a subclass
implements the method.
35. 10.9 Interfaces (1 of 3)
• An interface is similar to an abstract class that has all abstract
methods.
– It cannot be instantiated, and
– all of the methods listed in an interface must be written elsewhere.
• The purpose of an interface is to specify behavior for other classes.
• It is often said that an interface is like a “contract,” and when a class
implements an interface it must adhere to the contract.
• An interface looks similar to a class, except:
– the keyword interface is used instead of the keyword class,
and
– the methods that are specified in an interface have no bodies, only
headers that are terminated by semicolons.
36. 10.9 Interfaces (2 of 3)
• The general format of an interface definition:
• All methods specified by an interface are public by default.
• A class can implement one or more interfaces.
37. 10.9 Interfaces (3 of 3)
• If a class implements an interface, it uses the implements
keyword in the class header.
• Example:
– GradedActivity.java
– Relatable.java
– FinalExam3.java
– InterfaceDemo.java
38. Fields in Interfaces
• An interface can contain field declarations:
– all fields in an interface are treated as final and static.
• Because they automatically become final, you must provide an
initialization value.
• In this interface, FIELD1 and FIELD2 are final static int
variables.
• Any class that implements this interface has access to these
variables.
39. Implementing Multiple Interfaces
• A class can be derived from only one superclass.
• Java allows a class to implement multiple interfaces.
• When a class implements multiple interfaces, it must provide
the methods specified by all of them.
• To specify multiple interfaces in a class definition, simply list
the names of the interfaces, separated by commas, after the
implements key word.
41. Polymorphism with Interfaces (1 of 3)
• Java allows you to create reference variables of an interface type.
• An interface reference variable can reference any object that implements that
interface, regardless of its class type.
• This is another example of polymorphism.
• Example:
– RetailItem.java
– CompactDisc.java
– DvdMovie.java
– PolymorphicInterfaceDemo.java
42. Polymorphism with Interfaces (2 of 3)
• In the example code, two RetailItem reference variables, item1 and item2, are
declared.
• The item1 variable references a CompactDisc object and the item2 variable
references a DvdMovie object.
• When a class implements an interface, an inheritance relationship known as
interface inheritance is established.
– a CompactDisc object is a RetailItem, and
– a DvdMovie object is a RetailItem.
43. Polymorphism with Interfaces (3 of 3)
• A reference to an interface can point to any class that
implements that interface.
• You cannot create an instance of an interface.
• When an interface variable references an object:
– only the methods declared in the interface are available,
– explicit type casting is required to access the other
methods of an object referenced by an interface reference.
44. Default Methods
• Beginning in Java 8, interfaces may have default methods.
• A default method is an interface method that has a body.
• You can add new methods to an existing interface without
causing errors in the classes that already implement the
interface.
• Example:
– Displayable.java
– Person.java
– InterfaceDemoDefaultMethod.java
45. 10.10 Anonymous Inner Classes
• An inner class is a class that is defined inside another class.
• An anonymous inner class is an inner class that has no name.
• An anonymous inner class must implement an interface, or extend another class.
• Useful when you need a class that is simple, and to be instantiated only once in your
code.
• Example:
– IntCalculator.java
– AnonymousClassDemo.java