Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Java Unit 4

Uploaded by

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

Java Unit 4

Uploaded by

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

Inheritance & Packaging

Inheritance: is a mechanism by which a new class is created from an existing class.


The new class is called the subclass or derived class, and the existing class is called the
superclass or base class. The subclass inherits all the properties and methods of the
superclass, and can also add new properties and methods of its own.

using the `extends` keyword in Java. When a class extends another class, it
inherits all the members (fields and methods) of the parent class, and can also add its
own members.

Subclasses and Superclasses:


A subclass is a class that is derived from another class (called the superclass). A subclass
inherits all the members of its superclass, and can add its own members. A superclass
can have multiple subclasses, but a subclass can only have one superclass.

'super' keyword usage:


The `super` keyword in Java is used to refer to the superclass of the current object. It can
be used to call the superclass constructor, access the superclass's methods and variables,
and call the superclass's overridden methods.

Overriding Methods:
In Java, a subclass can provide its own implementation for a method that is already
defined in its superclass. This is known as method overriding. To override a method, the
subclass must define a method with the same name, return type, and parameter list as
the superclass's method.

Dynamic Method Dispatch:


Dynamic method dispatch is a mechanism in Java that allows a subclass object to invoke
an overridden method of the superclass. The Java virtual machine determines the
appropriate method to call at runtime, based on the type of the object at the time of the
call.

The Object class:


The Object class is a superclass of all classes in Java. Every class in Java is a subclass of
Object. The Object class provides a set of common methods that are inherited by all
classes, such as `toString()`, `equals()`, and `hashCode()`.

Abstract and Final Classes:


An abstract class is a class that cannot be instantiated, but can be subclassed. An
abstract class can have abstract methods (methods that have no implementation) as
well as concrete methods (methods with an implementation).

Packaging: is a way to organize Java classes into groups or modules. A package is a


collection of related classes that can be stored together in a directory hierarchy.
Packages are used to avoid naming conflicts between classes and to provide a logical
organization of the code.

Access Control: Access control in Java is used to restrict access to the classes,
methods, and variables of a package or class. There are four access modifiers in Java -
public, private, protected, and default. These access modifiers are used to control the
visibility of classes, methods, and variables.

Interfaces: An interface in Java is a collection of abstract methods that can be


implemented by a class. It defines a set of methods that a class should implement. An
interface can also contain constant variables and default methods.
Defining an Interface: To define an interface, you need to use the 'interface' keyword
followed by the name of the interface. You can declare methods and variables in the
interface, but you cannot define them. The methods declared in the interface are by
default abstract and public.

Implementing an Interface: To implement an interface, you need to use the


'implements' keyword followed by the name of the interface. Once you implement an
interface, you need to provide an implementation for all the abstract methods declared
in the interface.

Applying Interfaces: Interfaces are used in Java to achieve polymorphism. By


implementing an interface, a class can be used as an instance of that interface. This
allows you to write generic code that can work with any class that implements that
interface.

You might also like