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

JAVA 6

Uploaded by

aelshahed2027
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

JAVA 6

Uploaded by

aelshahed2027
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

JAVA Programming

Fundamental & OOP


Eng. Nada Mohamed Sarhan
[email protected]
OUTLINES
• Abstract class
• Exercise
• The Collections Framework
• Exercise
• Assignment
Abstract Class
• A class which contains the abstract keyword in its declaration is known as
abstract class.
• Abstract classes may or may not contain abstract methods, i.e., methods
without body ( public void get(); )
• But, if a class has at least one abstract method, then the class must be
declared abstract.
• If a class is declared abstract, it cannot be instantiated.
• 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.
Abstract Class
Abstract Method
• If you want a class to contain a particular method but you want the
actual implementation of that method to be determined by child
classes, you can declare the method in the parent class as an abstract.
• abstract keyword is used to declare the method as abstract.
• You have to place the abstract keyword before the method name in
the method declaration.
• An abstract method contains a method signature, but no method
body.
• Instead of curly braces, an abstract method will have a semoi colon (;)
at the end.
Abstract Method
• Declaring a method as abstract has two consequences −
• The class containing it must be declared as abstract.
• Any class inheriting the current class must either override the abstract
method or declare itself as abstract.
• Note − Eventually, a descendant class has to implement the abstract
method; otherwise, you would have a hierarchy of abstract classes
that cannot be instantiated.
Abstract Method
Exercise
• Write a Java program to create an abstract class Animal with an
abstract method called sound(). Create subclasses Lion and Tiger that
extend the Animal class and implement the sound() method to make
a specific sound for each animal.
Exercise
• Write a Java program to create an abstract class Animal with abstract
methods eat() and sleep(). Create subclasses Lion, Tiger, and Deer
that extend the Animal class and implement the eat() and sleep()
methods differently based on their specific behavior.
Quiz
•Which methods of an object can be accessed via an interface that it
implements?
a. All the methods implemented in the object’s class
b. All the methods implemented in the object’s superclass
c. The methods declared in the interface
Quiz
•How can you change the reference type of an object?
a. By calling getReference
b. By casting
c. By declaring a new reference and assigning the object
The Collections Framework
Any group of individual objects that are represented as a single unit is known as a
Java Collection of Objects.

The collections framework is located in the java.util package. The framework


is helpful when working with lists or collections of objects. It contains:
• Interfaces
• Abstract classes
• Concrete classes (Example: ArrayList)
The Collections Framework
• Collections in java:
1. List (ArrayList, LinkedList)
2. Queue
3. Deque
4. Set (HashSet)
5. SortedSet
6. Map (TreeMap)
The Collections
Framework(ArrayList)
• Similar to an array, an ArrayList allows object storage.
• Unlike an array, an ArrayList object:
• Automatically expands when a new item is added.
• Automatically shrinks when items are removed.
• Requires:
import java.util.ArrayList;
The Collections
Framework(ArrayList)
• Creating an ArrayList:

• If we try to store any other type of object in this ArrayList an error will
occur.
The Collections
Framework(ArrayList)
The Collections
Framework(ArrayList)
The Collections
Framework(ArrayList)
Exercise
• Write a Java program to create an array list, add some colors (strings)
and print out the collection.
Exercise
• Write a Java program to update an array element by the given
element.
Exercise
• Write a Java program to insert an element into the array list at the
first position.
What we learn..
• Basic syntax • Deconstructor
• Data types • OOP Concept
• Variables • Inheritance
• Operators • Polymorphism
• Comments • Overloading
• Access modifiers • Overriding
• Conditions • Encapsulation
• Loops • Abstraction
• Methods • Interfaces
• Object & Classes • Abstract Class
• Strings
• The Collections Framework
• Arrays
• Constructors
Quiz
• What do you mean by data encapsulation?
• Data Encapsulation is an Object-Oriented Programming concept of hiding the
data attributes and their behaviours in a single unit.
• It helps developers to follow modularity while developing software by ensuring
that each object is independent of other objects by having its own methods,
attributes, and functionalities.
• It is used for the security of the private properties of an object and hence serves
the purpose of data hiding.
Quiz
• Can you implement pointers in a Java Program?
• Pointers are a feature associated with the C programming language. They are
responsible for providing the memory address in a situation where the
programmer directly stores the data. But unfortunately, you won't find any
pointers in Java
Quiz
• what is the difference between final and const keyword in java?
• The main difference between const and final is that const can be considered as a
compile-time constant while final can be considered as a run-time constant
Quiz
• Different between heap and stack.
• Heap Space is used throughout the application, but Stack is only used for the
method — or methods — currently running. The Heap Space contains all objects
are created, but Stack contains any reference to those objects. Objects stored in
the Heap can be accessed throughout the application.
Quiz
• Can you call a constructor of a class inside another constructor?
• you can call one constructor from another constructor within the same class. This
process is known as constructor chaining, and the keyword this() is used to make
this call.
Assignment
• Write a Java programming to create a banking system with three
classes - Bank, Account, SavingsAccount, and CurrentAccount. The
bank should have a list of accounts and methods for adding them.
Accounts should be an interface with methods to deposit, withdraw,
calculate interest, and view balances. SavingsAccount and
CurrentAccount should implement the Account interface and have
their own unique methods.

You might also like