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

005 - Writing Java Classes - 2

This document summarizes key concepts in Java including inheritance, polymorphism through method overriding, the super keyword, abstract classes, interfaces, and the final modifier applied to classes, methods, and variables. Inheritance allows classes to extend existing classes to reuse properties and methods. Method overriding involves subclasses overriding methods from the parent class. Abstract classes define common behaviors without providing implementation, while interfaces define behaviors without implementation.

Uploaded by

shakir.hello
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

005 - Writing Java Classes - 2

This document summarizes key concepts in Java including inheritance, polymorphism through method overriding, the super keyword, abstract classes, interfaces, and the final modifier applied to classes, methods, and variables. Inheritance allows classes to extend existing classes to reuse properties and methods. Method overriding involves subclasses overriding methods from the parent class. Abstract classes define common behaviors without providing implementation, while interfaces define behaviors without implementation.

Uploaded by

shakir.hello
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Writing java classes II

Shakir Hussain
Will cover :
y Inheritance
y Polymorphism : Method Overriding
y Keyword : super
y Abstract Class
y Interface
y final : class, method, variable
Inheritance
y Inheritance is the capability of a class to use
the properties and methods of another class
while adding its own functionality.

Example : You could create a generic employee


class with states and actions that are
common to all employees. Then more
specific classes could be defined for salaried,
commissioned and hourly employees.
Inheritance..

Generic class is known as Parent/Super/Base class


Specific class is known as Child/Sub/Derived class
Inheritance...
Another Example

y See , common features


y Create a generic class which holds all common
features
y All other classes will inherit it and use it.
Inheritance….
Inheritance…..
y Inheritance is Generalization to
Specialization
y Inheritance is nothing but creating new
class based on existing class.
y Is-A relationship
y “extends” keyword is used for inheritance
in java.
y private members are not inherited.
y constructors are not inherited.
Keyword : super
y If your method overrides one of its super
class's methods, you can invoke the
overridden method through the use of
the keyword super

y Can Invoke super class constructor as :


super()
Invocation of a super class constructor
must be the first line in the subclass
constructor.
Method Overriding
y Method Overriding is achieved when a
subclass overrides non-static methods
defined in the super class, following which
the new method implementation in the
subclass that is executed.
y The new method definition must have the
same method signature (i.e., method name
and parameters) and return type.
y The new method definition cannot narrow
the accessibility of the method, but it can
widen it
Abstract Class
y Facility to collect generic / common
features into a single super class.
y One or more methods are declared but
not defined.
y Advantages :
◦ Useful to achieve polymorphism.
◦ Helps to create base classes that are generic
and implementation is not available.
Abstract Class…
y Java allows creation of a super class that
declares a method but does not supply any
implementation. Such a method is called
abstract method
y Any class with zero or more abstract
method(s) is called an abstract class
y You cannot create instances of abstract
classes
y Abstract classes may have data attributes,
concrete methods, and constructors
y It is a good practice to make constructors
protected
Abstract Class…
y If a sub-class does not provide
implementation for abstract methods, it
must be declared abstract
y A failure to do so will result in compiler
errors
Interface
y An interface is a contract between client
code and the class that implements that
interface
y In Java it is identified by the “interface”
keyword
y A class can implement many, unrelated
interfaces
y Many unrelated classes can implement the
same interface
Interface…
y Concrete classes that implement an
interface must implement every method
in the interface
y The interface name can be used as a type
of reference
variable. The usual dynamic binding takes
place
y You can use the instanceof operator to
determine if an object’s class implements
an interface
Interface…
The valid combinations are

y class extends class


y class implements interface
y interface extends interface

All other combinations are invalid.


Abstract Classes & Interfaces
• Abstract classes are used • Interfaces can be
only when there is a “is-a” implemented by classes that
type of relationship between are not related to one
the classes. another.
• You cannot extend more • You can implement more
than one abstract class. than one interface.
• Abstract class can contain • Interfaces contain only
abstract as well as abstract methods.
implemented methods
Final Classes
y Java allows you to apply final to classes
y Final classes cannot be sub-classed
y An ideal usage scenario is where you
don’t want to allow
y your class to be extended for security
reasons
y E.g. String class is declared final
Final methods
y Java allows you to apply final to methods
y Final methods cannot be overridden
y An ideal usage scenario is where you don’t want to
allow your method implementations to change to
ensure a consistent state of your object
y Methods declared final are sometimes used for
optimizations
y When used in situations where class hierarchies exist,
using final with methods leads to significant
performance enhancements
y Methods marked static or private are automatically
marked final because dynamic binding cannot be
applied in either case
Final variables
y Variables marked final are treated as
constants
y Any attempt to change a final variable
results in a compiler error
y If you mark a reference type variable final,
you cannot use it to refer to any other
object.You can however change the
object’s contents

You might also like