0% found this document useful (0 votes)
11 views

JAVA 4

Uploaded by

aelshahed2027
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

JAVA 4

Uploaded by

aelshahed2027
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

JAVA Programming

Fundamental & OOP


Eng. Nada Mohamed Sarhan
[email protected]
OUTLINES
• OOP Concept
• Inheritance
• Polymorphism
• Overloading
• Overriding
• Encapsulation
• Abstraction
• Interfaces
• Packages
• The Collections Framework
OOP concept
• What is OOP?
• Is a programming technique in which programs are written on the
basis of object.
• Ex:
• JAVA
• C++
• Python
OOP concept
• Benefits of OOP:
• Reusability
• Extensibility
• Decomposability
• Composability
• Understandability
• Security
OOP concept
• OOP characteristic:
Question
• What is the default constructor for the following class?
public class Penny {
String name = "lane";
}
a. public Penny(String name)
b. public Penny()
c. class()
d. String()
e. private Penny()
Inheritance
Inheritance
Inheritance
• A program technique that is used to reuse an existing class to build a
new classis known as inheritance.
• Inheritance can be defined as the process where one class acquires
the properties (methods and fields) of another. 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, child class) and the class whose properties are
inherited is known as superclass (base class, parent class).
Inheritance
• Extends Keyword
• extends is the keyword used to inherit the properties of a class.
Following is the syntax of extends keyword
Inheritance
• The Superclass reference variable can hold the subclass object, but
using that variable you can access only the members of the
superclass, so to access the members of both classes it is
recommended to always create reference variable to the subclass.
• A subclass inherits all the members (fields, methods, and nested
classes) from its superclass. Constructors are not members, so they
are not inherited by subclasses, but the constructor of the superclass
can be invoked from the subclass.
Inheritance
• The super keyword
• The super keyword is similar to this keyword. Following are the
scenarios where the super keyword is used.
• It is used to differentiate the members of superclass from the
members of subclass, if they have same names.
• It is used to invoke the superclass constructor from subclass.
Inheritance
• Invoking Superclass Constructor
• If a class is inheriting the properties of another class, the subclass
automatically acquires the default constructor of the superclass. But if
you want to call a parameterized constructor of the superclass, you
need to use the super keyword
• Constructors are not inherited .
Question
• When a subclass is instantiated , the superclass default constructor is
executed first?

A. True
B. False
Inheritance
• Is-A Relationship
• IS-A is a way of saying: This object is a type of that object. Let us see
how the extends keyword is used to achieve inheritance.
Inheritance
• Now, based on the above example, in Object-Oriented terms, which
the following are true −
1. Animal is the subclass of Mammal class.
2. Animal is the superclass of Reptile class.
3. Mammal and Reptile are subclasses
of Animal class.
4. Dog is the subclass of both Mammal
and Animal classes.
Inheritance
• Now, if we consider the IS-A relationship, we can say −
• Mammal IS-A Animal
• Reptile IS-A Animal
• Dog IS-A Mammal
• Hence: Dog IS-A Animal as well
Inheritance
• Level of inheritance:
1) single
2) Multi level
3) Hierarchical
4) Multiple
5) Hybrid
Inheritance (single)
• When a sub class inherit the property from only one base class, then
it is known as Single Inheritance.
Inheritance (Multi level)
• When a sub class inherit the property fro the class that itself inherit
from another class, then it is known as Multi Level inheritance.
Inheritance (Hierarchical)
• When several sub class inherit the property from single base class,
then it is known as Hierarchical Inheritance.
Inheritance (Multiple)
• When a sub class inherit the property from several base class, then it
is known as Multiple Inheritance.
Inheritance (hybrid)
• In Hybrid Inheritance several inheritance forms are combined.
Polymorphism
• The word polymorphism is combination of two words poly and
morphism. Poly means many and Morphism means form.
• In OOP polymorphism is the ability of object of different types to
respond to functions of the same name. (single method having
multiple functions under the same name. A single-action gets
executed in different ways.)
• The most common use of polymorphism in OOP occurs when a parent
class reference is used to refer to a child class object.
• The user doesn’t have to know the exact type of the object in
advance. The behavior of the object can be implemented at run time.
Polymorphism
• Overloading
• Overriding
Polymorphism (Overriding)
• 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.
• In object-oriented terms:
• Overriding means to override the functionality of an existing method.
• Constructors cannot be overridden.
Polymorphism (Overriding)
Polymorphism (Rules for method
Overriding)
• The argument list should be exactly the same as that of the overridden
method.
• The return type should be the same or a subtype of the return type declared
in the original overridden method in the superclass.
• The access level cannot be more restrictive than the overridden method's
access level. For example: If the superclass method is declared public then the
overriding method in the sub class cannot be either private or protected.
• Instance methods can be overridden only if they are inherited by the subclass.
• A method declared final cannot be overridden.
• A method declared static cannot be overridden but can be re-declared.
Exercise
• Write a Java program to create a class called Animal with a method called
makeSound(). Create 2 subclasses called Cat and Dog that overrides the
makeSound() method to bark.
• Write a Java program to create a class called Shape with a method called
getArea(). Create 2 subclass called Rectangle and Square that overrides the
getArea() method to calculate the area of (user inputs).
• Write a Java program that creates a class hierarchy for employees of a
company. The base class should be Employee, with subclasses Manager,
Developer, and Programmer. Each subclass should have properties such as
name, address, salary, and job title. Implement methods for calculating
bonuses, generating performance reports, and unique method managing
dev, managing pro, managing manger.

You might also like