SlideShare a Scribd company logo
1 of 15
Download to read offline
Inheritance, Interfaces, Polymorphism, Method Overriding
When you want to create a new class and there is already a class that includes some of the
code that you want, you can derive your new class from the existing class. In doing this, you
can reuse the fields and methods of the existing class without having to write. This is
known as Inheritance.For example:
class A {
int x;
long y;
void showValues( ) {
System.out.print(“x: ”+x+” y”+y);
}
void setValues(int a, long b) {
x=a;
y=b;
}
}
Here, B extends A, therefore B inherits all functions and member variables from A
class B extends A {
String z;
void showString( ) {
System.out.print(“z: ”+z);
}
void setString(String s) {
z=s;
}
}
A class that is derived from another class is called a subclass (also a derived class, extended
class, or child class). The class from which the subclass is derived is called a superclass (also
a base class or a parent class).
In previous example, class A is superclass (also a base class or a parent class).
class B is subclass(also a derived class, extended class, or child class)
A subclass inherits all the members (fields, methods, and nested classes) from its
superclass. Constructors are not members, so they are not inherited by subclasses, but the
constructor of the superclass can be invoked from the subclass.
to refer to superclass we use the keyword: super
class a class b extends a
{ {
int a=5; int a=10;
} void show()
{ System.out.println(“a of child class: “+a);
System.out.println(“ a of super class: “+super.a);
}
}
output: a of child class: 10
a of super class: 5
Same class Other class in
same
package
Child class in
same
package
Other class in
same
package
Child class in
other
package
private Yes No No No No
default Yes Yes Yes No No
protected Yes Yes Yes No Yes
public Yes Yes Yes Yes Yes
Figure: Accessibility of variable or function when using particular access-specifier
class a
{
int a=5;
a(int value)
{
a=value;
}
}
class b extends a
{
int a=10;
void show()
{ System.out.println(“a of child class: “+a);
System.out.println(“ a of super class: “+super.a);
}
b()
{
super(0); //call the super class constructor
}
}
When super class has parametrised constructor then child class constructor must pass the values to
super class constructor. Using:
super(values);
class a
{
private int a=5;
a(int value)
{
a=value;
}
}
class b extends a
{
int a=10;
void show()
{ System.out.println(“a of child class: “+a);
System.out.println(“ a of super class: “+super.a); // will give error
}
b()
{
super(0); //call the super class constructor
}
}
class a
{
int a=5;
}
class b extends a
{
int b=10;
}
class c extends b
{
int c=20;
}
Now
c obj=new c( );
System.out.println(“A: “+a);
System.out.println(“B: “+b);
System.out.println(“C: “+c);
will output:
A: 5
B: 10
C: 20
• All variables of an interface are public, final and static
• All methods of an interface are public and abstract
• interface itself is abstract
• An interface can only extends other interfaces
Syntax:
interface interfaceName
{
//members
}
for example:
interface MyInterface
{
int a=5; //public static final int a=5;
void show(); // public abstract void show();
}
classes that implements the interface must override interface’s all methods
class Demo implements MyInterface
{
int a=10;
@override
public void show() //Must write public
{
System.out.println(“A of interface: ”+MyInterface.a); //a is static so refer it using interface name
System.out.println(“A of class: ”+a);
}
}
class Demo extends AnotherClass implements MyInterface
{
int a=10;
@override
public void show()
{
//statements
}
}
Child class reference can be assigned to super class reference. For example:
class A
{
int a=5;
int b=6;
void show( )
{ System.out.println(“a: “+a+” b: “+b); }
}
class B extends A
{
int c=10;
int d=20;
void print()
{ System.out.println(“c: “+c+” d: “+d); }
}
A ob1=new A( );
B ob2=new B( );
By using ob1 we can access:
ob1.a;
ob1.b;
ob1.show();
By using ob2 we can access:
ob2.a;
ob2.b;
ob2.show();
ob2.c;
ob2.d;
ob2.print();
B ob2=new B( );
A ob1=ob2; //child class reference assigned to parent class
By using ob1 we can access:
ob1.a;
ob1.b;
ob1.show();
By using ob2 we can access:
ob2.a;
ob2.b;
ob2.show();
ob2.c;
ob2.d;
ob2.print();
When child class has method with same signature(i.e. same name and same arguments) as
in parent class then parent class method is overridden. For example:
class A
{
int a=5;
int b=6;
void show( )
{ System.out.println(“Show in A”); }
}
class B extends A
{
int c=10;
int d=20;
void show()
{ System.out.println(“Show in B”); }
}
A ob1=new A( );
B ob2=new B( );
By using ob1 we can access:
ob1.a;
ob1.b;
ob1.show(); //Show in A
By using ob2 we can access:
ob2.a;
ob2.b;
ob2.show(); //Show in B
ob2.c;
ob2.d;
B ob2=new B( );
A ob1=ob2; //child class reference assigned to parent class
By using ob1 we can access:
ob1.a;
ob1.b;
ob1.show(); //Show in B
By using ob2 we can access:
ob2.a;
ob2.b;
ob2.show(); //Show in B
ob2.c;
ob2.d;
For more Visit:
http://gsb-programming.blogspot.in/search/label/Java

More Related Content

What's hot

Tip Top Typing - A Look at Python Typing
Tip Top Typing - A Look at Python TypingTip Top Typing - A Look at Python Typing
Tip Top Typing - A Look at Python TypingPatrick Viafore
 
Design of OO language
Design of OO languageDesign of OO language
Design of OO languageGeorgiana T.
 
Peyton jones-2011-type classes
Peyton jones-2011-type classesPeyton jones-2011-type classes
Peyton jones-2011-type classesTakayuki Muranushi
 
Peyton jones-2009-fun with-type_functions-slide
Peyton jones-2009-fun with-type_functions-slidePeyton jones-2009-fun with-type_functions-slide
Peyton jones-2009-fun with-type_functions-slideTakayuki Muranushi
 
Collection v3
Collection v3Collection v3
Collection v3Sunil OS
 
Scala: building bridges between programming domains
Scala: building bridges between programming domainsScala: building bridges between programming domains
Scala: building bridges between programming domainsGermán Ferrari
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsPhilip Schwarz
 
The Ring programming language version 1.7 book - Part 34 of 196
The Ring programming language version 1.7 book - Part 34 of 196The Ring programming language version 1.7 book - Part 34 of 196
The Ring programming language version 1.7 book - Part 34 of 196Mahmoud Samir Fayed
 

What's hot (20)

Tip Top Typing - A Look at Python Typing
Tip Top Typing - A Look at Python TypingTip Top Typing - A Look at Python Typing
Tip Top Typing - A Look at Python Typing
 
C++ Template
C++ TemplateC++ Template
C++ Template
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Inheritance
InheritanceInheritance
Inheritance
 
Design of OO language
Design of OO languageDesign of OO language
Design of OO language
 
Java cheatsheet
Java cheatsheetJava cheatsheet
Java cheatsheet
 
Python programming : Abstract classes interfaces
Python programming : Abstract classes interfacesPython programming : Abstract classes interfaces
Python programming : Abstract classes interfaces
 
Peyton jones-2011-type classes
Peyton jones-2011-type classesPeyton jones-2011-type classes
Peyton jones-2011-type classes
 
Peyton jones-2009-fun with-type_functions-slide
Peyton jones-2009-fun with-type_functions-slidePeyton jones-2009-fun with-type_functions-slide
Peyton jones-2009-fun with-type_functions-slide
 
Collection v3
Collection v3Collection v3
Collection v3
 
Scala: building bridges between programming domains
Scala: building bridges between programming domainsScala: building bridges between programming domains
Scala: building bridges between programming domains
 
C# programming
C# programming C# programming
C# programming
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
 
The Ring programming language version 1.7 book - Part 34 of 196
The Ring programming language version 1.7 book - Part 34 of 196The Ring programming language version 1.7 book - Part 34 of 196
The Ring programming language version 1.7 book - Part 34 of 196
 
OOP Core Concept
OOP Core ConceptOOP Core Concept
OOP Core Concept
 
Closer look at classes
Closer look at classesCloser look at classes
Closer look at classes
 
OOP
OOPOOP
OOP
 
Kotlin Starter Pack
Kotlin Starter PackKotlin Starter Pack
Kotlin Starter Pack
 
Templates2
Templates2Templates2
Templates2
 
basic concepts
basic conceptsbasic concepts
basic concepts
 

Viewers also liked

Artificial Intelligence Notes- Set5
Artificial Intelligence Notes- Set5Artificial Intelligence Notes- Set5
Artificial Intelligence Notes- Set5Gurpreet singh
 
Artificial Intelligence Notes- Set4
Artificial Intelligence Notes- Set4Artificial Intelligence Notes- Set4
Artificial Intelligence Notes- Set4Gurpreet singh
 
Multiplateform testing
Multiplateform testingMultiplateform testing
Multiplateform testingGurpreet singh
 
Artificial Intelligence Notes- Set6
Artificial Intelligence Notes- Set6Artificial Intelligence Notes- Set6
Artificial Intelligence Notes- Set6Gurpreet singh
 
Artificial Intelligence Notes- Set1
Artificial Intelligence Notes- Set1Artificial Intelligence Notes- Set1
Artificial Intelligence Notes- Set1Gurpreet singh
 
Artificial Intelligence Notes- Set3
Artificial Intelligence Notes- Set3Artificial Intelligence Notes- Set3
Artificial Intelligence Notes- Set3Gurpreet singh
 
Ingenium test(Exam Management System) Project Presentation (Full)
Ingenium test(Exam Management System) Project Presentation (Full)Ingenium test(Exam Management System) Project Presentation (Full)
Ingenium test(Exam Management System) Project Presentation (Full)Gurpreet singh
 
Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)Gurpreet singh
 
Software Testing and Quality Assurance Assignment 2
Software Testing and Quality Assurance Assignment 2Software Testing and Quality Assurance Assignment 2
Software Testing and Quality Assurance Assignment 2Gurpreet singh
 
Explain Communication among agents in Artificial Intelligence
Explain Communication among agents in Artificial IntelligenceExplain Communication among agents in Artificial Intelligence
Explain Communication among agents in Artificial IntelligenceGurpreet singh
 
Cloud Computing Assignment 3
Cloud Computing Assignment 3Cloud Computing Assignment 3
Cloud Computing Assignment 3Gurpreet singh
 

Viewers also liked (17)

Asg1 ai
Asg1 aiAsg1 ai
Asg1 ai
 
Artificial Intelligence Notes- Set5
Artificial Intelligence Notes- Set5Artificial Intelligence Notes- Set5
Artificial Intelligence Notes- Set5
 
Artificial Intelligence Notes- Set4
Artificial Intelligence Notes- Set4Artificial Intelligence Notes- Set4
Artificial Intelligence Notes- Set4
 
Multiplateform testing
Multiplateform testingMultiplateform testing
Multiplateform testing
 
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
 
Artificial Intelligence Notes- Set6
Artificial Intelligence Notes- Set6Artificial Intelligence Notes- Set6
Artificial Intelligence Notes- Set6
 
Learn Java Part 7
Learn Java Part 7Learn Java Part 7
Learn Java Part 7
 
Learn Java Part 4
Learn Java Part 4Learn Java Part 4
Learn Java Part 4
 
Artificial Intelligence Notes- Set1
Artificial Intelligence Notes- Set1Artificial Intelligence Notes- Set1
Artificial Intelligence Notes- Set1
 
Stars
StarsStars
Stars
 
Learn Java Part 2
Learn Java Part 2Learn Java Part 2
Learn Java Part 2
 
Artificial Intelligence Notes- Set3
Artificial Intelligence Notes- Set3Artificial Intelligence Notes- Set3
Artificial Intelligence Notes- Set3
 
Ingenium test(Exam Management System) Project Presentation (Full)
Ingenium test(Exam Management System) Project Presentation (Full)Ingenium test(Exam Management System) Project Presentation (Full)
Ingenium test(Exam Management System) Project Presentation (Full)
 
Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)
 
Software Testing and Quality Assurance Assignment 2
Software Testing and Quality Assurance Assignment 2Software Testing and Quality Assurance Assignment 2
Software Testing and Quality Assurance Assignment 2
 
Explain Communication among agents in Artificial Intelligence
Explain Communication among agents in Artificial IntelligenceExplain Communication among agents in Artificial Intelligence
Explain Communication among agents in Artificial Intelligence
 
Cloud Computing Assignment 3
Cloud Computing Assignment 3Cloud Computing Assignment 3
Cloud Computing Assignment 3
 

Similar to Learn Java Part 11

Similar to Learn Java Part 11 (20)

Keyword of java
Keyword of javaKeyword of java
Keyword of java
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Java
JavaJava
Java
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)
 
Java tutoria part 2
Java tutoria part 2Java tutoria part 2
Java tutoria part 2
 
Java tutorial part 2
Java tutorial part 2Java tutorial part 2
Java tutorial part 2
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
java tutorial 3
 java tutorial 3 java tutorial 3
java tutorial 3
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
 
Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Java unit2
Java unit2Java unit2
Java unit2
 
E5
E5E5
E5
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 
Core java day5
Core java day5Core java day5
Core java day5
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Inheritance and overriding
Inheritance  and overridingInheritance  and overriding
Inheritance and overriding
 

More from Gurpreet singh

Introduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP ReportingIntroduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP ReportingGurpreet singh
 
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...Gurpreet singh
 
Oracle Application Developmenr Framework
Oracle Application Developmenr FrameworkOracle Application Developmenr Framework
Oracle Application Developmenr FrameworkGurpreet singh
 
Oracle advanced queuing
Oracle advanced queuingOracle advanced queuing
Oracle advanced queuingGurpreet singh
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in JavaGurpreet singh
 
IO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxingIO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxingGurpreet singh
 
Creating business group in oracle apps
Creating business group in oracle appsCreating business group in oracle apps
Creating business group in oracle appsGurpreet singh
 
Defing locations in Oracle Apps
Defing locations in Oracle AppsDefing locations in Oracle Apps
Defing locations in Oracle AppsGurpreet singh
 
Assigning role AME_BUS_ANALYST
Assigning role AME_BUS_ANALYSTAssigning role AME_BUS_ANALYST
Assigning role AME_BUS_ANALYSTGurpreet singh
 
Computer Graphics Notes
Computer Graphics NotesComputer Graphics Notes
Computer Graphics NotesGurpreet singh
 

More from Gurpreet singh (20)

Introduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP ReportingIntroduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP Reporting
 
Why Messaging system?
Why Messaging system?Why Messaging system?
Why Messaging system?
 
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
 
Oracle Application Developmenr Framework
Oracle Application Developmenr FrameworkOracle Application Developmenr Framework
Oracle Application Developmenr Framework
 
Java Servlet part 3
Java Servlet part 3Java Servlet part 3
Java Servlet part 3
 
Oracle advanced queuing
Oracle advanced queuingOracle advanced queuing
Oracle advanced queuing
 
Oracle SQL Part 3
Oracle SQL Part 3Oracle SQL Part 3
Oracle SQL Part 3
 
Oracle SQL Part 2
Oracle SQL Part 2Oracle SQL Part 2
Oracle SQL Part 2
 
Oracle SQL Part1
Oracle SQL Part1Oracle SQL Part1
Oracle SQL Part1
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
 
IO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxingIO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxing
 
Java Servlets Part 2
Java Servlets Part 2Java Servlets Part 2
Java Servlets Part 2
 
Creating business group in oracle apps
Creating business group in oracle appsCreating business group in oracle apps
Creating business group in oracle apps
 
Defing locations in Oracle Apps
Defing locations in Oracle AppsDefing locations in Oracle Apps
Defing locations in Oracle Apps
 
Assigning role AME_BUS_ANALYST
Assigning role AME_BUS_ANALYSTAssigning role AME_BUS_ANALYST
Assigning role AME_BUS_ANALYST
 
PL/SQL Part 5
PL/SQL Part 5PL/SQL Part 5
PL/SQL Part 5
 
PL/SQL Part 3
PL/SQL Part 3PL/SQL Part 3
PL/SQL Part 3
 
PL/SQL Part 2
PL/SQL Part 2PL/SQL Part 2
PL/SQL Part 2
 
PL/SQL Part 1
PL/SQL Part 1PL/SQL Part 1
PL/SQL Part 1
 
Computer Graphics Notes
Computer Graphics NotesComputer Graphics Notes
Computer Graphics Notes
 

Recently uploaded

247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 

Recently uploaded (20)

247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 

Learn Java Part 11

  • 2. When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write. This is known as Inheritance.For example: class A { int x; long y; void showValues( ) { System.out.print(“x: ”+x+” y”+y); } void setValues(int a, long b) { x=a; y=b; } } Here, B extends A, therefore B inherits all functions and member variables from A class B extends A { String z; void showString( ) { System.out.print(“z: ”+z); } void setString(String s) { z=s; } }
  • 3. A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class). In previous example, class A is superclass (also a base class or a parent class). class B is subclass(also a derived class, extended class, or child class) A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. to refer to superclass we use the keyword: super class a class b extends a { { int a=5; int a=10; } void show() { System.out.println(“a of child class: “+a); System.out.println(“ a of super class: “+super.a); } } output: a of child class: 10 a of super class: 5
  • 4. Same class Other class in same package Child class in same package Other class in same package Child class in other package private Yes No No No No default Yes Yes Yes No No protected Yes Yes Yes No Yes public Yes Yes Yes Yes Yes Figure: Accessibility of variable or function when using particular access-specifier
  • 5. class a { int a=5; a(int value) { a=value; } } class b extends a { int a=10; void show() { System.out.println(“a of child class: “+a); System.out.println(“ a of super class: “+super.a); } b() { super(0); //call the super class constructor } } When super class has parametrised constructor then child class constructor must pass the values to super class constructor. Using: super(values);
  • 6. class a { private int a=5; a(int value) { a=value; } } class b extends a { int a=10; void show() { System.out.println(“a of child class: “+a); System.out.println(“ a of super class: “+super.a); // will give error } b() { super(0); //call the super class constructor } }
  • 7. class a { int a=5; } class b extends a { int b=10; } class c extends b { int c=20; } Now c obj=new c( ); System.out.println(“A: “+a); System.out.println(“B: “+b); System.out.println(“C: “+c); will output: A: 5 B: 10 C: 20
  • 8. • All variables of an interface are public, final and static • All methods of an interface are public and abstract • interface itself is abstract • An interface can only extends other interfaces Syntax: interface interfaceName { //members } for example: interface MyInterface { int a=5; //public static final int a=5; void show(); // public abstract void show(); } classes that implements the interface must override interface’s all methods
  • 9. class Demo implements MyInterface { int a=10; @override public void show() //Must write public { System.out.println(“A of interface: ”+MyInterface.a); //a is static so refer it using interface name System.out.println(“A of class: ”+a); } }
  • 10. class Demo extends AnotherClass implements MyInterface { int a=10; @override public void show() { //statements } }
  • 11. Child class reference can be assigned to super class reference. For example: class A { int a=5; int b=6; void show( ) { System.out.println(“a: “+a+” b: “+b); } } class B extends A { int c=10; int d=20; void print() { System.out.println(“c: “+c+” d: “+d); } }
  • 12. A ob1=new A( ); B ob2=new B( ); By using ob1 we can access: ob1.a; ob1.b; ob1.show(); By using ob2 we can access: ob2.a; ob2.b; ob2.show(); ob2.c; ob2.d; ob2.print(); B ob2=new B( ); A ob1=ob2; //child class reference assigned to parent class By using ob1 we can access: ob1.a; ob1.b; ob1.show(); By using ob2 we can access: ob2.a; ob2.b; ob2.show(); ob2.c; ob2.d; ob2.print();
  • 13. When child class has method with same signature(i.e. same name and same arguments) as in parent class then parent class method is overridden. For example: class A { int a=5; int b=6; void show( ) { System.out.println(“Show in A”); } } class B extends A { int c=10; int d=20; void show() { System.out.println(“Show in B”); } }
  • 14. A ob1=new A( ); B ob2=new B( ); By using ob1 we can access: ob1.a; ob1.b; ob1.show(); //Show in A By using ob2 we can access: ob2.a; ob2.b; ob2.show(); //Show in B ob2.c; ob2.d; B ob2=new B( ); A ob1=ob2; //child class reference assigned to parent class By using ob1 we can access: ob1.a; ob1.b; ob1.show(); //Show in B By using ob2 we can access: ob2.a; ob2.b; ob2.show(); //Show in B ob2.c; ob2.d;