SlideShare a Scribd company logo
Derived conceptsDerived concepts
By
Nilesh Dalvi
Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College.
http://www.slideshare.net/nileshdalvi01
Java and Data StructuresJava and Data Structures
Inheritance
• Inheritance is one of the most useful and essential
characteristics of oops.
• Existing classes are main components of
inheritance.
• New classes are created from existing one.
• Properties of existing classes are simply extended
to the new classes.
• New classes are called as derived classes and
existing one are base classes.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Types of Inheritance
1. Single Inheritance
2. Hierarchical Inheritance
3. Multilevel Inheritance
4. Hybrid Inheritance
5. Multiple Inheritance (Interfaces)
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Types of Inheritance
Single inheritance:
A derived class with only one base class is
called as single inheritance.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Types of Inheritance
Multilevel inheritance:
The mechanism of deriving a class from
another ‘derived class’ is known as multilevel
inheritance.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Types of Inheritance
Hierarchical inheritance:
One class may be inherited by more than one
class. This process is known as hierarchical
inheritance.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Types of Inheritance
Hybrid inheritance:
It is combination of Hierarchical and Multilevel
inheritance.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Types of Inheritance
Multiple inheritance (Interfaces):
A derived class with several base classes is
called as Multiple inheritance.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Defining Sub class
• A Sub class can be defined by specifying its relationship with
the super class in addition to its own details.
class sub-class-name extends super-class-name
{
//members of sub class.
}
• The extends indicates that the properties of super class are
extended to the sub class.
• Sub class contain its own properties as well as those of the
super class.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Visibility Control
• It is also known as access modifiers.
• Java provides:
– Public access
– Friendly access (Default)
– Protected access
– Private access
– Private protected access
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Visibility Control
Public access:
• Any variable or method is visible to the entire
class in which it is defined.
• But, to make a member accessible outside
with objects, we simply declare the variable or
method as public.
• A variable or method declared as public has
the widest possible visibility and accessible
everywhere.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Visibility Control
Friendly access:
• When no access modifier is specified, the member
defaults to a limited version of public accessibility
known as "friendly" level of access.
• The difference between the "public" access and the
"friendly" access is that the public modifier makes
fields visible in all classes, regardless of their
packages while the friendly access makes fields
visible only in the same package, but not in other
packages.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Visibility Control
Protected access:
• The visibility level of a "protected" field lies in
between the public access and friendly access.
• That is, the protected modifier makes the fields
visible not only to all classes and subclasses in the
same package but also to subclasses in other
packages
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Visibility Control
Private access:
• Private fields have the highest degree of protection.
• They are accessible only with their own class.
• They cannot be inherited by subclasses and
therefore not accessible in subclasses.
• Method declared as private behaves like final
method .
• It prevents the method from being sub classed.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Visibility Control
Private protected access:
• A field can be declared with two
keywords private and protected together.
Eg. private protected int codeNumber;
• This gives a visibility level in between the "protected"
access and "private" access.
• This modifier makes the fields visible in all subclasses
regardless of what package they are in.
• Remember, these fields are not accessible by other
classes in the same package.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Visibility Control
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Single inheritance
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Room
{
int length;
int breadth;
Room (int x, int y)
{
length = x;
breadth = y;
}
int area ()
{
return(length * breadth);
}
}
Single inheritance
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class BedRoom extends Room
{
int heigth;
BedRoom(int x, int y, int z)
{
super (x, y);
height = z;
}
int volume ()
{
return (length * breadth * height);
}
}
Single inheritance
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class IntDemo
{
public static void main(String args [])
{
BedRoom b = new BedRoom(14, 12, 10);
System.out.println("Area :" + b.area());
System.out.println("Volume :" + b.volume());
}
}
super keyword
• The super is a reference variable that is used
to refer immediate parent class object.
• Usage of super Keyword
– super is used to refer immediate parent class
instance variable.
– super() is used to invoke immediate parent class
constructor.
– super is used to invoke immediate parent class
method.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
super keyword
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
//refer immediate parent class instance variable.
class Vehicle
{
int speed = 50;
}
class Bike extends Vehicle
{
int speed = 100;
void display()
{
System.out.println(super.speed);
}
public static void main(String args[])
{
Bike b=new Bike();
b.display();
}
}
super keyword
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
//invoke parent class constructor.
class Vehicle
{
Vehicle()
{
System.out.println("Vehicle is created");
}
}
class Bike extends Vehicle
{
Bike()
{
super();//will invoke parent class constructor
System.out.println("Bike is created");
}
public static void main(String args[])
{
Bike b=new Bike();
}
}
super keyword
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
// super can be used to invoke parent class method.
class Person
{
void message(){
System.out.println("welcome");
}
}
class Student extends Person
{
void message(){
System.out.println("welcome to java");
}
void display(){
message();
super.message();
}
public static void main(String args[]){
Student s=new Student();
s.display();
}
}
Method Overriding
• If subclass (child class) has the same method as declared in
the parent class, it is known as method overriding.
• Advantage of Java Method Overriding
– Method Overriding is used to provide specific implementation of a
method that is already provided by its super class.
– Method Overriding is used for Runtime Polymorphism
• Rules for Method Overriding
– method must have same name as in the parent class
– method must have same parameter as in the parent class.
– must be IS-A relationship (inheritance).
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Method Overriding
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Vehicle
{
void run()
{
System.out.println("Vehicle is running");
}
}
class Bike extends Vehicle
{
void run()
{
System.out.println("Bike is running safely");
}
public static void main(String args[])
{
Bike obj = new Bike();
obj.run();
}
}
final keyword
• It is used to restrict the user.
• The final keyword can be used in many context.
• Final can be:
– Variable
– Method
– class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
final Variable
• If you make any variable as final, you cannot change
the value of final variable(It will be constant).
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Bike
{
final int speedlimit=90;
void run()
{
speedlimit=400;
}
public static void main(String args[])
{
Bike obj=new Bike();
obj.run();
}
}
final Method
• If you make any method as final, you cannot override
it.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Bike
{
final void run() {
System.out.println("running");
}
}
class Honda extends Bike
{
void run() {
System.out.println("running safely
with 100kmph");
}
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
}
}
final Class
• If you make any class as final, you cannot extend it.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
final class Bike{}
class Honda extends Bike
{
void run()
{
System.out.println("running safely
with 100kmph");
}
public static void main(String args[])
{
Honda honda= new Honda();
honda.run();
}
}
Abstract Modifier
• It is applied to
– Class
– Method
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Abstract Class
• An abstract class never be instantiated.
• If a class is declared as abstract then, the sole purpose is for
the class to be extended.
• A class can not both final and abstract.
• If class contain abstract method, the class should be declared
abstract otherwise compile time error thrown.
• Abstract class may contain both abstract and normal
methods.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
abstract class shape
{
-----------
-----------
abstract void draw();
-----------
}
Abstract Method
• It is a method declared without any implementation .
• Method body provided by sub class
• Never be final
• Any class that extends abstract class must implement all the
abstract method of super class, unless the subclass is also an
abstract class.
• The abstract method ends with semicolon.
• Example: public abstract sample();
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Rectangle extends shape
{
void draw(){
------
}
}
Abstract Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
abstract class Media
{
protected String title;
protected float price;
media(String s, float p)
{
title = s;
price = p;
}
abstract void display();
}
Abstract Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Book extends Media
{
protected int pgs;
Book(String s, float p, int p)
{
title = s;
price = p;
pgs = p;
}
void display ()
{
System.out.println("Title :"+ title);
System.out.println("Pages :"+ pgs);
System.out.println("Price :"+ price);
}
}
Abstract Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Tape extends Media
{
protected int time;
Book(String s, float p, int t)
{
title = s;
price = p;
time = t;
}
void display ()
{
System.out.println("Title :"+ title);
System.out.println("Duration :"+ time);
System.out.println("Price :"+ price);
}
}
Abstract Class
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class AbstractMain
{
public static void main(String args [])
{
Media book = new Book("ABC", 100, 20);
Media tape = new Tape("XYZ", 100, 20);
book.display();
tape.display();
}
}
Finalizer Method
• Constructor is used to initialize an object when it is declared,
process is known as initialization.
• Java supports a concept of finalization.
• JRE automatically frees up the memory recourses used y the
objects.
• But object may hold non-object resources such as window
system fonts and file descriptor.
• Garbage collector cannot be free these resources in order to
free these we must use a finalizer method.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
finalize(){
//
}
Interfaces: Multiple inheritance
• An interface is basically kind of class
• Like classes interfaces contains methods and
variable but with major difference
• It defines only abstract methods and final fields
• Do not specifies any code to implement these
methods and data fields contain only constants.
• Its responsibility of the class that implements an
interface to define the code for implementation
of these methods.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Defining interface
interface interface-name // implicitly abstract
{
variable declaration; // implicitly public
methods declaration; // implicitly abstract, public
}
• Example:
interface Item // implicitly abstract
{
static final int code = 1001;
static final String name = “Fan”;
void display();
}
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Difference between class & Interface
• Members of interface are always constant i.e. their
values are final.
• Methods in interface are abstract in nature i.e. no
code associated with it.
• An interface never be instantiated, it can only
inherited by a class.
• It can only use public access specifier.
• An interface is not extended by a class; it is
implemented by a class.
• An interface can extend multiple interfaces.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Extending interfaces
interface name2 extends name1
{
//body of name2
}
• Example:
interface ItemConstants
{
static final int code = 1001;
static final String name = “Fan”;
}
interface Item extends ItemConstants
{
void display();
}
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Implementing interfaces
class class-name implements interface-name
{
//body of class-name
}
• More general form:
class class-name extends super-class implements interface-name
{
//body of class-name
}
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Implementing Multiple inheritance
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Student
Test
Results
Sports
extends
extends
implements
Packages
• A package is a collection of similar types of classes,
interfaces and sub-packages.
• Package in java can be categorized in two form,
built-in package and user-defined package.
• Since package acts as the library which is shared by
the many people.
• Standard Java library is distributed over the number
of packages, including :
– java.lang
– java.util
– java.net and so on.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Packages
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Creating packages
package mypack;// package declaration
public class class-name1
{
body of class-name
}
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Using package
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
import mypack.*;
class class-name2 extends class-name1
{
body of class-name2
}
Hiding Classes
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Q & A

More Related Content

What's hot

8. String
8. String8. String
8. String
Nilesh Dalvi
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
Nilesh Dalvi
 
13. Queue
13. Queue13. Queue
13. Queue
Nilesh Dalvi
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
Ravi_Kant_Sahu
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
Ravi_Kant_Sahu
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
Indu Sharma Bhardwaj
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
BG Java EE Course
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
Ravi_Kant_Sahu
 

What's hot (9)

8. String
8. String8. String
8. String
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
13. Queue
13. Queue13. Queue
13. Queue
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 

Similar to 5. Inheritances, Packages and Intefaces

Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
Nilesh Dalvi
 
Inheritance
InheritanceInheritance
Inheritance
Ravi_Kant_Sahu
 
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
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
Kalai Selvi
 
Inheritance
InheritanceInheritance
Inheritance
Munsif Ullah
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
teach4uin
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
teach4uin
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
sonukumarjha12
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
DevaKumari Vijay
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
Arslan Waseem
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Lovely Professional University
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
PRN USM
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Nilesh Dalvi
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
Java keywords
Java keywordsJava keywords
Java keywords
Ravi_Kant_Sahu
 
Access modifiers
Access modifiersAccess modifiers
Access modifiers
Kasun Ranga Wijeweera
 
Java(inheritance)
Java(inheritance)Java(inheritance)
Java(inheritance)
Pooja Bhojwani
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
rajkamaltibacademy
 
Classes in Java great learning.pdf
Classes in Java great learning.pdfClasses in Java great learning.pdf
Classes in Java great learning.pdf
SHASHIKANT346021
 

Similar to 5. Inheritances, Packages and Intefaces (20)

Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Inheritance
InheritanceInheritance
Inheritance
 
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
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Access modifiers
Access modifiersAccess modifiers
Access modifiers
 
Java(inheritance)
Java(inheritance)Java(inheritance)
Java(inheritance)
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
Classes in Java great learning.pdf
Classes in Java great learning.pdfClasses in Java great learning.pdf
Classes in Java great learning.pdf
 

More from Nilesh Dalvi

14. Linked List
14. Linked List14. Linked List
14. Linked List
Nilesh Dalvi
 
12. Stack
12. Stack12. Stack
12. Stack
Nilesh Dalvi
 
11. Arrays
11. Arrays11. Arrays
11. Arrays
Nilesh Dalvi
 
Templates
TemplatesTemplates
Templates
Nilesh Dalvi
 
File handling
File handlingFile handling
File handling
Nilesh Dalvi
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
Nilesh Dalvi
 
Strings
StringsStrings
Strings
Nilesh Dalvi
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Nilesh Dalvi
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Nilesh Dalvi
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
Nilesh Dalvi
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
Nilesh Dalvi
 

More from Nilesh Dalvi (12)

14. Linked List
14. Linked List14. Linked List
14. Linked List
 
12. Stack
12. Stack12. Stack
12. Stack
 
11. Arrays
11. Arrays11. Arrays
11. Arrays
 
Templates
TemplatesTemplates
Templates
 
File handling
File handlingFile handling
File handling
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Strings
StringsStrings
Strings
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 

Recently uploaded

How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 

5. Inheritances, Packages and Intefaces

  • 1. Derived conceptsDerived concepts By Nilesh Dalvi Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College. http://www.slideshare.net/nileshdalvi01 Java and Data StructuresJava and Data Structures
  • 2. Inheritance • Inheritance is one of the most useful and essential characteristics of oops. • Existing classes are main components of inheritance. • New classes are created from existing one. • Properties of existing classes are simply extended to the new classes. • New classes are called as derived classes and existing one are base classes. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 3. Types of Inheritance 1. Single Inheritance 2. Hierarchical Inheritance 3. Multilevel Inheritance 4. Hybrid Inheritance 5. Multiple Inheritance (Interfaces) Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 4. Types of Inheritance Single inheritance: A derived class with only one base class is called as single inheritance. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 5. Types of Inheritance Multilevel inheritance: The mechanism of deriving a class from another ‘derived class’ is known as multilevel inheritance. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 6. Types of Inheritance Hierarchical inheritance: One class may be inherited by more than one class. This process is known as hierarchical inheritance. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 7. Types of Inheritance Hybrid inheritance: It is combination of Hierarchical and Multilevel inheritance. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 8. Types of Inheritance Multiple inheritance (Interfaces): A derived class with several base classes is called as Multiple inheritance. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 9. Defining Sub class • A Sub class can be defined by specifying its relationship with the super class in addition to its own details. class sub-class-name extends super-class-name { //members of sub class. } • The extends indicates that the properties of super class are extended to the sub class. • Sub class contain its own properties as well as those of the super class. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 10. Visibility Control • It is also known as access modifiers. • Java provides: – Public access – Friendly access (Default) – Protected access – Private access – Private protected access Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 11. Visibility Control Public access: • Any variable or method is visible to the entire class in which it is defined. • But, to make a member accessible outside with objects, we simply declare the variable or method as public. • A variable or method declared as public has the widest possible visibility and accessible everywhere. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 12. Visibility Control Friendly access: • When no access modifier is specified, the member defaults to a limited version of public accessibility known as "friendly" level of access. • The difference between the "public" access and the "friendly" access is that the public modifier makes fields visible in all classes, regardless of their packages while the friendly access makes fields visible only in the same package, but not in other packages. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 13. Visibility Control Protected access: • The visibility level of a "protected" field lies in between the public access and friendly access. • That is, the protected modifier makes the fields visible not only to all classes and subclasses in the same package but also to subclasses in other packages Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 14. Visibility Control Private access: • Private fields have the highest degree of protection. • They are accessible only with their own class. • They cannot be inherited by subclasses and therefore not accessible in subclasses. • Method declared as private behaves like final method . • It prevents the method from being sub classed. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 15. Visibility Control Private protected access: • A field can be declared with two keywords private and protected together. Eg. private protected int codeNumber; • This gives a visibility level in between the "protected" access and "private" access. • This modifier makes the fields visible in all subclasses regardless of what package they are in. • Remember, these fields are not accessible by other classes in the same package. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 16. Visibility Control Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 17. Single inheritance Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Room { int length; int breadth; Room (int x, int y) { length = x; breadth = y; } int area () { return(length * breadth); } }
  • 18. Single inheritance Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class BedRoom extends Room { int heigth; BedRoom(int x, int y, int z) { super (x, y); height = z; } int volume () { return (length * breadth * height); } }
  • 19. Single inheritance Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class IntDemo { public static void main(String args []) { BedRoom b = new BedRoom(14, 12, 10); System.out.println("Area :" + b.area()); System.out.println("Volume :" + b.volume()); } }
  • 20. super keyword • The super is a reference variable that is used to refer immediate parent class object. • Usage of super Keyword – super is used to refer immediate parent class instance variable. – super() is used to invoke immediate parent class constructor. – super is used to invoke immediate parent class method. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 21. super keyword Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). //refer immediate parent class instance variable. class Vehicle { int speed = 50; } class Bike extends Vehicle { int speed = 100; void display() { System.out.println(super.speed); } public static void main(String args[]) { Bike b=new Bike(); b.display(); } }
  • 22. super keyword Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). //invoke parent class constructor. class Vehicle { Vehicle() { System.out.println("Vehicle is created"); } } class Bike extends Vehicle { Bike() { super();//will invoke parent class constructor System.out.println("Bike is created"); } public static void main(String args[]) { Bike b=new Bike(); } }
  • 23. super keyword Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). // super can be used to invoke parent class method. class Person { void message(){ System.out.println("welcome"); } } class Student extends Person { void message(){ System.out.println("welcome to java"); } void display(){ message(); super.message(); } public static void main(String args[]){ Student s=new Student(); s.display(); } }
  • 24. Method Overriding • If subclass (child class) has the same method as declared in the parent class, it is known as method overriding. • Advantage of Java Method Overriding – Method Overriding is used to provide specific implementation of a method that is already provided by its super class. – Method Overriding is used for Runtime Polymorphism • Rules for Method Overriding – method must have same name as in the parent class – method must have same parameter as in the parent class. – must be IS-A relationship (inheritance). Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 25. Method Overriding Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Vehicle { void run() { System.out.println("Vehicle is running"); } } class Bike extends Vehicle { void run() { System.out.println("Bike is running safely"); } public static void main(String args[]) { Bike obj = new Bike(); obj.run(); } }
  • 26. final keyword • It is used to restrict the user. • The final keyword can be used in many context. • Final can be: – Variable – Method – class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 27. final Variable • If you make any variable as final, you cannot change the value of final variable(It will be constant). Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Bike { final int speedlimit=90; void run() { speedlimit=400; } public static void main(String args[]) { Bike obj=new Bike(); obj.run(); } }
  • 28. final Method • If you make any method as final, you cannot override it. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Bike { final void run() { System.out.println("running"); } } class Honda extends Bike { void run() { System.out.println("running safely with 100kmph"); } public static void main(String args[]){ Honda honda= new Honda(); honda.run(); } }
  • 29. final Class • If you make any class as final, you cannot extend it. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). final class Bike{} class Honda extends Bike { void run() { System.out.println("running safely with 100kmph"); } public static void main(String args[]) { Honda honda= new Honda(); honda.run(); } }
  • 30. Abstract Modifier • It is applied to – Class – Method Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 31. Abstract Class • An abstract class never be instantiated. • If a class is declared as abstract then, the sole purpose is for the class to be extended. • A class can not both final and abstract. • If class contain abstract method, the class should be declared abstract otherwise compile time error thrown. • Abstract class may contain both abstract and normal methods. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). abstract class shape { ----------- ----------- abstract void draw(); ----------- }
  • 32. Abstract Method • It is a method declared without any implementation . • Method body provided by sub class • Never be final • Any class that extends abstract class must implement all the abstract method of super class, unless the subclass is also an abstract class. • The abstract method ends with semicolon. • Example: public abstract sample(); Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Rectangle extends shape { void draw(){ ------ } }
  • 33. Abstract Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). abstract class Media { protected String title; protected float price; media(String s, float p) { title = s; price = p; } abstract void display(); }
  • 34. Abstract Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Book extends Media { protected int pgs; Book(String s, float p, int p) { title = s; price = p; pgs = p; } void display () { System.out.println("Title :"+ title); System.out.println("Pages :"+ pgs); System.out.println("Price :"+ price); } }
  • 35. Abstract Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Tape extends Media { protected int time; Book(String s, float p, int t) { title = s; price = p; time = t; } void display () { System.out.println("Title :"+ title); System.out.println("Duration :"+ time); System.out.println("Price :"+ price); } }
  • 36. Abstract Class Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class AbstractMain { public static void main(String args []) { Media book = new Book("ABC", 100, 20); Media tape = new Tape("XYZ", 100, 20); book.display(); tape.display(); } }
  • 37. Finalizer Method • Constructor is used to initialize an object when it is declared, process is known as initialization. • Java supports a concept of finalization. • JRE automatically frees up the memory recourses used y the objects. • But object may hold non-object resources such as window system fonts and file descriptor. • Garbage collector cannot be free these resources in order to free these we must use a finalizer method. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). finalize(){ // }
  • 38. Interfaces: Multiple inheritance • An interface is basically kind of class • Like classes interfaces contains methods and variable but with major difference • It defines only abstract methods and final fields • Do not specifies any code to implement these methods and data fields contain only constants. • Its responsibility of the class that implements an interface to define the code for implementation of these methods. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 39. Defining interface interface interface-name // implicitly abstract { variable declaration; // implicitly public methods declaration; // implicitly abstract, public } • Example: interface Item // implicitly abstract { static final int code = 1001; static final String name = “Fan”; void display(); } Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 40. Difference between class & Interface • Members of interface are always constant i.e. their values are final. • Methods in interface are abstract in nature i.e. no code associated with it. • An interface never be instantiated, it can only inherited by a class. • It can only use public access specifier. • An interface is not extended by a class; it is implemented by a class. • An interface can extend multiple interfaces. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 41. Extending interfaces interface name2 extends name1 { //body of name2 } • Example: interface ItemConstants { static final int code = 1001; static final String name = “Fan”; } interface Item extends ItemConstants { void display(); } Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 42. Implementing interfaces class class-name implements interface-name { //body of class-name } • More general form: class class-name extends super-class implements interface-name { //body of class-name } Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 43. Implementing Multiple inheritance Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Student Test Results Sports extends extends implements
  • 44. Packages • A package is a collection of similar types of classes, interfaces and sub-packages. • Package in java can be categorized in two form, built-in package and user-defined package. • Since package acts as the library which is shared by the many people. • Standard Java library is distributed over the number of packages, including : – java.lang – java.util – java.net and so on. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 46. Creating packages package mypack;// package declaration public class class-name1 { body of class-name } Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 47. Using package Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). import mypack.*; class class-name2 extends class-name1 { body of class-name2 }
  • 48. Hiding Classes Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 49. Q & A