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

Lecture 7 - Language Features

The document discusses advanced Java programming concepts including Run-Time Type Identification (RTTI), Reflection, Generics, Lambda Expressions, and default/static methods in interfaces. It highlights the limitations of polymorphism in inheritance hierarchies and introduces solutions like the instanceof operator and downcasting. Additionally, it addresses potential issues such as the Diamond problem in multiple inheritance scenarios and provides references for further reading.

Uploaded by

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

Lecture 7 - Language Features

The document discusses advanced Java programming concepts including Run-Time Type Identification (RTTI), Reflection, Generics, Lambda Expressions, and default/static methods in interfaces. It highlights the limitations of polymorphism in inheritance hierarchies and introduces solutions like the instanceof operator and downcasting. Additionally, it addresses potential issues such as the Diamond problem in multiple inheritance scenarios and provides references for further reading.

Uploaded by

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

Askar Khaimuldin

Senior-lecturer
[email protected]
Astana IT University
▪ Run-Time Type Identification (RTTI)
▪ Reflection
▪ Generics
▪ Lambda Expressions
▪ Default methods in Interfaces
▪ Static methods in Interfaces
▪ Although polymorphism and dynamic binding are powerful
tools, they are not sufficient to take care of all the issues that
arise when dealing with an inheritance hierarchy.
▪ Consider having classes as Shape, Circle, Rectangle and
ShapeList which has several shapes. What if we need to
calculate a quantity of circles in a ShapeList?
▪ instanceof operator
▪ Downcasting
▪ Anything else?
▪ Reflection allows an executing Java program to examine or
"introspect" upon itself, and manipulate internal properties of the
program
▪ Based on the notion of a special class known as Class
▪ A reference to Class can be obtained using the getClass() method

▪ A Class has several methods that can be invoked to find out


various properties of the class (name, list of fields and methods,
etc.)
▪ Generics (generic types and methods)
allow us to get away from the rigid
definition of the types used
▪ In the example, we assume that all
numbers are integers, therefore, it could
not be used for doubles
▪ Should create a class DoubleNumbers
and provide the same content?
▪ Java Generic methods and generic
classes enable programmers to specify,
with a single method declaration, a set of
related methods, or with a single class
declaration, a set of related types,
respectively
▪ Generics (generic types and methods)
allow us to get away from the rigid
definition of the types used
▪ In the example, we assume that all
numbers are integers, therefore, it could
not be used for doubles
▪ Should create a class DoubleNumbers
and provide the same content?
▪ Java Generic methods and generic
classes enable programmers to specify,
with a single method declaration, a set of
related methods, or with a single class
declaration, a set of related types,
respectively
▪ A lambda expression represents an anonymous method—that is, a
method without a name.
▪ A lambda consists of a parameter list followed by the arrow token
(->) and a body

▪ Lambda expressions (or lambdas for short) enable you to create


methods that can be treated as data.
▪ you can pass lambda expressions as arguments to other methods,
▪ assign lambda expressions to variables for later use
▪ and return lambda expressions from methods.
▪ Introduced in Java SE 8
▪ interfaces also may contain public default methods with concrete
default implementations
▪ They specify how operations are performed when an
implementing class does not override the methods
▪ If a class implements such an interface, the class also receives the
interface’s default implementations (if any)
▪ To declare a default method, place the keyword default before the
method’s return type and provide a concrete method
implementation
▪ Diamond problem could occur when we deal with
multiple inheritance
▪ The core problem is that an object of type D could
have more than one implementation of the same
method
▪ Initially, it was assumed that Java cannot have a
Diamond problem, since it does not provide multiple
inheritance
▪ But with the help of default methods in interfaces it
becomes possible
▪ Static Methods in Interface are those methods, which are
defined in the interface with the keyword static
▪ Unlike other methods in Interface, these static methods contain
the complete definition of the function
▪ Since the definition is complete and the method is static,
therefore these methods cannot be overridden or changed in
the implementation class
Object-Oriented Analysis, Design and Implementation, 2nd
Edition, Brahma Dathan and Sarnath Ramnath, Springer
▪ Chapter 4.4

Java: How to Program (Early Objects), 11th Edition, by Paul Deitel


and Harvey Deitel, Pearson
▪ Chapters 8.4, 17.3, 10.9 – 10.13

You might also like