SlideShare a Scribd company logo
1 of 14
Download to read offline
Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid
١
Inheritance
Inheritance can be defined as the process where one class acquires the properties
(methods and fields) of another class. With the use of inheritance the information is made
manageable in a hierarchical order.
The class which inherits the properties of other is known as subclass (derived class,
extended class,child class) and the class whose properties are inherited is known as
superclass (base class, parent class). In Java every class has one and only one direct
superclass (single inheritance).The extends keyword is used to inherit the properties
of a class.
‫شيء‬ ‫يك‬ ‫هاي‬ ‫ويژگي‬ ‫از‬ ‫برخي‬ ‫يا‬ ‫و‬ ‫تمامي‬ ‫كه‬ ‫كند‬ ‫ايجاد‬ ‫شيء‬ ‫يك‬ ‫بخواهد‬ ‫نويس‬ ‫برنامه‬ ‫اگر‬ ،‫خﻼصه‬ ‫طور‬ ‫به‬
‫كند‬ ‫استفاده‬ ‫جاوا‬ ‫نويسي‬ ‫برنامه‬ ‫زبان‬ ‫در‬ ‫بري‬ ‫ارث‬ ‫قابليت‬ ‫از‬ ‫تواند‬ ‫مي‬ ‫باشد‬ ‫داشته‬ ‫را‬ ‫ديگر‬.
: ‫توجه‬.‫كند‬ ‫نمي‬ ‫پشتيباني‬ ‫را‬ ‫كﻼس‬ ‫چند‬ ‫از‬ ‫بري‬ ‫ارث‬ ‫جاوا‬
Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid
٢
Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid
٣
What You Can Do in a Subclass
A subclass inherits all the members (fields, methods, and nested classes) from its
superclass, no matter what package the subclass is in. You can use the inherited
members as is, replace them, hide them, or supplement them with new members.
Constructors are not members, so they are not inherited by subclasses, but the
constructor of the superclass can be invoked from the subclass.
You can use the inherited members as is, replace them, hide them, or supplement them
with new members:
 With Inheritance, you can reuse the fields and methods of the existing class without
having to write (and debug!) them yourself.
 The inherited fields can be used directly, just like any other fields.
 You can declare a field in the subclass with the same name as the one in the
superclass, thus hiding it (not recommended).
 You can declare new fields in the subclass that are not in the superclass.
 The inherited methods can be used directly as they are.
 You can write a new instance method in the subclass that has the same signature
as the one in the superclass, thus overriding it.
 You can write a new static method in the subclass that has the same signature as
the one in the superclass, thus hiding it.
 You can declare new methods in the subclass that are not in the superclass.
 You can write a subclass constructor that invokes the constructor of the superclass,
either implicitly or by using the keyword super.
Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid
۴
Overriding:
In object-oriented terms, overriding means to override the functionality of an existing
method. Both subclass and parent class must have the same signature (name, plus the
number and the type of its parameters and return type).
The benefit of overriding is: ability to define a behavior that's specific to the subclass type,
which means a subclass can implement a parent class method based on its requirement.
 Instance Methods:
An instance method in a subclass overrides the superclass's method.
 Static Methods:
A static method in a subclass hides the superclass's method.
Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid
۵
Overloading:
we can use two method in same class that have same name.
Polymorphism: The occurrence of an object in several different forms
Polymorphism is the ability of an object to take on many forms. More specifically,
it is the ability to redefine methods for derived classes. (Overriding, Overloading)
Example:
public class Employee {
int age;
int dailySalary = 40000;
String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getDailySalary() {
return dailySalary;
}
public void setDailySalary(int dailySalary) {
this.dailySalary = dailySalary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int calculateMonthlySalary(int days) {
return this.dailySalary * days;
}
}
public class Programmer extends Employee{
Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid
۶
int prgBonus;
String programLanguage;
public int getPrgBonus() {
return prgBonus;
}
public void setPrgBonus(int prgBonus) {
this.prgBonus = prgBonus;
}
public String getProgramLanguage() {
return programLanguage;
}
public void setProgramLanguage(String programLanguage) {
this.programLanguage = programLanguage;
}
public int calculateMonthlySalary(int days) {
int baseSalary = super.calculateMonthlySalary(days);
return this.prgBonus + baseSalary;
}
}
‫تمرين‬١‫كﻼس‬ :Employee‫و‬Programmer‫ازاي‬ ‫به‬ ‫نويسها‬ ‫برنامه‬ ‫به‬ ‫كه‬ ‫كنيد‬ ‫نويسي‬ ‫باز‬ ‫طوري‬ ‫را‬
‫موفق‬ ‫پروژه‬ ‫هر‬٢‫مانده‬ ‫كه‬ ‫كنيد‬ ‫تعريف‬ ‫كﻼسها‬ ‫از‬ ‫كدام‬ ‫هر‬ ‫براي‬ ‫متدي‬ ‫و‬ ‫بدهد‬ ‫تشويقي‬ ‫مرخصي‬ ‫روز‬
.‫كند‬ ‫چاپ‬ ‫را‬ ‫كارمند‬ ‫هر‬ ‫مرخصي‬
‫تمرين‬٢‫نام‬ ‫با‬ ‫جنرالي‬ ‫كﻼس‬ :Animal‫عمل‬ ‫و‬ ‫خواص‬ ‫از‬ ‫مورد‬ ‫چند‬ ‫و‬ ‫كنيد‬ ‫ايجاد‬‫حيوانات‬ ‫مشترك‬ ‫كردهاي‬
‫نماييد‬ ‫پياده‬ ‫را‬ ‫ميكنيد‬ ‫انتخاب‬ ‫خودتان‬ ‫كه‬ ‫حيواناتي‬ ‫به‬ ‫مربوط‬ ‫كﻼس‬ ‫مورد‬ ‫دو‬ ‫سپس‬ .‫نماييد‬ ‫تعريف‬ ‫آن‬ ‫در‬ ‫را‬
‫كﻼس‬ ‫خواص‬ ‫كه‬ ‫بطوري‬Animal.‫ببرد‬ ‫ارث‬ ‫به‬ ‫را‬
Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid
٧
Casting Objects:
We have seen that an object is of the data type of the class from which it was instantiated.
Example:
public MountainBike myBike = new MountainBike();
then myBike is of type MountainBike. MountainBike is descended from Bicycle and
Object.
Therefore, a MountainBike is a Bicycle and is also an Object, and it can be used wherever
Bicycle or Object objects are called for.
The reverse is not necessarily true: a Bicycle may be a MountainBike, but it isn't
necessarily. Similarly, an Object may be a Bicycle or a MountainBike, but it isn't
necessarily.
Casting shows the use of an object of one type in place of another type, among the objects
permitted by inheritance and implementations.
Example:
Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid
٨
if we write:
Object obj = new MountainBike();
then obj is both an Object and a MountainBike (until such time as obj is assigned another
object that is not a MountainBike). This is called implicit casting.
If, on the other hand, we write
MountainBike tempBike = obj;
we would get a compiletime error because obj is not known to the compiler to be a
MountainBike.
MountainBike tempBike = (MountainBike)obj;
This cast inserts a runtime check that obj is assigned a MountainBike so that the compiler
can safely assume that obj is a MountainBike.
Note: If obj is not a MountainBike at runtime, an exception will be thrown.
Note: You can make a logical test as to the type of a particular object using the
instanceof operator.
if (obj instanceof MountainBike) {
MountainBike tempBike = (MountainBike)obj;
}
Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid
٩
Abstraction
Abstraction is a process of hiding the implementation details and showing only
functionality 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.
Abstraction lets you focus on what the object does instead of how it does it.
Data abstraction is a process of refining away the unimportant detail of an object.
Abstraction tries to reduce and factor out details so that the programmer can focus on a
few concepts at a time. Abstraction captures only those details about an object that are
relevant to the current perspective.
Abstract method:
An abstract method is a method that is declared without an implementation (without
braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
Abstract class:
A class that is declared with abstract keyword, is known as abstract class in java. it
may or may not include abstract methods. It also can have non-abstract methods (method
with body).
 if a class has at least one abstract method, then the class must be declared
abstract.
 Abstract classes cannot be instantiated, but they can be subclassed, so to use an
abstract class, you have to inherit it from another class, provide implementations to
the abstract methods in it.
 If you inherit an abstract class, you have to provide implementations to all the
abstract methods in it.
public abstract class Employee {
private String name;
private String address;
private int number;
public Employee(String name, String address, int number) {
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
public double computePay() {
Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid
١
System.out.println("Inside Employee computePay");
return 0.0;
}
public void mailCheck() {
System.out.println("Mailing a check to " + this.name + " " + this.address);
}
public String toString() {
return name + " " + address + " " + number;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setAddress(String newAddress) {
address = newAddress;
}
public int getNumber() {
return number;
}
}
public class BehsazanProgrammer extends EmployeeAbstractClass {
private double salary; // Annual salary
public BehsazanProgrammer(String name, String address, int number, double salary) {
super(name, address, number);
setSalary(salary);
}
public void mailCheck() {
System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName() + " with salary " +
salary);
}
public double getSalary() {
return salary;
}
public void setSalary(double newSalary) {
if(newSalary >= 0.0) {
salary = newSalary;
}
}
public double computePay() {
System.out.println("Computing salary pay for " + getName());
return salary/52;
}
}
Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid
١
Interface Class:
An interface is a reference type in Java. It is similar to a class that can contain only
constants, method signatures, default methods, static methods, and nested types.
Method bodies exist only for default methods and static methods. Writing an interface is
similar to writing a class. But a class describes the attributes and behaviors of an object.
And an interface contains behaviors that a class implements:
 The interface keyword is used to declare an interface.
 Interfaces cannot be instantiated. They can only be implemented by classes or
extended by other interfaces
 An interface does not contain any constructors.
 An interface cannot contain instance fields. The only fields that can appear in an
interface must be declared both static and final (constants), you can omit these
modifiers.
 An interface can extend multiple interfaces.
Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid
١
Example 1:
public interface Shape {
public void getArea();
public void getPerimeter();
public void draw();
}
public class Rectangle implements Shape{
public void getArea() {
System.out.println("The area of Rectangle is: ");
}
public void getPerimeter() {
System.out.println("The Perimeter of Rectangle is: ");
}
public void draw() {
System.out.println("Drawing Rectangle.");
}
}
public class Circle implements Shape{
public void getArea() {
System.out.println("The area of Circle is: ");
}
public void getPerimeter() {
System.out.println("The Perimeter of Circle is: ");
}
public void draw() {
System.out.println("Drawing Circle.");
}
}
Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid
١
Example 2:
public interface Color {
public void applyColor();
public void setTransparency();
}
public class RedColor implements Color{
public void applyColor() {
System.out.println("applying red color.");
}
public void setTransparency() {
System.out.println("applying color transparency.");
}
}
Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid
١
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple interfaces i.e.
known as multiple inheritance.
Example:

More Related Content

What's hot

Unit1 introduction to Java
Unit1 introduction to JavaUnit1 introduction to Java
Unit1 introduction to JavaDevaKumari Vijay
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Javabackdoor
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with pythonArslan Arshad
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Java Programming Guide Quick Reference
Java Programming Guide Quick ReferenceJava Programming Guide Quick Reference
Java Programming Guide Quick ReferenceFrescatiStory
 
Most Asked Java Interview Question and Answer
Most Asked Java Interview Question and AnswerMost Asked Java Interview Question and Answer
Most Asked Java Interview Question and AnswerTOPS Technologies
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+javaYe Win
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonSujith Kumar
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...Sagar Verma
 
Java Cheat Sheet
Java Cheat SheetJava Cheat Sheet
Java Cheat SheetGlowTouch
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Sagar Verma
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceOUM SAOKOSAL
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfacesDevaKumari Vijay
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satyaSatya Johnny
 

What's hot (20)

Multi threading
Multi threadingMulti threading
Multi threading
 
Unit1 introduction to Java
Unit1 introduction to JavaUnit1 introduction to Java
Unit1 introduction to Java
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Java cheat sheet
Java cheat sheet Java cheat sheet
Java cheat sheet
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
Java Programming Guide Quick Reference
Java Programming Guide Quick ReferenceJava Programming Guide Quick Reference
Java Programming Guide Quick Reference
 
Most Asked Java Interview Question and Answer
Most Asked Java Interview Question and AnswerMost Asked Java Interview Question and Answer
Most Asked Java Interview Question and Answer
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
02 basic java programming and operators
02 basic java programming and operators02 basic java programming and operators
02 basic java programming and operators
 
Java Cheat Sheet
Java Cheat SheetJava Cheat Sheet
Java Cheat Sheet
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
 
Project Coin
Project CoinProject Coin
Project Coin
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 

Similar to Java inheritance

Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...Ayes Chinmay
 
Object Oriented Javascript part2
Object Oriented Javascript part2Object Oriented Javascript part2
Object Oriented Javascript part2Usman Mehmood
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdfvenud11
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and PolymorphismKartikKapgate
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxRudranilDas11
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritanceKalai Selvi
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Abid Kohistani
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.pptParikhitGhosh1
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascriptUsman Mehmood
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentSuresh Mohta
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 

Similar to Java inheritance (20)

Java basics
Java basicsJava basics
Java basics
 
Inheritance
InheritanceInheritance
Inheritance
 
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
 
Object Oriented Javascript part2
Object Oriented Javascript part2Object Oriented Javascript part2
Object Oriented Javascript part2
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdf
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Only oop
Only oopOnly oop
Only oop
 
11.Object Oriented Programming.pdf
11.Object Oriented Programming.pdf11.Object Oriented Programming.pdf
11.Object Oriented Programming.pdf
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
java_inheritance.pdf
java_inheritance.pdfjava_inheritance.pdf
java_inheritance.pdf
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 

More from Hamid Ghorbani

More from Hamid Ghorbani (13)

Spring aop
Spring aopSpring aop
Spring aop
 
Spring boot jpa
Spring boot jpaSpring boot jpa
Spring boot jpa
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Payment Tokenization
Payment TokenizationPayment Tokenization
Payment Tokenization
 
Reactjs Basics
Reactjs BasicsReactjs Basics
Reactjs Basics
 
Rest web service
Rest web serviceRest web service
Rest web service
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Java collections
Java collectionsJava collections
Java collections
 
IBM Integeration Bus(IIB) Fundamentals
IBM Integeration Bus(IIB) FundamentalsIBM Integeration Bus(IIB) Fundamentals
IBM Integeration Bus(IIB) Fundamentals
 
ESB Overview
ESB OverviewESB Overview
ESB Overview
 
Spring security configuration
Spring security configurationSpring security configuration
Spring security configuration
 
SOA & ESB in banking systems(Persian language)
SOA & ESB in banking systems(Persian language)SOA & ESB in banking systems(Persian language)
SOA & ESB in banking systems(Persian language)
 

Recently uploaded

BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutionsmonugehlot87
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?Watsoo Telematics
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 

Recently uploaded (20)

Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutions
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 

Java inheritance

  • 1. Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid ١ Inheritance Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another class. With the use of inheritance the information is made manageable in a hierarchical order. The class which inherits the properties of other is known as subclass (derived class, extended class,child class) and the class whose properties are inherited is known as superclass (base class, parent class). In Java every class has one and only one direct superclass (single inheritance).The extends keyword is used to inherit the properties of a class. ‫شيء‬ ‫يك‬ ‫هاي‬ ‫ويژگي‬ ‫از‬ ‫برخي‬ ‫يا‬ ‫و‬ ‫تمامي‬ ‫كه‬ ‫كند‬ ‫ايجاد‬ ‫شيء‬ ‫يك‬ ‫بخواهد‬ ‫نويس‬ ‫برنامه‬ ‫اگر‬ ،‫خﻼصه‬ ‫طور‬ ‫به‬ ‫كند‬ ‫استفاده‬ ‫جاوا‬ ‫نويسي‬ ‫برنامه‬ ‫زبان‬ ‫در‬ ‫بري‬ ‫ارث‬ ‫قابليت‬ ‫از‬ ‫تواند‬ ‫مي‬ ‫باشد‬ ‫داشته‬ ‫را‬ ‫ديگر‬. : ‫توجه‬.‫كند‬ ‫نمي‬ ‫پشتيباني‬ ‫را‬ ‫كﻼس‬ ‫چند‬ ‫از‬ ‫بري‬ ‫ارث‬ ‫جاوا‬
  • 2. Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid ٢
  • 3. Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid ٣ What You Can Do in a Subclass A subclass inherits all the members (fields, methods, and nested classes) from its superclass, no matter what package the subclass is in. You can use the inherited members as is, replace them, hide them, or supplement them with new members. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. You can use the inherited members as is, replace them, hide them, or supplement them with new members:  With Inheritance, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.  The inherited fields can be used directly, just like any other fields.  You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).  You can declare new fields in the subclass that are not in the superclass.  The inherited methods can be used directly as they are.  You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.  You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.  You can declare new methods in the subclass that are not in the superclass.  You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.
  • 4. Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid ۴ Overriding: In object-oriented terms, overriding means to override the functionality of an existing method. Both subclass and parent class must have the same signature (name, plus the number and the type of its parameters and return type). The benefit of overriding is: ability to define a behavior that's specific to the subclass type, which means a subclass can implement a parent class method based on its requirement.  Instance Methods: An instance method in a subclass overrides the superclass's method.  Static Methods: A static method in a subclass hides the superclass's method.
  • 5. Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid ۵ Overloading: we can use two method in same class that have same name. Polymorphism: The occurrence of an object in several different forms Polymorphism is the ability of an object to take on many forms. More specifically, it is the ability to redefine methods for derived classes. (Overriding, Overloading) Example: public class Employee { int age; int dailySalary = 40000; String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getDailySalary() { return dailySalary; } public void setDailySalary(int dailySalary) { this.dailySalary = dailySalary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int calculateMonthlySalary(int days) { return this.dailySalary * days; } } public class Programmer extends Employee{
  • 6. Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid ۶ int prgBonus; String programLanguage; public int getPrgBonus() { return prgBonus; } public void setPrgBonus(int prgBonus) { this.prgBonus = prgBonus; } public String getProgramLanguage() { return programLanguage; } public void setProgramLanguage(String programLanguage) { this.programLanguage = programLanguage; } public int calculateMonthlySalary(int days) { int baseSalary = super.calculateMonthlySalary(days); return this.prgBonus + baseSalary; } } ‫تمرين‬١‫كﻼس‬ :Employee‫و‬Programmer‫ازاي‬ ‫به‬ ‫نويسها‬ ‫برنامه‬ ‫به‬ ‫كه‬ ‫كنيد‬ ‫نويسي‬ ‫باز‬ ‫طوري‬ ‫را‬ ‫موفق‬ ‫پروژه‬ ‫هر‬٢‫مانده‬ ‫كه‬ ‫كنيد‬ ‫تعريف‬ ‫كﻼسها‬ ‫از‬ ‫كدام‬ ‫هر‬ ‫براي‬ ‫متدي‬ ‫و‬ ‫بدهد‬ ‫تشويقي‬ ‫مرخصي‬ ‫روز‬ .‫كند‬ ‫چاپ‬ ‫را‬ ‫كارمند‬ ‫هر‬ ‫مرخصي‬ ‫تمرين‬٢‫نام‬ ‫با‬ ‫جنرالي‬ ‫كﻼس‬ :Animal‫عمل‬ ‫و‬ ‫خواص‬ ‫از‬ ‫مورد‬ ‫چند‬ ‫و‬ ‫كنيد‬ ‫ايجاد‬‫حيوانات‬ ‫مشترك‬ ‫كردهاي‬ ‫نماييد‬ ‫پياده‬ ‫را‬ ‫ميكنيد‬ ‫انتخاب‬ ‫خودتان‬ ‫كه‬ ‫حيواناتي‬ ‫به‬ ‫مربوط‬ ‫كﻼس‬ ‫مورد‬ ‫دو‬ ‫سپس‬ .‫نماييد‬ ‫تعريف‬ ‫آن‬ ‫در‬ ‫را‬ ‫كﻼس‬ ‫خواص‬ ‫كه‬ ‫بطوري‬Animal.‫ببرد‬ ‫ارث‬ ‫به‬ ‫را‬
  • 7. Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid ٧ Casting Objects: We have seen that an object is of the data type of the class from which it was instantiated. Example: public MountainBike myBike = new MountainBike(); then myBike is of type MountainBike. MountainBike is descended from Bicycle and Object. Therefore, a MountainBike is a Bicycle and is also an Object, and it can be used wherever Bicycle or Object objects are called for. The reverse is not necessarily true: a Bicycle may be a MountainBike, but it isn't necessarily. Similarly, an Object may be a Bicycle or a MountainBike, but it isn't necessarily. Casting shows the use of an object of one type in place of another type, among the objects permitted by inheritance and implementations. Example:
  • 8. Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid ٨ if we write: Object obj = new MountainBike(); then obj is both an Object and a MountainBike (until such time as obj is assigned another object that is not a MountainBike). This is called implicit casting. If, on the other hand, we write MountainBike tempBike = obj; we would get a compiletime error because obj is not known to the compiler to be a MountainBike. MountainBike tempBike = (MountainBike)obj; This cast inserts a runtime check that obj is assigned a MountainBike so that the compiler can safely assume that obj is a MountainBike. Note: If obj is not a MountainBike at runtime, an exception will be thrown. Note: You can make a logical test as to the type of a particular object using the instanceof operator. if (obj instanceof MountainBike) { MountainBike tempBike = (MountainBike)obj; }
  • 9. Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid ٩ Abstraction Abstraction is a process of hiding the implementation details and showing only functionality 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. Abstraction lets you focus on what the object does instead of how it does it. Data abstraction is a process of refining away the unimportant detail of an object. Abstraction tries to reduce and factor out details so that the programmer can focus on a few concepts at a time. Abstraction captures only those details about an object that are relevant to the current perspective. Abstract method: An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: abstract void moveTo(double deltaX, double deltaY); Abstract class: A class that is declared with abstract keyword, is known as abstract class in java. it may or may not include abstract methods. It also can have non-abstract methods (method with body).  if a class has at least one abstract method, then the class must be declared abstract.  Abstract classes cannot be instantiated, but they can be subclassed, so to use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.  If you inherit an abstract class, you have to provide implementations to all the abstract methods in it. public abstract class Employee { private String name; private String address; private int number; public Employee(String name, String address, int number) { System.out.println("Constructing an Employee"); this.name = name; this.address = address; this.number = number; } public double computePay() {
  • 10. Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid ١ System.out.println("Inside Employee computePay"); return 0.0; } public void mailCheck() { System.out.println("Mailing a check to " + this.name + " " + this.address); } public String toString() { return name + " " + address + " " + number; } public String getName() { return name; } public String getAddress() { return address; } public void setAddress(String newAddress) { address = newAddress; } public int getNumber() { return number; } } public class BehsazanProgrammer extends EmployeeAbstractClass { private double salary; // Annual salary public BehsazanProgrammer(String name, String address, int number, double salary) { super(name, address, number); setSalary(salary); } public void mailCheck() { System.out.println("Within mailCheck of Salary class "); System.out.println("Mailing check to " + getName() + " with salary " + salary); } public double getSalary() { return salary; } public void setSalary(double newSalary) { if(newSalary >= 0.0) { salary = newSalary; } } public double computePay() { System.out.println("Computing salary pay for " + getName()); return salary/52; } }
  • 11. Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid ١ Interface Class: An interface is a reference type in Java. It is similar to a class that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements:  The interface keyword is used to declare an interface.  Interfaces cannot be instantiated. They can only be implemented by classes or extended by other interfaces  An interface does not contain any constructors.  An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final (constants), you can omit these modifiers.  An interface can extend multiple interfaces.
  • 12. Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid ١ Example 1: public interface Shape { public void getArea(); public void getPerimeter(); public void draw(); } public class Rectangle implements Shape{ public void getArea() { System.out.println("The area of Rectangle is: "); } public void getPerimeter() { System.out.println("The Perimeter of Rectangle is: "); } public void draw() { System.out.println("Drawing Rectangle."); } } public class Circle implements Shape{ public void getArea() { System.out.println("The area of Circle is: "); } public void getPerimeter() { System.out.println("The Perimeter of Circle is: "); } public void draw() { System.out.println("Drawing Circle."); } }
  • 13. Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid ١ Example 2: public interface Color { public void applyColor(); public void setTransparency(); } public class RedColor implements Color{ public void applyColor() { System.out.println("applying red color."); } public void setTransparency() { System.out.println("applying color transparency."); } }
  • 14. Hamid Ghorbani Java Tutorial (Inheritance) https://ir.linkedin.com/in/ghorbanihamid ١ Multiple inheritance in Java by interface If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance. Example: