SlideShare a Scribd company logo
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

PDF
Java Reflection
PDF
Java Threads
PDF
Java programming basics
PPTX
Basics of Java
PDF
Basic java for Android Developer
PPTX
Java interview questions 2
PDF
Object Oriented Programming in PHP
PPTX
Object oriented programming in python
Java Reflection
Java Threads
Java programming basics
Basics of Java
Basic java for Android Developer
Java interview questions 2
Object Oriented Programming in PHP
Object oriented programming in python

What's hot (20)

PDF
Multi threading
PPTX
Unit1 introduction to Java
PPT
Object and Classes in Java
PPTX
Object oriented programming with python
PPS
Introduction to class in java
DOCX
Java cheat sheet
PDF
Java Programming - 04 object oriented in java
PDF
Java Programming Guide Quick Reference
PPT
Most Asked Java Interview Question and Answer
PPT
Java Tut1
PPTX
Object+oriented+programming+in+java
PPTX
Basics of Object Oriented Programming in Python
PPTX
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
PDF
02 basic java programming and operators
PDF
Java Cheat Sheet
PPT
Core Java Programming | Data Type | operator | java Control Flow| Class 2
PDF
Java OOP Programming language (Part 5) - Inheritance
PPTX
Unit3 part3-packages and interfaces
PPT
PDF
Advance java kvr -satya
Multi threading
Unit1 introduction to Java
Object and Classes in Java
Object oriented programming with python
Introduction to class in java
Java cheat sheet
Java Programming - 04 object oriented in java
Java Programming Guide Quick Reference
Most Asked Java Interview Question and Answer
Java Tut1
Object+oriented+programming+in+java
Basics of Object Oriented Programming in Python
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
02 basic java programming and operators
Java Cheat Sheet
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Java OOP Programming language (Part 5) - Inheritance
Unit3 part3-packages and interfaces
Advance java kvr -satya
Ad

Similar to Java inheritance (20)

PPTX
Java basics
PPTX
Inheritance
PPTX
Basics to java programming and concepts of java
PPTX
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
PPTX
Object Oriented Javascript part2
PDF
1669617800196.pdf
PDF
Java Inheritance
PPTX
Inheritance and Polymorphism
PPSX
Oop features java presentationshow
PPTX
Only oop
PPTX
Oop concepts classes Method Overriding.pptx
PPTX
Inheritance & interface ppt Inheritance
PDF
11.Object Oriented Programming.pdf
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
PPTX
Unit3 inheritance
PPTX
Modules 333333333³3444444444444444444.pptx
PPTX
Polymorphism in C# Function overloading in C#
PPT
InheritanceAndPolymorphismprein Java.ppt
PPT
06 InheritanceAndPolymorphism.ppt
PPT
9781439035665 ppt ch10
Java basics
Inheritance
Basics to java programming and concepts of java
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Object Oriented Javascript part2
1669617800196.pdf
Java Inheritance
Inheritance and Polymorphism
Oop features java presentationshow
Only oop
Oop concepts classes Method Overriding.pptx
Inheritance & interface ppt Inheritance
11.Object Oriented Programming.pdf
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
Unit3 inheritance
Modules 333333333³3444444444444444444.pptx
Polymorphism in C# Function overloading in C#
InheritanceAndPolymorphismprein Java.ppt
06 InheritanceAndPolymorphism.ppt
9781439035665 ppt ch10
Ad

More from Hamid Ghorbani (13)

PDF
Spring aop
PDF
Spring boot jpa
PDF
Spring mvc
PDF
Payment Tokenization
PDF
Reactjs Basics
PDF
Rest web service
PDF
Java I/o streams
PDF
Java Generics
PDF
Java collections
PDF
IBM Integeration Bus(IIB) Fundamentals
PDF
ESB Overview
PDF
Spring security configuration
PDF
SOA & ESB in banking systems(Persian language)
Spring aop
Spring boot jpa
Spring mvc
Payment Tokenization
Reactjs Basics
Rest web service
Java I/o streams
Java Generics
Java collections
IBM Integeration Bus(IIB) Fundamentals
ESB Overview
Spring security configuration
SOA & ESB in banking systems(Persian language)

Recently uploaded (20)

PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
System and Network Administration Chapter 2
PPTX
Transform Your Business with a Software ERP System
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
Nekopoi APK 2025 free lastest update
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
PTS Company Brochure 2025 (1).pdf.......
PPT
Introduction Database Management System for Course Database
PDF
medical staffing services at VALiNTRY
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
top salesforce developer skills in 2025.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Internet Downloader Manager (IDM) Crack 6.42 Build 41
System and Network Administration Chapter 2
Transform Your Business with a Software ERP System
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Digital Systems & Binary Numbers (comprehensive )
Softaken Excel to vCard Converter Software.pdf
ai tools demonstartion for schools and inter college
Nekopoi APK 2025 free lastest update
Wondershare Filmora 15 Crack With Activation Key [2025
Operating system designcfffgfgggggggvggggggggg
PTS Company Brochure 2025 (1).pdf.......
Introduction Database Management System for Course Database
medical staffing services at VALiNTRY
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Navsoft: AI-Powered Business Solutions & Custom Software Development
top salesforce developer skills in 2025.pdf

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: