SlideShare a Scribd company logo
UNIT-II INHERITANCES,PACKAGES AND INTERFACE
Overloading Methods – Objects as Parameters – Returning Objects
–Static, Nested and Inner Classes. Inheritance: Basics– Types of
Inheritance -Super keyword -Method Overriding – Dynamic Method
Dispatch –Abstract Classes – final with Inheritance. Packages and
Interfaces: Packages – Packages and Member Access –Importing
Packages – Interfaces.
Overloading Methods
 Method Overloading is a feature in Java that allows a class to have
more than one methods having same name, but with different
signatures (Parameters).
 (Each method must have different number of parameters or
parameters having different types and orders).
Three ways to overload a method
1. Number of parameters. (Different number of parameters in argument list)
• For example: This is a valid case of overloading
• add(int, int)
• add(int, int, int)
2. Data type of parameters. (Difference in data type of parameters)
• For example:
• add(int, int)
• add(int, float)
3. Sequence of Data type of parameters.
• For example:
• add(int, float)
• add(float, int)
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
IN NOTEPAD
Method Overloading with Type
Promotion:
• The name Type Promotion specifies that a small size
datatype can be promoted to a large size datatype. i.e., an
Integer data type can be promoted to long, float, double, etc.
• This Automatic Type Promotion is done when any method
which accepts a higher size data type argument is called with
the smaller data type.
object oriented programming unit two ppt
object oriented programming unit two ppt
Objects as parameters
Objects as parameters
Return
Objects
Return
Objects
STATIC, NESTED & INNER CLASSES
• An inner class is a class that is defined inside another class.
SYNTAX: FOR DECLARING INNER CLASSES
[modifier] class OuterClassName
{
---- Code -----
[modifier] class InnerClassName
{
---- Code ----
}
}
 It is possible to define a class within another class; such classes are known as
nested classes.
 if class B is defined within class A, then B does not exist independently of A.
 There are two types of nested classes: static and non-static.
 Static nested class is one that has the static modifier applied.
 Because it is static, it must access the non-static members of its enclosing class
through an object.
 That is, it cannot refer to non-static members of its enclosing class directly.
 The most important type of nested class is the inner class.
 An inner class is a non-static nested class.
 It has access to all of the variables and methods of its outer class and may
refer to them directly in the same way that other non-static members of the
outer class do.
Non-Static Inner Class
Access Outer Class From Inner Class
• We can access attributes and methods of the
inner class.
Static Inner Class
• Inner class also be a static class,which means when you
access it without creating an objects for inner class.
object oriented programming unit two ppt
Inheritance
• Inheritance is a process of deriving a new class from existing class, also
called as “extending a class”.
• The class whose property is being inherited by another class is called
“base class” (or) “parent class” (or) “super class”.
• The class that inherits a particular property or a set of properties from
the base class is called “derived class” (or) “child class” (or) “sub class”.
object oriented programming unit two ppt
• ADVANTAGES OF INHERITANCE:
• Reusability of Code:
• Inheritance is mainly used for code reusability (Code reusability means
that we can add extra features to an existing class without modifying it).
• Effort and Time Saving:
• The advantage of reusability saves the programmer time and effort. Since
the main code written can be reused in various situations as needed.
• Increased Reliability:
• The program with inheritance becomes more understandable and easily
maintainable as the sub classes are created from the existing reliably
working classes.
“extends” KEYWORD:
• Inheriting a class means creating a new class as an extension of another
class.
• The extends keyword is used to inherit a class from existing class.
• The general form of a class declaration that inherits a superclass is shown
here:
•  Syntax:
Characteristics of Class Inheritance:
1. A class cannot be inherited from more than one base
class. Java does not support multiple inheritance
2. Sub class can access only the non-private members
of the super class.
3. Private data members of a super class are local only
to that class. Therefore, they can’t be accessed outside
the super class, even sub classes can’t access the private
members.
4. Protected features in Java are visible to all subclasses
as well as all other classes in the same package.
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
TYPES OF INHERITACE:
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
object oriented programming unit two ppt
object oriented programming unit two ppt
SINGLE INHERITANCE
• The process of creating only one subclass from
only one super class is known as Single
Inheritance.
• Only two classes are involved in this inheritance.
• The subclass can access all the members of
super class.
Example: Animal -> Dog
MULTILEVEL INHERITANCE
• The process of creating a new sub class from an already
inherited sub class is known as Multilevel Inheritance.
• Multiple classes are involved in inheritance, but one
class extends only one.
• The lowermost subclass can make use of all its super
classes' members.
• Multilevel inheritance is an indirect way of
implementing multiple inheritance.
Example: Animal ->Dog -> BabyDog
object oriented programming unit two ppt
HIERARCHICAL INHERITANCE
object oriented programming unit two ppt
Hybrid Inheritance
• In Hierarchical Inheritance, one class serves as a superclass
(base class) for more than one subclass.
• Class A serves as a base class for the derived classes B, C, and D.
object oriented programming unit two ppt
object oriented programming unit two ppt
Super keyword
• Super keyword is refer to the super class(parent class) object.
• It is used to call superclass methods, and to access the superclass
constructor.
• The super keyword is used to access the parent class object and is
based on the concept of inheritance in Java.
• The most common use of the super keyword is to eliminate the
confusion between superclasses and subclasses that have methods
with same name.
• super keyword is used for the following three purposes:
1. To invoke superclass constructor.
2. To invoke superclass members variables.
3. To invoke superclass methods.
Without Super keyword
Call Superclass Method
Access Attributes of the Superclass
Use of super() to access superclass constructor
Method overriding
• If subclass (child class) has the same method as
declared in the parent class, it is known as method
overriding in Java.
• Rules for Java Method Overriding
1. The method must have the same name as in the
parent class
2. The method must have the same parameter as in
the parent class.
3. There must be an (inheritance).
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
Dynamic method dispatch
• Dynamic method dispatch is the mechanism
by which a call to an overridden method is
resolved at run time, rather than compile time.
• Dynamic method dispatch is important
because this is how Java implements run-time
polymorphism
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
ABSTRACT CLASSES
• Abstraction is a process of hiding the
implementation details and showing only the
essential features to the user.
• For example: sending sms, you just type the text
and send the message. You don't know the
internal processing about the message delivery.
• Keyword for abstract-”abstract”
• It contains
 Abstract class-(a class contains atleast one abstract
method).
 Abstract method(methods having only declaration inside
the class)-definition will be in derived class.
 Concrete class/method-(class which doesn’t contain any
abstract method/class)
• We can’t create object for abstract class.
object oriented programming unit two ppt
Rules for abstract class
• An abstract class must be declared with an abstract
keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated i.e.,object of such class cannot be
created directly using new keyword. .
• It can have constructors and static methods also.
• It can have final methods which will force the subclass not to
change the body of the method.
Syntax for abstract class
Syntax for abstract method
Rules for Abstract Methods
• The abstract keyword is also used to declare a
method as abstract.
• An abstract method consists of a parameters, but
no method body.
• Abstract method would have no definition, and its
parameters is followed by a semicolon, not curly
braces
Example for abstract class
abstract class shape
{
int x,y; //member declaration
void draw() //concrete method
{
System.out.println(“draw”);
}
abstract void draw(); //abstract method
}
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
FINAL WITH INHERITANCE
• Final is a keyword or reserved word in java used for restricting some
functionality.
• It can be applied to member variables, methods, class and local
variables in Java.
• final keyword has three uses:
1. For declaring variable – to create a named constant. A final
variable cannot be changed once it is initialized.
2. For declaring the methods – to prevent method overriding. A
final method cannot be overridden by subclasses.
3. For declaring the class – to prevent a class from inheritance. A
final class cannot be inherited.
1. Final Variable:
• Any variable either member variable or local
variable (declared inside method) modified by final
keyword is called final variable.
• Syntax:
• The final variable can be assigned only once.
• The value of the final variable will not be changed during the
execution of the program.
• If an attempt is made to alter the final variable value, the
java compiler will throw an error message.
• There is a final variable speedlimit, we are going to change the value of
this variable, but It can't be changed because final variable once
assigned a value can never be changed.
2. Final Methods:
• Final keyword in java can also be applied to methods.
• A java method with final keyword is called final method and it cannot
be overridden in sub-class.
• If a method is defined with final keyword, it cannot be overridden in the
subclass and its behaviour should remain constant in sub-classes.
object oriented programming unit two ppt
3. Final Classes:
• Java class with final modifier is called final class in Java and they cannot
be sub-classed or inherited.
object oriented programming unit two ppt
Points to Remember
1) A constructor cannot be declared as final
2) We cannot change the value of a final variable.
3) A final method cannot be overridden.
4) A final class cannot be inherited.
5) If method parameters are declared final then the value of
these parameters cannot be changed.
PACKAGES
• A Package can be defined as a collection of classes,
interfaces, enumerations providing access protection and
name space management.
Package can be categorized in two form:
1. Built-in package
2. user-defined package.
object oriented programming unit two ppt
CREATING USER DEFINED PACKAGES:
• Java package created by user to categorize their project's
classes and interface are known as user-defined packages.
• When creating a package, you should choose a name for the
package.
• Put a package statement with that name at the top of every
source file that contains the classes and interfaces.
• The package statement should be the first line in the source
file.
• There can be only one package statement in each source file
Steps involved in creating user-defined package:
• Include package statement along with the
package name as the first statement in the
program.
• Write class declarations.
• Save the file in this directory as “name of
class.java”.
• Compile this file using java compiler.
Syntax
package packagename;
Class classname
{
………………
}
To create the above package,
1. Create a directory called pack.
2. Open a new file and enter the code given
above.
3. Save the file as class1.java in the directory.
4. A package called pack has now been created
which contains one class class1.
object oriented programming unit two ppt
Output:
HEllo
object oriented programming unit two ppt
ACCESSING A PACKAGE (using “import” keyword):
• The import keyword is used to make the classes and interface
of another package accessible to the current package.
• There are three ways to access the package
from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
Using packagename.*
• If you use package.* then all the classes and interfaces of this
package will be accessible but not subpackages.
Using packagename.classname
• If you import package.classname then only declared class of this
package will be accessible.
Using fully qualified name
• If you use fully qualified name then only declared class of this
package will be accessible.
• Now there is no need to import. But you need to use fully qualified
name every time when you are accessing the class or interface.
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
PACKAGES AND MEMBER ACCESS
• Access level modifiers determine whether
other classes can use a particular field or
invoke a particular method.
• There are two levels of access control:
• At the top level— public, or package-private
At the member level—public, private,
protected, or package-private
Top Level access control:
• A class may be declared with the modifier public, in
which case that class is visible to all classes
everywhere.
• If a class has no modifier (the default, also known
as package-private), it is visible only within its own
package.
Member Level access control:
• public – if a member is declared with public, it is visible
and accessible to all classes everywhere.
• private - The private modifier specifies that the member
can only be accessed in its own class.
• protected - The protected modifier specifies that the
member can only be accessed within its own package
and, in addition, by a subclass of its class in another
package.
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
INTERFACES
• “interface” is a keyword which is used to achieve full
abstraction. Using interface, we can specify what the class
must do but not how it does.
• An interface is a collection of method definitions (without
implementations) and constant values.
• It is a blueprint of a class.
• It has static constants and abstract methods.
• An interface is similar to a class in the following
ways:
• An interface can contain any number of methods.
• An interface is written in a file with a .java
extension, with the name of the interface matching
the name of the file.
• The bytecode of an interface appears in a .class file.
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
Where,
• Access_specifer : either public or none.
• Name: name of an interface can be any valid java identifier.
• Variables: They are implicitly public, final and static,
meaning that they cannot be changed by the implementing
class. They must be initialized with a constant value.
• Methods: They are implicitly public and abstract, meaning
that they must be declared without body and defined only by
the implementing class.
object oriented programming unit two ppt
object oriented programming unit two ppt
Implementing Interfaces (“implements” keyword):
• Once an interface has been defined, one or
more classes can implement that interface.
• A class uses the implements keyword to
implement an interface.
• The implements keyword appears in the class
declaration following the extends portion of
the declaration.
Implementing Interfaces
• The general form of a class that includes the
implements clause looks like this:
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
object oriented programming unit two ppt
Nested Interfaces
• An interface can be declared a member of a
class or another interface.
• Such an interface is called a member interface
or a nested interface.
• A nested interface can be declared as public,
private, or protected.
object oriented programming unit two ppt

More Related Content

Similar to object oriented programming unit two ppt (20)

OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
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
AshwathGupta
 
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
NithyaN19
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
rayanbabur
 
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
markbrianBautista
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxObject oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
Topic inheritance
Topic  inheritanceTopic  inheritance
Topic inheritance
Ankit Kumar
 
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptxInheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
Unit 4
Unit 4Unit 4
Unit 4
LOVELY PROFESSIONAL UNIVERSITY
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
RumiHossain5
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2ee
Shiva Cse
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
Thakur Amit Tomer
 
OOPS – General Understanding in .NET
OOPS – General Understanding in .NETOOPS – General Understanding in .NET
OOPS – General Understanding in .NET
Sabith Byari
 
Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
raksharao
 
Chapter 03 enscapsulation
Chapter 03 enscapsulationChapter 03 enscapsulation
Chapter 03 enscapsulation
Nurhanna Aziz
 
Unit 3
Unit 3Unit 3
Unit 3
LOVELY PROFESSIONAL UNIVERSITY
 
27csharp
27csharp27csharp
27csharp
Sireesh K
 
27c
27c27c
27c
Sireesh K
 
Software enginnnering introduction (2).pptx
Software enginnnering introduction (2).pptxSoftware enginnnering introduction (2).pptx
Software enginnnering introduction (2).pptx
parmidakhrz02
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078
Aravind NC
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
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
AshwathGupta
 
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
NithyaN19
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
rayanbabur
 
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
markbrianBautista
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxObject oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
Topic inheritance
Topic  inheritanceTopic  inheritance
Topic inheritance
Ankit Kumar
 
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptxInheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2ee
Shiva Cse
 
OOPS – General Understanding in .NET
OOPS – General Understanding in .NETOOPS – General Understanding in .NET
OOPS – General Understanding in .NET
Sabith Byari
 
Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
raksharao
 
Chapter 03 enscapsulation
Chapter 03 enscapsulationChapter 03 enscapsulation
Chapter 03 enscapsulation
Nurhanna Aziz
 
Software enginnnering introduction (2).pptx
Software enginnnering introduction (2).pptxSoftware enginnnering introduction (2).pptx
Software enginnnering introduction (2).pptx
parmidakhrz02
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078
Aravind NC
 

Recently uploaded (20)

THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdfForestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
ChalaKelbessa
 
la storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglesela storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglese
LetiziaLucente
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in IndiaSmart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
fincrifcontent
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
Quiz Club of PSG College of Arts & Science
 
How to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRMHow to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRM
Celine George
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
How to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time OffHow to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time Off
Celine George
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
National Information Standards Organization (NISO)
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdfForestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
ChalaKelbessa
 
la storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglesela storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglese
LetiziaLucente
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in IndiaSmart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
fincrifcontent
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
How to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRMHow to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRM
Celine George
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
How to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time OffHow to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time Off
Celine George
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
Ad

object oriented programming unit two ppt

  • 1. UNIT-II INHERITANCES,PACKAGES AND INTERFACE Overloading Methods – Objects as Parameters – Returning Objects –Static, Nested and Inner Classes. Inheritance: Basics– Types of Inheritance -Super keyword -Method Overriding – Dynamic Method Dispatch –Abstract Classes – final with Inheritance. Packages and Interfaces: Packages – Packages and Member Access –Importing Packages – Interfaces.
  • 2. Overloading Methods  Method Overloading is a feature in Java that allows a class to have more than one methods having same name, but with different signatures (Parameters).  (Each method must have different number of parameters or parameters having different types and orders).
  • 3. Three ways to overload a method 1. Number of parameters. (Different number of parameters in argument list) • For example: This is a valid case of overloading • add(int, int) • add(int, int, int) 2. Data type of parameters. (Difference in data type of parameters) • For example: • add(int, int) • add(int, float)
  • 4. 3. Sequence of Data type of parameters. • For example: • add(int, float) • add(float, int)
  • 9. Method Overloading with Type Promotion: • The name Type Promotion specifies that a small size datatype can be promoted to a large size datatype. i.e., an Integer data type can be promoted to long, float, double, etc. • This Automatic Type Promotion is done when any method which accepts a higher size data type argument is called with the smaller data type.
  • 16. STATIC, NESTED & INNER CLASSES • An inner class is a class that is defined inside another class. SYNTAX: FOR DECLARING INNER CLASSES [modifier] class OuterClassName { ---- Code ----- [modifier] class InnerClassName { ---- Code ---- } }
  • 17.  It is possible to define a class within another class; such classes are known as nested classes.  if class B is defined within class A, then B does not exist independently of A.  There are two types of nested classes: static and non-static.  Static nested class is one that has the static modifier applied.  Because it is static, it must access the non-static members of its enclosing class through an object.  That is, it cannot refer to non-static members of its enclosing class directly.  The most important type of nested class is the inner class.  An inner class is a non-static nested class.  It has access to all of the variables and methods of its outer class and may refer to them directly in the same way that other non-static members of the outer class do.
  • 19. Access Outer Class From Inner Class • We can access attributes and methods of the inner class.
  • 20. Static Inner Class • Inner class also be a static class,which means when you access it without creating an objects for inner class.
  • 22. Inheritance • Inheritance is a process of deriving a new class from existing class, also called as “extending a class”. • The class whose property is being inherited by another class is called “base class” (or) “parent class” (or) “super class”. • The class that inherits a particular property or a set of properties from the base class is called “derived class” (or) “child class” (or) “sub class”.
  • 24. • ADVANTAGES OF INHERITANCE: • Reusability of Code: • Inheritance is mainly used for code reusability (Code reusability means that we can add extra features to an existing class without modifying it). • Effort and Time Saving: • The advantage of reusability saves the programmer time and effort. Since the main code written can be reused in various situations as needed. • Increased Reliability: • The program with inheritance becomes more understandable and easily maintainable as the sub classes are created from the existing reliably working classes.
  • 25. “extends” KEYWORD: • Inheriting a class means creating a new class as an extension of another class. • The extends keyword is used to inherit a class from existing class. • The general form of a class declaration that inherits a superclass is shown here: •  Syntax:
  • 26. Characteristics of Class Inheritance: 1. A class cannot be inherited from more than one base class. Java does not support multiple inheritance 2. Sub class can access only the non-private members of the super class. 3. Private data members of a super class are local only to that class. Therefore, they can’t be accessed outside the super class, even sub classes can’t access the private members. 4. Protected features in Java are visible to all subclasses as well as all other classes in the same package.
  • 35. TYPES OF INHERITACE: 1. Single Inheritance 2. Multilevel Inheritance 3. Multiple Inheritance 4. Hierarchical Inheritance 5. Hybrid Inheritance
  • 38. SINGLE INHERITANCE • The process of creating only one subclass from only one super class is known as Single Inheritance. • Only two classes are involved in this inheritance. • The subclass can access all the members of super class.
  • 40. MULTILEVEL INHERITANCE • The process of creating a new sub class from an already inherited sub class is known as Multilevel Inheritance. • Multiple classes are involved in inheritance, but one class extends only one. • The lowermost subclass can make use of all its super classes' members. • Multilevel inheritance is an indirect way of implementing multiple inheritance.
  • 41. Example: Animal ->Dog -> BabyDog
  • 45. Hybrid Inheritance • In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. • Class A serves as a base class for the derived classes B, C, and D.
  • 48. Super keyword • Super keyword is refer to the super class(parent class) object. • It is used to call superclass methods, and to access the superclass constructor. • The super keyword is used to access the parent class object and is based on the concept of inheritance in Java. • The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with same name. • super keyword is used for the following three purposes: 1. To invoke superclass constructor. 2. To invoke superclass members variables. 3. To invoke superclass methods.
  • 51. Access Attributes of the Superclass
  • 52. Use of super() to access superclass constructor
  • 53. Method overriding • If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. • Rules for Java Method Overriding 1. The method must have the same name as in the parent class 2. The method must have the same parameter as in the parent class. 3. There must be an (inheritance).
  • 62. Dynamic method dispatch • Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. • Dynamic method dispatch is important because this is how Java implements run-time polymorphism
  • 67. ABSTRACT CLASSES • Abstraction is a process of hiding the implementation details and showing only the essential features to the user. • For example: sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery.
  • 68. • Keyword for abstract-”abstract” • It contains  Abstract class-(a class contains atleast one abstract method).  Abstract method(methods having only declaration inside the class)-definition will be in derived class.  Concrete class/method-(class which doesn’t contain any abstract method/class) • We can’t create object for abstract class.
  • 70. Rules for abstract class • An abstract class must be declared with an abstract keyword. • It can have abstract and non-abstract methods. • It cannot be instantiated i.e.,object of such class cannot be created directly using new keyword. . • It can have constructors and static methods also. • It can have final methods which will force the subclass not to change the body of the method.
  • 73. Rules for Abstract Methods • The abstract keyword is also used to declare a method as abstract. • An abstract method consists of a parameters, but no method body. • Abstract method would have no definition, and its parameters is followed by a semicolon, not curly braces
  • 74. Example for abstract class abstract class shape { int x,y; //member declaration void draw() //concrete method { System.out.println(“draw”); } abstract void draw(); //abstract method }
  • 79. FINAL WITH INHERITANCE • Final is a keyword or reserved word in java used for restricting some functionality. • It can be applied to member variables, methods, class and local variables in Java. • final keyword has three uses: 1. For declaring variable – to create a named constant. A final variable cannot be changed once it is initialized. 2. For declaring the methods – to prevent method overriding. A final method cannot be overridden by subclasses. 3. For declaring the class – to prevent a class from inheritance. A final class cannot be inherited.
  • 80. 1. Final Variable: • Any variable either member variable or local variable (declared inside method) modified by final keyword is called final variable. • Syntax:
  • 81. • The final variable can be assigned only once. • The value of the final variable will not be changed during the execution of the program. • If an attempt is made to alter the final variable value, the java compiler will throw an error message.
  • 82. • There is a final variable speedlimit, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.
  • 83. 2. Final Methods: • Final keyword in java can also be applied to methods. • A java method with final keyword is called final method and it cannot be overridden in sub-class. • If a method is defined with final keyword, it cannot be overridden in the subclass and its behaviour should remain constant in sub-classes.
  • 85. 3. Final Classes: • Java class with final modifier is called final class in Java and they cannot be sub-classed or inherited.
  • 87. Points to Remember 1) A constructor cannot be declared as final 2) We cannot change the value of a final variable. 3) A final method cannot be overridden. 4) A final class cannot be inherited. 5) If method parameters are declared final then the value of these parameters cannot be changed.
  • 88. PACKAGES • A Package can be defined as a collection of classes, interfaces, enumerations providing access protection and name space management. Package can be categorized in two form: 1. Built-in package 2. user-defined package.
  • 90. CREATING USER DEFINED PACKAGES: • Java package created by user to categorize their project's classes and interface are known as user-defined packages. • When creating a package, you should choose a name for the package. • Put a package statement with that name at the top of every source file that contains the classes and interfaces. • The package statement should be the first line in the source file. • There can be only one package statement in each source file
  • 91. Steps involved in creating user-defined package: • Include package statement along with the package name as the first statement in the program. • Write class declarations. • Save the file in this directory as “name of class.java”. • Compile this file using java compiler.
  • 93. To create the above package, 1. Create a directory called pack. 2. Open a new file and enter the code given above. 3. Save the file as class1.java in the directory. 4. A package called pack has now been created which contains one class class1.
  • 97. ACCESSING A PACKAGE (using “import” keyword): • The import keyword is used to make the classes and interface of another package accessible to the current package.
  • 98. • There are three ways to access the package from outside the package. 1. import package.*; 2. import package.classname; 3. fully qualified name.
  • 99. Using packagename.* • If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages. Using packagename.classname • If you import package.classname then only declared class of this package will be accessible. Using fully qualified name • If you use fully qualified name then only declared class of this package will be accessible. • Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface.
  • 103. PACKAGES AND MEMBER ACCESS • Access level modifiers determine whether other classes can use a particular field or invoke a particular method. • There are two levels of access control: • At the top level— public, or package-private At the member level—public, private, protected, or package-private
  • 104. Top Level access control: • A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. • If a class has no modifier (the default, also known as package-private), it is visible only within its own package.
  • 105. Member Level access control: • public – if a member is declared with public, it is visible and accessible to all classes everywhere. • private - The private modifier specifies that the member can only be accessed in its own class. • protected - The protected modifier specifies that the member can only be accessed within its own package and, in addition, by a subclass of its class in another package.
  • 110. INTERFACES • “interface” is a keyword which is used to achieve full abstraction. Using interface, we can specify what the class must do but not how it does. • An interface is a collection of method definitions (without implementations) and constant values. • It is a blueprint of a class. • It has static constants and abstract methods.
  • 111. • An interface is similar to a class in the following ways: • An interface can contain any number of methods. • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file. • The bytecode of an interface appears in a .class file.
  • 115. Where, • Access_specifer : either public or none. • Name: name of an interface can be any valid java identifier. • Variables: They are implicitly public, final and static, meaning that they cannot be changed by the implementing class. They must be initialized with a constant value. • Methods: They are implicitly public and abstract, meaning that they must be declared without body and defined only by the implementing class.
  • 118. Implementing Interfaces (“implements” keyword): • Once an interface has been defined, one or more classes can implement that interface. • A class uses the implements keyword to implement an interface. • The implements keyword appears in the class declaration following the extends portion of the declaration.
  • 119. Implementing Interfaces • The general form of a class that includes the implements clause looks like this:
  • 125. Nested Interfaces • An interface can be declared a member of a class or another interface. • Such an interface is called a member interface or a nested interface. • A nested interface can be declared as public, private, or protected.