0% found this document useful (0 votes)
10 views14 pages

Chapter 08

The document discusses key concepts of classes and objects in Java, focusing on constructors, visibility modifiers, accessor and mutator methods, and inheritance. Constructors initialize new objects and differ from regular methods in several ways, while visibility modifiers control access levels to class members. Inheritance allows for code reusability by creating subclasses that extend existing classes, maintaining an 'is-a' relationship, and managing access to superclass members.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views14 pages

Chapter 08

The document discusses key concepts of classes and objects in Java, focusing on constructors, visibility modifiers, accessor and mutator methods, and inheritance. Constructors initialize new objects and differ from regular methods in several ways, while visibility modifiers control access levels to class members. Inheritance allows for code reusability by creating subclasses that extend existing classes, maintaining an 'is-a' relationship, and managing access to superclass members.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Classes and Objects

in Java
Chapter 08
Constructor

▪ Constructor is a special kind of method that is invoked when a new object is created.
▪ Constructor can perform any action; but it is mainly designed to perform initializing
actions.
▪ Every class is having its default constructor; sometimes referred to as no-argument
constructor.
▪ It initializes the attributes of a newly created object using default values based on
their data types.
▪ Constructor differs from general methods in the following ways:
– Constructor must have the same name as class name.
– Constructor does not have return type.
– Constructor is invoked implicitly only when an object is constructed using new operator.
– Constructor cannot be invoked explicitly elsewhere in the program.
– Like other methods, Constructor can also be overloaded.
Visibility Modifiers for Access
Control

▪ Access control is about controlling visibility. So, access modifiers are also
known as visibility modifiers.
▪ If a method or variable is visible to another class, then only it can be
referred in another class.
▪ To protect a method or variable from such references, we use the four
levels of visibility to provide necessary protection.
▪ The Four P's of protection are public, package (default protection),
protected and private.
▪ Access modifiers public, protected and private are used before the type
of variable or method.
▪ When no modifier is used, it is default one having visibility only within a
package that contains the class.
Visibility Modifiers for Access
Control

▪ Public
– Any method or variable is visible to the class in which it is defined.
– If we want to make it visible to all the classes outside this class, declare the
method or variable to have public access.
– This is the widest possible access.
– Example: public float length; public double area( );
Visibility Modifiers for Access
Control
▪ Package (Without any modifier)
– This is the next level of access that has no precise name.
– It is indicated by the lack of any access modifier keyword in a declaration.
– This the default level of protection.
– The scope is narrower than public variables.
– The variable or method can be accessed from anywhere in the package that contain the class but not from
outside that package.
– A source file without package statement is considered as package by default.

▪ Protected
– This level of protection is used to allow the access only to subclasses or to share with the methods declared as
“friend”.
– Thus the visibility is narrower than previous two levels; but wider than full privacy provided by fourth level
“private”.

▪ Private
– Highest level of protection can be achieved by using private protection level.
– This provides the narrowest visibility. The private methtods and variables are directly accessible only by the
methods defined with in a class.
– They cannot be seen by any other class.
Accessor and Mutator Methods

▪ When we restrict access to data by declaring them as private,


our purpose is to protect them from getting directly accessed
or modified by methods of other class.
▪ If we want to allow such data to be used by others, then we
write “accessor” methods.
▪ If we want to allow such data to be modified by others, then
we write “mutator” methods.
▪ Accessor methods are also known as “getter” and mutator
methods as “setter”.
▪ Example: void setLength(float L) { length = L; }
▪ Use of accessor and mutator methods will prevent the
variables from getting directly accessed and modified by other
users of the class.
Inheritance

▪ Object-oriented programming language provide reusability feature using


inheritance.
▪ Inheritance allows us to build new class with added capabilities by extending
existing class.
▪ Inheritance models 'is-a' relationship between two classes.
▪ Whenever two classes have 'is-a' relationship, we use inheritance.
▪ Common features are kept in superclass. A subclass inherits all instance
variables and methods from superclass and it may have its own added variables
and methods.
▪ Note that constructors are not inherited in subclass.
▪ When an object of subclass is instantiated, memory is allocated for all its
attributes including inherited ones.
Inheritance
Inheritance
Inheritance
Inheritance
Inheritance

▪ Private Members of Superclass


– Private members are not directly accessible outside the class.
– These attributes belongs to subclass also, but still private members are not visible in its subclass.
– To make them available elsewhere, use public accessor and mutator methods.

▪ Protected Members of Superclass


– Protected members are available as private members in the inherited subclass

▪ Composition and Aggregation


– Composition and Aggregation are the constructor of classes that incorporate other objects.
– They establish a “has-a” relationship between classes.
– For e.g., if we define class “Lbirary”, it has a reading room. Here reading room is an object of the class “Room”.
When a class includes objects of other class, it is also referred to as container class.
Inheritance

You might also like