Class Inheritance - Java Programming Language - Lecture Slides, Slides of Computer Science

These are the Lecture Slides of Java Programming Language which includes Applet Class, Passing Parameters to Applets, Conversions, Applications and Applets, Running a Program, Applet, Application, Mouse Event, Keyboard Event etc. Key important points are: Class Inheritance, Superclasses and Subclasses, Keywords, Overriding Methods, Object Class, Modifiers, Protected, Final and Abstract, Polymorphism and Object Casting, Interfaces

Typology: Slides

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv ๐Ÿ‡ฎ๐Ÿ‡ณ

4.3

(12)

194 documents

1 / 34

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 6 Class Inheritance
๏†Superclasses and Subclasses
๏†Keywords: super
๏†Overriding methods
๏†The Object Class
๏†Modifiers: protected, final and abstract
๏†Polymorphism and Object Casting
๏†Interfaces
๏†Inner Classes
๏†Program Development and Class Design
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download Class Inheritance - Java Programming Language - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Chapter 6 Class Inheritance

๏† Superclasses and Subclasses ๏† Keywords: super ๏† Overriding methods ๏† The Object Class ๏† Modifiers: protected, final and abstract ๏† Polymorphism and Object Casting ๏† Interfaces ๏† Inner Classes ๏† Program Development and Class Design

Superclasses and Subclasses

Superclass^ Circle^ Circle Methods^ Circle Data

Inheritance

Subclass^ Cylinder^ Cylinder MethodsCircle Methods Cylinder Data^ Circle Data

Circle

  • radius
  • +getRadiussetRadius +findArea

Cylinder

  • length
  • +getLengthsetLength +findVolume

Superclass Subclass

UML Diagram

Example 6.

Testing Inheritance

  • Objective: Create a Cylinder object and explore the relationship between the Cylinder and Circle classes.

TestCylinder Run

Using the Keyword super

  • To call a superclass constructor
  • To call a superclass method

The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways:

The Object Class

  • The Object class is the root of all Java classes.
  • The toString() method returns a string representation of the object.
  • The equals() method compares the contents of two objects.
  • The clone() method copy objects

The protected Modifier

  • The protected modifier can be applied on data and methods in a class.
  • A protected data or a protected method in a public class can be accessed by any class in the same package or its subclasses, even if the subclasses are in a different package.

The final Modifier

  • The final class cannot be extended:

final class Math {...}

  • The final variable is a constant:

final static double PI = 3.14159;

  • The final method cannot be

modified by its subclasses.

The abstract Modifier

  • The abstract class

โ€“ Cannot be instantiated

โ€“ Should be extended and

implemented in subclasses

  • The abstract method

โ€“ Method signature without

implementation

Polymorphism and Dynamic

Binding

Polymorphism refers to the ability to

determine at runtime which code to

run, given multiple methods with the

same name but different operations in

the same class or in different classes.

This ability is also known as dynamic

binding.

Example 6.

Testing Polymorphism

  • Objective: This example creates two geometric

objects: a circle, and a rectangle, invokes the

equalArea method to check if the two objects

have equal area, and invokes the

displayGeometricObject method to display the

objects.

TestPolymorphism (^) Run

Casting from

Superclass to Subclass

Explicit casting must be used when casting an object from a superclass to a subclass. This type of casting may not always succeed.

Cylinder myCylinder = (Cylinder)myCircle;

The instanceof Operator

Use the instanceof operator to test whether an object is an instance of a class:

Circle myCircle = new Circle();

if (myCircle instanceof Cylinder) { Cylinder myCylinder = (Cylinder)myCircle; ... }

Interfaces

  • What Is an Interface?
  • Creating an Interface
  • Implementing an Interface
  • What is Marker Interface?

Creating an Interface

modifier interface InterfaceName { constants declarations; methods signatures; }