Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Inheritance, Slides of Computer Science

The concept of inheritance in java programming language. It explains that inheritance is a code reuse mechanism, where a new class (subclass/derived class/child class) can be created by extending an existing class (superclass/parent class/base class). The subclass inherits the fields and methods of the superclass, allowing for code reuse and polymorphism. The document also covers topics such as abstract classes, method modifiers, inheritance vs composition, and the use of interfaces in java. It provides examples to illustrate the concepts and highlights the benefits of using inheritance in object-oriented programming. The information presented in this document could be useful for students studying java programming, object-oriented design, and software engineering principles.

Typology: Slides

2023/2024

Uploaded on 08/29/2024

vinit-jadhav
vinit-jadhav 🇮🇳

2 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Java Inheritance
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Java Inheritance and more Slides Computer Science in PDF only on Docsity!

Java Inheritance

Inheritance

  • (^) On the surface, inheritance is a code re-use issue. - (^) we can extend code that is already written in a manageable manner.
  • (^) Inheritance is more
    • (^) it supports polymorphism at the language level

Inheritance Example

  • (^) Employee: name, email, phone
    • (^) FulltimeEmployee: also has salary, office, benefits, …
      • (^) Manager: CompanyCar, can change salaries, rates contracts, offices, etc.
    • (^) Contractor: HourlyRate, ContractDuration, …
  • (^) A manager is a special kind of FullTimeEmployee, which is a special kind of Employee.

Polymorphism

  • (^) Create code that deals with general object types, without the need to know what specific type each object is.
  • (^) Generate a list of employee names:
    • (^) all objects derived from Employee have a name field since Employee has a name field
    • (^) no need to treat managers differently from anyone else.

Shapes

  • (^) Shape:
    • (^) color, layer fields
    • (^) draw() draw itself on the screen
    • (^) calcArea() calculates it's own area.
    • (^) serialize() generate a string that can be saved and later used to re-generate the object.

Kinds of Shapes

  • (^) Rectangle
  • (^) Triangle
  • (^) Circle Each could be a kind of shape (could be specializations of the shape class). Each knows how to draw itself, etc. Could write code to have all shapes draw themselves, or save the whole collection to a file.

Abstract Class modifier

  • (^) Abstract modifier means that the class can be used as a superclass only. - (^) no objects of this class can be created. - (^) can have attributes, even code - (^) all are inherited - (^) methods can be overridden
  • (^) Used in inheritance hierarchies

Interesting Method Modifiers

  • (^) private/protected/public :
    • (^) protected means private to all but subclasses
    • (^) what if none of these specified?
  • (^) abstract : no implementation given, must be supplied by subclass. - (^) the class itself must also be declared abstract
  • (^) final : the method cannot be changed by a subclass (no alternative implementation can be provided by a subclass).

Inheritance vs. Composition

  • (^) When one object type depends on another, the relationship could be: - (^) is-a - (^) has-a
  • (^) Sometimes it's hard to define the relationship, but in general you use composition (aggregation) when the relationship is has-a

Composition

  • (^) One class has instance variables that refer to object of another.
  • (^) Sometimes we have a collection of objects, the class just provides the glue. - (^) establishes the relationship between objects.
  • (^) There is nothing special happening here (as far as the compiler is concerned).

Inheritance

  • (^) A derived class object is an object of the base class. - (^) is-a, not has-a. - (^) all fields and methods are inherited.
  • (^) The derived class object also has some stuff that the base class does not provide (usually).

Java Inheritance

  • (^) Two kinds:
    • (^) implementation: the code that defines methods.
    • (^) interface: the method prototypes only.
  • (^) Other OOP languages often provide the same capabilities (but not as an explicit option).

Accessing superclass methods

from derived class.

  • (^) Can use super() to access all (non-private) superclass methods. - (^) even those replaced with new versions in the derived class.
  • (^) Can use super() to call base class constructor. - (^) use arguments to specify desired constructor

Single inheritance only

(implementation inheritance).

  • (^) You can't extend more than one class!
    • (^) the derived class can't have more than one base class.
  • (^) You can do multiple inheritance with interface inheritance.