0% found this document useful (0 votes)
26 views20 pages

KEJDJFHSJ

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)
26 views20 pages

KEJDJFHSJ

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/ 20

CLASS

VARIABLE
Add a short description

11:11P
M
Topics Covered

Topic 1 Topic 2
TOPICS
Topic 3
What is Class Variable?
Is a variable that belongs to the class
itself, rather than to any particular
instance
of the class.
Example:
class Car:
# class variable
wheels = 4

def __init__(self, make, model):


self.make = make
self.model = model

def description(self):
print(f"This car is a {self.make} {self.model}
with {Car.wheels} wheels.")
Class variables are defined inside the class
definition, but outside any method, and are
accessible to all instances of the class.
Section Header • Section Header • Section Header • Section Header • Section Header • Section Header • Section Header • Section Header

INHERITANCE
Back to Agenda Page

Section Header • Section Header • Section Header • Section Header • Section Header • Section Header • Section Header • Section Header
What is INHERITANCE?
Is a key concept in object-oriented
programming (OOP) that allows a class to
inherit properties and behaviors from a
parent or superclass. This can save a lot of
time and effort in coding.
In addition, it allows developers to
reuse code and avoid duplicating
functionality.
In Java, inheritance is implemented
through the use of the extends
keyword.
1. Code reuse: Inheritance allows you to
reuse code from an existing class by creating
a subclass that extends it. This saves time and
effort by reducing the amount of code you
need to write.
2. Polymorphism: Inheritance enables
polymorphism, which means that you can use a
subclass object wherever a superclass object is
expected. This makes your code more flexible
and easier to maintain.
3. Simplified code: Inheritance simplifies
your code by allowing you to create a
hierarchy of classes that share common
properties and behaviors.
4. Increased flexibility: Inheritance makes it easier to
add new functionality to your code by creating a new
subclass that inherits from an existing class. This
allows you to modify or extend the behavior of the
original class without changing its code.
Example:
public class Animal {
private String name;
private int age;

public Animal(String name, int age) {


this.name = name;
this.age = age;
}

public void eat() {


System.out.println(name + " is eating.");
}
}
4. Increased flexibility: Inheritance makes it easier to
add new functionality to your code by creating a new
subclass that inherits from an existing class. This
allows you to modify or extend the behavior of the
original class without changing its code.
SUBCLASSES
WHAT IS SUBCLASSES?
Refers to a derived classes, heir classes, or
child classes, are classes that inherit one or
more language entities from another
class/classes.
THE USE OF SUBCLASSES
Subclasses allow you to "extend" the functionality
of another class
(or subclass). Once you create a subclass, you gain
all the methods and properties of the class you are
subclassing and can then build upon it.
This is a powerful concept in OOP which spares
you from rewriting functionality, and instead
adding only what you need to an existing class.
Example:
--Boss subclass
local Boss = Person:extends("Boss")
function Boss:constructor(name)
--# Pass up the arguments to the parent class
Boss.super.constructor(self, name)
end

--instances
local manager = Manager:new("Jenny")
print(manager:getName()) --> Jenny

local boss = Boss:new("Jorge")


print(boss:getName()) --> Jorge
Thank you!

You might also like