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.
شيء يك هاي ويژگي از برخي يا و تمامي كه كند ايجاد شيء يك بخواهد نويس برنامه اگر ،خﻼصه طور به
كند استفاده جاوا نويسي برنامه زبان در بري ارث قابليت از تواند مي باشد داشته را ديگر.
: توجه.كند نمي پشتيباني را كﻼس چند از بري ارث جاوا
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{
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: